module Juvet::Attributable
Public Class Methods
new(attributes={})
click to toggle source
# File lib/juvet/attributable.rb, line 24 def initialize(attributes={}) initialize! attributes attribute! attributes end
Public Instance Methods
attribute!(attributes)
click to toggle source
# File lib/juvet/attributable.rb, line 7 def attribute!(attributes) (@_attributes ||= []).concat attributes.keys attributes.keys.each { |name| define_attribute! name } end
attributes()
click to toggle source
# File lib/juvet/attributable.rb, line 12 def attributes attributes = {} @_attributes.each do |attribute| if self.respond_to? attribute attributes[attribute] = self.send(attribute) end end attributes end
initialize!(attributes)
click to toggle source
# File lib/juvet/attributable.rb, line 3 def initialize!(attributes) attributes.each { |name, value| set_variable! name, value } end
Private Instance Methods
create_method(name, &block)
click to toggle source
# File lib/juvet/attributable.rb, line 29 def create_method(name, &block) self.class.send(:define_method, name, &block) end
define_attribute!(name)
click to toggle source
# File lib/juvet/attributable.rb, line 33 def define_attribute!(name) define_getter!(name) define_setter!(name) end
define_getter!(name)
click to toggle source
# File lib/juvet/attributable.rb, line 38 def define_getter!(name) create_method name do instance_variable_get("@#{name}") end unless respond_to? name end
define_setter!(name)
click to toggle source
# File lib/juvet/attributable.rb, line 44 def define_setter!(name) method = "#{name}=" create_method method do |value| instance_variable_set("@#{name}", value) end unless respond_to? method end
set_variable!(name, value)
click to toggle source
# File lib/juvet/attributable.rb, line 51 def set_variable!(name, value) variable = "@#{name}" instance_variable_set("@#{name}", value) \ unless instance_variable_defined? variable end