Class: Footstats::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/footstats/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



5
6
7
8
9
10
11
# File 'lib/footstats/resource.rb', line 5

def initialize(attributes={})
  attributes.each do |attr_name, value|
    if self.class.fields.include? attr_name.to_sym
      send("#{self.class.fields[attr_name.to_sym]}=", value)
    end
  end
end

Class Method Details

.field(internal_key, external_key) ⇒ Object



17
18
19
20
# File 'lib/footstats/resource.rb', line 17

def self.field(internal_key, external_key)
  attr_accessor internal_key
  fields[external_key] = internal_key unless fields.include? external_key
end

.fieldsObject



13
14
15
# File 'lib/footstats/resource.rb', line 13

def self.fields
  @fields ||= {}
end

Instance Method Details

#to_hashObject



22
23
24
25
26
27
28
# File 'lib/footstats/resource.rb', line 22

def to_hash
  hash = Hash.new
  self.class.fields.values.collect do |key|
    hash[key] = self.send(key)
  end
  hash
end

#to_json(*params) ⇒ Object



30
31
32
# File 'lib/footstats/resource.rb', line 30

def to_json(*params)
  self.to_hash.to_json
end