class LessonlyApi::ResourceType::Base
Public Class Methods
dump(data_type)
click to toggle source
# File lib/lessonly_api/resource_type/base.rb, line 34 def dump(data_type) data_type.to_json end
fields(*keys_with_types)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/lessonly_api/resource_type/base.rb, line 30 def load(string) from_json(string) end
new(attributes = {})
click to toggle source
Calls superclass method
# 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
Private Class Methods
define_custom_field(name, data_type)
click to toggle source
# File lib/lessonly_api/resource_type/base.rb, line 48 def define_custom_field(name, data_type) attr_reader name define_method("#{name}=") do |value| value = LessonlyApi::Utils::CustomFieldConverter.new(data_type).convert(value) instance_variable_set("@#{name}", value) end name end
define_methods_for(name_or_hash)
click to toggle source
# File lib/lessonly_api/resource_type/base.rb, line 40 def define_methods_for(name_or_hash) if name_or_hash.is_a?(Hash) define_custom_field(*name_or_hash.to_a.first) else define_simple_field(name_or_hash) end end
define_simple_field(name)
click to toggle source
# File lib/lessonly_api/resource_type/base.rb, line 59 def define_simple_field(name) attr_accessor name name end
Public Instance Methods
attributes()
click to toggle source
# 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