Class: LessonlyApi::ResourceType::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Serializers::JSON
Defined in:
lib/lessonly_api/resource_type/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



66
67
68
69
70
# File 'lib/lessonly_api/resource_type/base.rb', line 66

def initialize(attributes = {})
  defined_attributes = attributes.with_indifferent_access.slice(*fields_names)

  super(defined_attributes)
end

Class Method Details

.dump(data_type) ⇒ Object



35
36
37
# File 'lib/lessonly_api/resource_type/base.rb', line 35

def dump(data_type)
  data_type.to_json
end

.fields(*keys_with_types) ⇒ Object



24
25
26
27
28
29
# File 'lib/lessonly_api/resource_type/base.rb', line 24

def fields(*keys_with_types)
  keys_with_types.each do |name_or_hash|
    field_name = define_methods_for(name_or_hash)
    self.fields_names += [field_name]
  end
end

.from_json(json) ⇒ Object



17
18
19
20
21
22
# File 'lib/lessonly_api/resource_type/base.rb', line 17

def from_json(json)
  parsed_json = json && JSON.parse(json).to_h
  return new({}) unless parsed_json

  new(parsed_json)
end

.load(string) ⇒ Object



31
32
33
# File 'lib/lessonly_api/resource_type/base.rb', line 31

def load(string)
  from_json(string)
end

Instance Method Details

#attributesObject



72
73
74
75
76
77
# File 'lib/lessonly_api/resource_type/base.rb', line 72

def attributes
  fields_names.each_with_object({}) do |field, hash|
    value = public_send(field)
    hash[field] = value if value
  end
end