Class: ActiveAttr::AttributeDefinition

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/active_attr/attribute_definition.rb

Overview

Represents an attribute for reflection

Examples:

Usage

AttributeDefinition.new(:amount)

Since:

  • 0.2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ ActiveAttr::AttributeDefinition

Creates a new AttributeDefinition

Examples:

Create an attribute defintion

AttributeDefinition.new(:amount)

Raises:

  • (TypeError)

Since:

  • 0.2.0



55
56
57
58
59
# 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

Instance Attribute Details

#nameObject (readonly)

The attribute name

Since:

  • 0.2.0



13
14
15
# File 'lib/active_attr/attribute_definition.rb', line 13

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ -1, ...

Compare attribute definitions

Examples:

attribute_definition <=> other

Since:

  • 0.2.1



26
27
28
29
30
# 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) ⇒ Object

Read an attribute option

Examples:

attribute_definition[:type]

Since:

  • 0.5.0



40
41
42
# File 'lib/active_attr/attribute_definition.rb', line 40

def [](key)
  @options[key]
end

#inspectString

Returns the code that would generate the attribute definition

Examples:

Inspect the attribute definition

attribute.inspect

Since:

  • 0.6.0



70
71
72
73
74
# 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_sString

The attribute name

Since:

  • 0.2.0



81
82
83
# File 'lib/active_attr/attribute_definition.rb', line 81

def to_s
  name.to_s
end

#to_symSymbol

The attribute name

Since:

  • 0.2.1



90
91
92
# File 'lib/active_attr/attribute_definition.rb', line 90

def to_sym
  name
end