Class: RemoteResource::AttributeMethodAttacher

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

Overview

Public: Creates an instance of the AttributeMethodAttacher, which is responsible for setting up and attaching methods onto a target class which are used to lookup cached attributes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_class, options = {}) ⇒ AttributeMethodAttacher

Public: Creates an instance of the AttributeMethodAttacher, which is responsible for setting up and attaching methods onto a target class which are used to lookup cached attributes.

base_class - the class which will have methods attached to. options - options hash with the following keys (default: {}). Generally,

      prefix should be used when overriding the names of all the
      methods is desired and attributes_map should be used when
      overriding only a few method names is desired.
:prefix - prefix for the names of the newly created methods.
:attributes_map - a hash for overriding method names to create. The
                  keys in this hash represent an attribute defined on
                  the base_class. The values are the overriding name
                  of the method to be defined on the target_class.

Returns a new instance.



27
28
29
30
# File 'lib/remote_resource/attribute_method_attacher.rb', line 27

def initialize(base_class, options = {})
  @base_class = base_class
  @options = ensure_options(options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/remote_resource/attribute_method_attacher.rb', line 10

def options
  @options
end

Instance Method Details

#attach_to(target_class, path_to_attrs = 'self') ⇒ Object

Public: Set the base_classes’ attribute methods on the given target class. Sets a MethodResolver instance on the target_class as well. Logs warnings if any methods on the target class are overwritten.

target_class - The class upon which the base_classes’ attribute methods

should be set.

path_to_attrs - A string representing a line of ruby code that will be

evaluated. This line is the relative path from a
target_class instance to the object that holds the
attributes and where the `get_attribute` is defined. If
path_to_attrs is (default: self), that implies that this
is defining methods on the Base class, because that object
contains the `get_attribute` method.

Returns the target_class



47
48
49
50
# File 'lib/remote_resource/attribute_method_attacher.rb', line 47

def attach_to(target_class, path_to_attrs = 'self')
  overwrite_method_warnings(target_class) unless path_to_attrs == 'self'
  target_class.send(:include, make_attribute_methods_module(path_to_attrs))
end