class ActiveAttr::AttributeDefinition

Represents an attribute for reflection

@example Usage

AttributeDefinition.new(:amount)

@since 0.2.0

Attributes

name[R]

The attribute name @since 0.2.0

options[R]

The attribute options @since 0.5.0

Public Class Methods

new(name, options={}) click to toggle source

Creates a new AttributeDefinition

@example Create an attribute defintion

AttributeDefinition.new(:amount)

@param [Symbol, String, to_sym] name attribute name @param [Hash{Symbol => Object}] options attribute options

@return [ActiveAttr::AttributeDefinition]

@since 0.2.0

# File lib/active_attr/attribute_definition.rb, line 55
def initialize(name, options={})
  raise TypeError, "can't convert #{name.class} into Symbol" unless name.respond_to? :to_sym
  @name = name.to_sym
  @options = options
end

Public Instance Methods

<=>(other) click to toggle source

Compare attribute definitions

@example

attribute_definition <=> other

@param [ActiveAttr::AttributeDefinition, Object] other The other

attribute definition to compare with.

@return [-1, 0, 1, nil]

@since 0.2.1

# File lib/active_attr/attribute_definition.rb, line 26
def <=>(other)
  return nil unless other.instance_of? self.class
  return nil if name == other.name && options != other.options
  self.name.to_s <=> other.name.to_s
end
[](key) click to toggle source

Read an attribute option

@example

attribute_definition[:type]

@param [Symbol] key The option key

@since 0.5.0

# File lib/active_attr/attribute_definition.rb, line 40
def [](key)
  @options[key]
end
inspect() click to toggle source

Returns the code that would generate the attribute definition

@example Inspect the attribute definition

attribute.inspect

@return [String] Human-readable presentation of the attribute

definition

@since 0.6.0

# File lib/active_attr/attribute_definition.rb, line 70
def inspect
  options_description = options.map { |key, value| "#{key.inspect} => #{value.inspect}" }.sort.join(", ")
  inspected_options = ", #{options_description}" unless options_description.empty?
  "attribute :#{name}#{inspected_options}"
end
to_s() click to toggle source

The attribute name

@return [String] the attribute name

@since 0.2.0

# File lib/active_attr/attribute_definition.rb, line 81
def to_s
  name.to_s
end
to_sym() click to toggle source

The attribute name

@return [Symbol] the attribute name

@since 0.2.1

# File lib/active_attr/attribute_definition.rb, line 90
def to_sym
  name
end