class DecentExposure::Context

Attributes

attribute[R]
context[R]

Public Class Methods

new(context, attribute) click to toggle source

Public: Initialize a context.

context - The Class where the attribute is defined. attribute - The attribute that will be accessed by a getter

and setter.
# File lib/decent_exposure/context.rb, line 10
def initialize(context, attribute)
  @context, @attribute = context, attribute
end

Public Instance Methods

get() click to toggle source

Public: Read an attribute on the context Class.

Get an attribute's value. If the attribute's instance variable is not defined, it will create one, execute attribute#fetch, and assign the result to the instance variable.

Returns the attribute's value.

# File lib/decent_exposure/context.rb, line 22
def get
  ivar_defined? ? ivar_get : set(fetch_value)
end
set(value) click to toggle source

Public: Write to an attribute on the context Class.

value - The value that will be set to the attribute's

instance variable.

Returns the attribute's value.

# File lib/decent_exposure/context.rb, line 32
def set(value)
  ivar_set(value)
end

Private Instance Methods

fetch_value() click to toggle source
# File lib/decent_exposure/context.rb, line 57
def fetch_value
  context.instance_exec(&attribute.fetch)
end
ivar_defined?() click to toggle source
# File lib/decent_exposure/context.rb, line 41
def ivar_defined?
  instance_variable_defined?(ivar_name)
end
ivar_get() click to toggle source
# File lib/decent_exposure/context.rb, line 45
def ivar_get
  instance_variable_get(ivar_name)
end
ivar_name() click to toggle source
# File lib/decent_exposure/context.rb, line 53
def ivar_name
  "@#{attribute.ivar_name.gsub("?", "_question_mark_")}"
end
ivar_set(value) click to toggle source
# File lib/decent_exposure/context.rb, line 49
def ivar_set(value)
  instance_variable_set(ivar_name, value)
end