class Gifnoc::Model

Public Class Methods

api_attribute_names() click to toggle source
# File lib/gifnoc/models/model.rb, line 14
def self.api_attribute_names
        return self.attribute_names & const_get(:API_ATTRIBUTE_LIST)
end
api_attributes_hash() click to toggle source
# File lib/gifnoc/models/model.rb, line 18
def self.api_attributes_hash
        return self.attributes_hash.select { |k, _| self.api_attribute_names.include?(k) }
end
attribute_names() click to toggle source
# File lib/gifnoc/models/model.rb, line 10
def self.attribute_names
        return self.attributes_hash.keys
end
attributes_hash() click to toggle source
# File lib/gifnoc/models/model.rb, line 6
def self.attributes_hash
        return const_get(:ATTRIBUTES)
end
from_api_hash(api_hash) click to toggle source
# File lib/gifnoc/models/model.rb, line 37
def self.from_api_hash(api_hash)
        validate_api_attributes(api_hash)
        args = api_hash.inject({}) do |hash, (key, value)|
                case api_attributes_hash[key.to_sym]
                when TypedAttrAccessor::TYPE_DATE_TIME
                        hash[key.to_sym] = value.nil? ? value : DateTime.iso8601(value)
                else
                        hash[key.to_sym] = value
                end
                hash
        end
        return new(**args)
end
new(**args) click to toggle source
# File lib/gifnoc/models/model.rb, line 22
def initialize(**args)
        raise NotImplementedError if self.class == Model
        self.class.validate_attributes(args)
        self.class.attribute_names.each { |arg| self.send("#{arg}=", args[arg]) }
end

Private Class Methods

validate_api_attributes(args) click to toggle source
# File lib/gifnoc/models/model.rb, line 53
def self.validate_api_attributes(args)
        return validate_attributes_internal(args, self.api_attribute_names)
end
validate_attributes(args) click to toggle source
# File lib/gifnoc/models/model.rb, line 57
def self.validate_attributes(args)
        return validate_attributes_internal(args, self.attribute_names)
end
validate_attributes_internal(args, allowed_arguments) click to toggle source

method should be considered private, but it's called from both an instance method and a class method

# File lib/gifnoc/models/model.rb, line 62
def self.validate_attributes_internal(args, allowed_arguments)
        current_arguments = args.keys.map(&:to_sym)
        allowed_arguments = allowed_arguments.map(&:to_sym)
        if (current_arguments | allowed_arguments).size > allowed_arguments.size
                raise ArgumentError, "incorrect arguments: #{self.name} has the following attributes: #{allowed_arguments}"
        end
end

Public Instance Methods

to_api_hash() click to toggle source
# File lib/gifnoc/models/model.rb, line 28
def to_api_hash
        return self.class.api_attributes_hash.inject({}) do |hash, (name, type)|
                value = self.send(name)
                value = value&.to_s if type == TypedAttrAccessor::TYPE_DATE_TIME
                hash[name.to_s] = value
                next(hash)
        end
end