Sha256: 44222a82394bf57704c18065f2a95233aa929d2866df8e7b2dc5578d4eda032b

Contents?: true

Size: 711 Bytes

Versions: 1

Compression:

Stored size: 711 Bytes

Contents

class Stats
  
  def initialize(hash)
    self.dynamic_attr(hash)

  end

  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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
best_friend-0.1.1 lib/best_friend/stats.rb