module Garb::Attributes::ClassMethods

Public Instance Methods

ga_attribute(*keys) click to toggle source
# File lib/garb/attributes.rb, line 8
def ga_attribute(*keys)
  hashes, symbols = keys.partition { |k| k.is_a? Hash }
  symbols.each { |key| define_a_method_for key }
  hashes.each { |hash| map_values_for hash }
end

Private Instance Methods

define_a_method_for(key, custom_key = nil) click to toggle source
# File lib/garb/attributes.rb, line 15
def define_a_method_for(key, custom_key = nil)
  custom_key ||= key.to_s.camelize(:lower)
  define_method(key) do
    var = "@#{key}"
    if instance_variable_defined? var
      instance_variable_get var
    else
      hash_key = (custom_key || key).to_s
      instance_variable_set var, @entry[hash_key]
    end
  end #unless method_defined?(key)
end
map_values_for(hash) click to toggle source
# File lib/garb/attributes.rb, line 28
def map_values_for(hash)
  hash.each_pair do |key, value|
    define_a_method_for key, value
  end
end