Class: ActiveRemote::AttributeDefinition

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/active_remote/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, type = :value, **options) ⇒ ActiveAttr::AttributeDefinition

Creates a new AttributeDefinition

Examples:

Create an attribute defintion

AttributeDefinition.new(:amount)

Raises:

  • (TypeError)

Since:

  • 0.2.0



59
60
61
62
63
64
65
# File 'lib/active_remote/attribute_definition.rb', line 59

def initialize(name, type = :value, **options)
  raise TypeError, "can't convert #{name.class} into Symbol" unless name.respond_to? :to_sym

  @name = name.to_sym
  @type = ::ActiveModel::Type.lookup(type)
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

The attribute name

Since:

  • 0.2.0



15
16
17
# File 'lib/active_remote/attribute_definition.rb', line 15

def name
  @name
end

Instance Method Details

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

Compare attribute definitions

Examples:

attribute_definition <=> other

Since:

  • 0.2.1



28
29
30
31
32
33
# File 'lib/active_remote/attribute_definition.rb', line 28

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



43
44
45
# File 'lib/active_remote/attribute_definition.rb', line 43

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

#from_rpc(value) ⇒ Object

Since:

  • 0.2.0



67
68
69
# File 'lib/active_remote/attribute_definition.rb', line 67

def from_rpc(value)
  type.deserialize(value)
end

#from_user(value) ⇒ Object

Since:

  • 0.2.0



71
72
73
# File 'lib/active_remote/attribute_definition.rb', line 71

def from_user(value)
  type.cast(value)
end

#inspectString

Returns the code that would generate the attribute definition

Examples:

Inspect the attribute definition

attribute.inspect

Since:

  • 0.6.0



84
85
86
87
88
# File 'lib/active_remote/attribute_definition.rb', line 84

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



95
# File 'lib/active_remote/attribute_definition.rb', line 95

delegate :to_s, :to => :name

#to_symSymbol

The attribute name

Since:

  • 0.2.1



102
103
104
# File 'lib/active_remote/attribute_definition.rb', line 102

def to_sym
  name
end