Class: RemoteResource::AttributeSpecification

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_resource/attribute_specification.rb

Overview

A value object representing an attribute defined in RemoteResource::Base. An AttributeSpecification contains the attributes’ method name, its resource, and its API client that is used for lookup. It also calculates its key which is used to lookup its value in storage.

scope is evaluated outside of this object and should remain unchanged throughout its life cycle. scope is used in building the attribute’s key and its equality. target_object on the other hand is just a reference to the object that scope was evaluated on. It may change throughout the attribute’s life cycle and is not used in determining equality.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, base_class) ⇒ AttributeSpecification

Returns a new instance of AttributeSpecification.



19
20
21
22
# File 'lib/remote_resource/attribute_specification.rb', line 19

def initialize(name, base_class)
  @name = name
  @base_class = base_class
end

Instance Attribute Details

#base_classObject (readonly)

Returns the value of attribute base_class.



16
17
18
# File 'lib/remote_resource/attribute_specification.rb', line 16

def base_class
  @base_class
end

#nameObject (readonly) Also known as: method

Returns the value of attribute name.



16
17
18
# File 'lib/remote_resource/attribute_specification.rb', line 16

def name
  @name
end

Instance Method Details

#keyObject



46
47
48
49
# File 'lib/remote_resource/attribute_specification.rb', line 46

def key
  @key ||= AttributeKey.new(@base_class.class.underscore, resource_name,
                            @base_class.scope, @name)
end

#locationObject



42
43
44
# File 'lib/remote_resource/attribute_specification.rb', line 42

def location
  "#{@base_class.class.name}##{@name}"
end

#resource(resource_client = nil) ⇒ Object



38
39
40
# File 'lib/remote_resource/attribute_specification.rb', line 38

def resource(resource_client = nil)
  @base_class.send(:resource, resource_name, resource_client)
end

#resource_nameObject



34
35
36
# File 'lib/remote_resource/attribute_specification.rb', line 34

def resource_name
  @base_class.class.attributes[@name]
end

#to_hashObject



25
26
27
28
29
30
31
32
# File 'lib/remote_resource/attribute_specification.rb', line 25

def to_hash
  {
    name: @name,
    resource: resource_name,
    base_class: @base_class.class.symbol_name,
    location: location
  }
end