Class: AdequateExposure::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/adequate_exposure/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Attribute

Public: Initialize an Attribute

options - Hash of options for the Attribute

:name      - The String name of the Attribute instance
:fetch     - The Proc fetch to calculate
             the value of the Attribute instance.
             This is only called if the attribute's
             instance variable is not defined.
:ivar_name - The String instance variable name that
             is associated with the attribute.


15
16
17
18
19
# File 'lib/adequate_exposure/attribute.rb', line 15

def initialize(options)
  @name = options.fetch(:name)
  @fetch = options.fetch(:fetch)
  @ivar_name = options.fetch(:ivar_name)
end

Instance Attribute Details

#fetchObject (readonly)

Returns the value of attribute fetch.



3
4
5
# File 'lib/adequate_exposure/attribute.rb', line 3

def fetch
  @fetch
end

#ivar_nameObject (readonly)

Returns the value of attribute ivar_name.



3
4
5
# File 'lib/adequate_exposure/attribute.rb', line 3

def ivar_name
  @ivar_name
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/adequate_exposure/attribute.rb', line 3

def name
  @name
end

Instance Method Details

#expose!(klass) ⇒ Object

Public: Expose a getter and setter method for the Attribute on the passed in Controller class.

klass - The Controller class where the Attribute getter and setter methods will be exposed.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/adequate_exposure/attribute.rb', line 41

def expose!(klass)
  attribute = self

  klass.instance_eval do
    define_method attribute.getter_method_name do
      Context.new(self, attribute).get
    end

    define_method attribute.setter_method_name do |value|
      Context.new(self, attribute).set(value)
    end
  end
end

#getter_method_nameObject

Public: The getter method for the Attribute.

Returns the name of the Attribute as a Symbol.



24
25
26
# File 'lib/adequate_exposure/attribute.rb', line 24

def getter_method_name
  name.to_sym
end

#setter_method_nameObject

Public: The setter method for the Attribute.

Returns the name of the attribute as a Symbol with an appended ‘=’.



31
32
33
# File 'lib/adequate_exposure/attribute.rb', line 31

def setter_method_name
  "#{name}=".to_sym
end