Class: RakutenApi::Base::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/rakuten_api/base/model.rb

Direct Known Subclasses

GenreSearch::Model, ItemSearch::Model

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.castingObject



36
37
38
# File 'lib/rakuten_api/base/model.rb', line 36

def casting
  {}
end

.downcase(word) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rakuten_api/base/model.rb', line 24

def downcase(word)
  _word = word.dup
  _word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
  _word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  _word.downcase!
  _word
end

.from_hash(hash) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/rakuten_api/base/model.rb', line 14

def from_hash(hash)
  obj = self.new
  hash.each_pair do |k, v|
    v = casting[k].call(v) if casting.include?(k)
    attribute = mapping.include?(k) ? mapping[k] : downcase(k)
    obj.send("#{attribute}=", v) if obj.respond_to?("#{attribute}=")
  end
  obj
end

.mappingObject



32
33
34
# File 'lib/rakuten_api/base/model.rb', line 32

def mapping
  {}
end

Instance Method Details

#to_hashObject



5
6
7
8
9
10
11
# File 'lib/rakuten_api/base/model.rb', line 5

def to_hash
  {}.tap do |h|
    instance_variables.each do |sym|
      h[sym.to_s.sub('@', '').to_sym] = instance_variable_get(sym)
    end
  end
end