Class: ClashOfClansApi::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/clash_of_clans_api/models/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, client) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
# File 'lib/clash_of_clans_api/models/base.rb', line 9

def initialize(hash, client)
	@hash   = hash
	@client = client
	
	validate!
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/clash_of_clans_api/models/base.rb', line 7

def client
  @client
end

Class Method Details

.property(method_name, key, type: nil, required: false, default: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/clash_of_clans_api/models/base.rb', line 29

def property(method_name, key, type: nil, required: false, default: nil)
	define_method(method_name) do
		deduced_type =
			case type
				when Symbol
					send(type)
				when String
					self.class.const_get(type)
				else
					type
			end
		
		if !@hash.key?(key)
			default
		elsif deduced_type.nil?
			self[key]
		elsif property_cached?(method_name)
			property_from_cache(method_name)
		else
			initializer_proc = proc do |item|
				if deduced_type.ancestors.include?(ClashOfClansApi::Models::Base)
					deduced_type.new(item, self.client)
				elsif deduced_type == DateTime
					DateTime.parse(item)
				else
					deduced_type.new(item)
				end
			end
			
			cache_property(method_name, self[key].then do |prop|
				prop.is_a?(Array) ? prop.map(&initializer_proc) : initializer_proc.call(prop)
			end)
		end
	end
	
	registered_properties[key] = {
		method_name: method_name,
		required:    required,
		default:     default,
		type:        type,
	}
end

.registered_propertiesObject



25
26
27
# File 'lib/clash_of_clans_api/models/base.rb', line 25

def registered_properties
	@registered_properties ||= {}
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
# File 'lib/clash_of_clans_api/models/base.rb', line 20

def [](key)
	@hash[key]
end

#cache_property(method_name, obj) ⇒ Object



77
78
79
80
81
# File 'lib/clash_of_clans_api/models/base.rb', line 77

def cache_property(method_name, obj)
	@property_cache ||= {}
	
	@property_cache[method_name] = obj
end

#property_cached?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/clash_of_clans_api/models/base.rb', line 73

def property_cached?(method_name)
	@property_cache && @property_cache.key?(method_name)
end

#property_from_cache(method_name) ⇒ Object



83
84
85
# File 'lib/clash_of_clans_api/models/base.rb', line 83

def property_from_cache(method_name)
	@property_cache[method_name]
end

#to_hObject



16
17
18
# File 'lib/clash_of_clans_api/models/base.rb', line 16

def to_h
	@hash
end

#validate!Object



87
88
89
90
91
92
93
94
95
# File 'lib/clash_of_clans_api/models/base.rb', line 87

def validate!
	missing = self.class.registered_properties.reject do |field_name, properties|
		!properties[:required] || @hash.key?(field_name)
	end
	
	if missing.any?
		raise InvalidDataError, "The following keys are required, but missing from the model data: #{missing.keys.map(&:inspect).join(', ')}"
	end
end