Class: ConfigStore::Model

Inherits:
Object
  • Object
show all
Extended by:
TypedAttrAccessor
Defined in:
lib/configstore/models/model.rb

Direct Known Subclasses

Account, Namespace, Record, Token

Constant Summary

Constants included from TypedAttrAccessor

TypedAttrAccessor::TYPE_BASE64, TypedAttrAccessor::TYPE_BOOLEAN, TypedAttrAccessor::TYPE_DATE_TIME, TypedAttrAccessor::TYPE_HASH, TypedAttrAccessor::TYPE_INTEGER, TypedAttrAccessor::TYPE_STRING

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TypedAttrAccessor

allowed_types, type_alias_to_type_array, type_checks, type_specific_checks, typed_attr_accessor

Constructor Details

#initialize(**args) ⇒ Model

Returns a new instance of Model.

Raises:

  • (NotImplementedError)


22
23
24
25
26
# File 'lib/configstore/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

Class Method Details

.api_attribute_namesObject



14
15
16
# File 'lib/configstore/models/model.rb', line 14

def self.api_attribute_names
	return self.attribute_names & const_get(:API_ATTRIBUTE_LIST)
end

.api_attributes_hashObject



18
19
20
# File 'lib/configstore/models/model.rb', line 18

def self.api_attributes_hash
	return self.attributes_hash.select { |k, _| self.api_attribute_names.include?(k) }
end

.attribute_namesObject



10
11
12
# File 'lib/configstore/models/model.rb', line 10

def self.attribute_names
	return self.attributes_hash.keys
end

.attributes_hashObject



6
7
8
# File 'lib/configstore/models/model.rb', line 6

def self.attributes_hash
	return const_get(:ATTRIBUTES)
end

.from_api_hash(api_hash) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/configstore/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

Instance Method Details

#to_api_hashObject



28
29
30
31
32
33
34
35
# File 'lib/configstore/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