class Attrocity::AttributeMethodsBuilder

Attributes

attributes[R]
object[R]

Public Class Methods

for_attribute(obj, attribute) click to toggle source
# File lib/attrocity/attributes/attribute_methods_builder.rb, line 10
def self.for_attribute(obj, attribute)
  self.new(obj, Array(attribute))
end
for_attribute_set(obj, attribute_set) click to toggle source
# File lib/attrocity/attributes/attribute_methods_builder.rb, line 14
def self.for_attribute_set(obj, attribute_set)
  self.new(obj, attribute_set.attributes)
end
new(object, attributes) click to toggle source
# File lib/attrocity/attributes/attribute_methods_builder.rb, line 6
def initialize(object, attributes)
  @object, @attributes = object, attributes
end

Public Instance Methods

build() click to toggle source
# File lib/attrocity/attributes/attribute_methods_builder.rb, line 18
def build
  attributes.each do |attr|
    define_methods(attr)
  end
end
define_methods(attribute) click to toggle source
# File lib/attrocity/attributes/attribute_methods_builder.rb, line 24
def define_methods(attribute)
  define_reader(attribute)
  define_writer(attribute)
  define_predicate(attribute)
end
define_predicate(attribute) click to toggle source
# File lib/attrocity/attributes/attribute_methods_builder.rb, line 40
def define_predicate(attribute)
  object.define_singleton_method("#{attribute.name}?") do
    # TODO: Would true & attribute.value work better here?
    !!(attribute.value)
  end
end
define_reader(attribute) click to toggle source
# File lib/attrocity/attributes/attribute_methods_builder.rb, line 30
def define_reader(attribute)
  object.define_singleton_method(attribute.name) { attribute.value }
end
define_writer(attribute) click to toggle source
# File lib/attrocity/attributes/attribute_methods_builder.rb, line 34
def define_writer(attribute)
  object.define_singleton_method("#{attribute.name}=") do |value|
    attribute.value = value
  end
end