Module: Serdee::Attributes::ClassMethods

Included in:
Nested
Defined in:
lib/serdee/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute(key, *args, &block) ⇒ Object



84
85
86
87
88
89
# File 'lib/serdee/attributes.rb', line 84

def attribute(key, *args, &block)
  attributes_class.attributes[key] = serializers[key] =
    Attribute.new(key, *args, &block)

  attributes_class.send(:attr_accessor, key)
end

#attributesObject



48
49
50
# File 'lib/serdee/attributes.rb', line 48

def attributes
  @attributes ||= {}
end

#attributes_classObject



80
81
82
# File 'lib/serdee/attributes.rb', line 80

def attributes_class
  self
end

#deserialize_key(method = nil, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/serdee/attributes.rb', line 66

def deserialize_key(method = nil, &block)
  if method
    @deserialize_key = ->(key) { self.send(method, key) }
  elsif block
    @deserialize_key = block
  else
    @deserialize_key || Serdee.deserialize_key
  end
end

#extract_to(data, obj) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/serdee/attributes.rb', line 123

def extract_to(data, obj)
  return if obj.nil?
  serializers.each do |_key, attribute|
    attribute.extract_to(data, obj)
  end
  obj
end

#from_json(json) ⇒ Object



105
106
107
# File 'lib/serdee/attributes.rb', line 105

def from_json(json)
  of_json(JSON.parse(json))
end

#insert_to(obj, data) ⇒ Object



131
132
133
134
135
136
# File 'lib/serdee/attributes.rb', line 131

def insert_to(obj, data)
  serializers.each do |_key, attribute|
    attribute.insert_to(obj, data)
  end
  data
end

#key_transformObject



76
77
78
# File 'lib/serdee/attributes.rb', line 76

def key_transform
  @key_transform || Serdee.default_key_transform
end

#nested(key, nested_class = nil, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/serdee/attributes.rb', line 91

def nested(key, nested_class = nil, &block)
  serializers[key] = Nested.new(
    attributes_class,
    key,
    nested_class&.serializers,
    &block
  )

  return unless nested_class

  attributes_class.attributes.merge!(nested_class.attributes)
  attributes_class.send(:attr_accessor, *nested_class.attributes.keys)
end

#of_json(json) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/serdee/attributes.rb', line 109

def of_json(json)
  allocate.tap do |obj|
    data = json.deep_transform_keys do |key|
      if deserialize_key
        deserialize_key.call(key).to_sym
      else
        key.to_sym
      end
    end
    extract_to(data, obj)
    obj.deserialized_from = json
  end
end

#serialize_key(method = nil, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/serdee/attributes.rb', line 56

def serialize_key(method = nil, &block)
  if method
    @serialize_key = ->(key) { self.send(method, key) }
  elsif block
    @serialize_key = block
  else
    @serialize_key || Serdee.serialize_key
  end
end

#serializersObject



52
53
54
# File 'lib/serdee/attributes.rb', line 52

def serializers
  @serializers ||= {}
end