Module: Serdee::Attributes

Defined in:
lib/serdee/attributes.rb

Defined Under Namespace

Modules: ClassMethods Classes: Nested

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#deserialized_fromObject

Returns the value of attribute deserialized_from.



10
11
12
# File 'lib/serdee/attributes.rb', line 10

def deserialized_from
  @deserialized_from
end

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/serdee/attributes.rb', line 6

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#as_jsonObject



33
34
35
36
37
38
39
40
41
# File 'lib/serdee/attributes.rb', line 33

def as_json
  self.class.insert_to(self, {}).deep_transform_keys! do |key|
    if self.class.serialize_key
      self.class.serialize_key.call(key.to_s)
    else
      key.to_s
    end
  end
end

#attributesObject



26
27
28
29
30
31
# File 'lib/serdee/attributes.rb', line 26

def attributes
  self.class.attributes.keys.each_with_object({}) do |key, hash|
    value = send(key)
    hash[key] = value if value
  end
end

#initialize(**values) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/serdee/attributes.rb', line 12

def initialize(**values)
  self.class.attributes.each do |key, attribute|
    if !attribute.optional && !values.key?(key)
      raise ArgumentError, "#{key} is required parameter"
    end

    if values.key?(key)
      send("#{key}=", values[key])
    elsif attribute.default
      send("#{key}=", attribute.default)
    end
  end
end

#to_jsonObject



43
44
45
# File 'lib/serdee/attributes.rb', line 43

def to_json
  as_json.to_json
end