Class: LessonlyApi::ResourceType::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
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.



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

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

  super(defined_attributes)
end

Class Method Details

.dump(data_type) ⇒ Object



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

def dump(data_type)
  data_type.to_json
end

.fields(*keys_with_types) ⇒ Object



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

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



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

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

  new(parsed_json)
end

.load(string) ⇒ Object



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

def load(string)
  from_json(string)
end

Instance Method Details

#attributesObject



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

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