Class: Staddress::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/staddress/models.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#rawObject (readonly)

元の(パース済み)レスポンス Hash。未定義フィールドはここから参照できる。



12
13
14
# File 'lib/staddress/models.rb', line 12

def raw
  @raw
end

Class Method Details

.camelize(name) ⇒ Object



38
39
40
41
# File 'lib/staddress/models.rb', line 38

def camelize(name)
  head, *rest = name.to_s.split("_")
  (rest.empty? ? head : head + rest.map(&:capitalize).join)
end

.field(name, key: nil, model: nil, list: false) ⇒ Object

フィールド定義。 name Ruby 側の snake_case 名(アクセサになる) key: JSON 側のキー(省略時は name を camelCase 化) model: ネストしたモデルクラス list: true なら配列としてマッピング



24
25
26
27
# File 'lib/staddress/models.rb', line 24

def field(name, key: nil, model: nil, list: false)
  fields[name] = { key: key || camelize(name), model: model, list: list }
  attr_reader name
end

.fieldsObject



15
16
17
# File 'lib/staddress/models.rb', line 15

def fields
  @fields ||= {}
end

.from(data) ⇒ Object

Hash からインスタンスを生成する。nil の場合は nil を返す。



30
31
32
33
34
35
36
# File 'lib/staddress/models.rb', line 30

def from(data)
  return nil if data.nil?

  obj = allocate
  obj.send(:populate, data, fields)
  obj
end