class Stats

Public Class Methods

new(hash) click to toggle source
# File lib/best_friend/stats.rb, line 3
def initialize(hash)
  self.dynamic_attr(hash)

end

Public Instance Methods

dynamic_attr(attributes) click to toggle source
# File lib/best_friend/stats.rb, line 8
def dynamic_attr(attributes)
  attributes.each do |attribute_key, attribute_value|
    if attribute_key
      # here we create in a dynamic way a the setter
      attribute_key = attribute_key.downcase.split(' ').join('_')
      self.class.send(:define_method, "#{attribute_key}=") do |value|
        instance_variable_set("@#{attribute_key}", value)
      end
      # here we create in a dynamic way the getter
      self.class.send(:define_method, attribute_key) do
        instance_variable_get("@#{attribute_key}")
      end
      self.send("#{attribute_key}=", attribute_value)
    end
  end
end