Class: ProviderKit::Settings
- Inherits:
-
Object
- Object
- ProviderKit::Settings
show all
- Defined in:
- lib/provider_kit/settings.rb
Overview
Serializable object to read/write from account settings fields
Defined Under Namespace
Classes: Jsonb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(raw_data) ⇒ Settings
Returns a new instance of Settings.
9
10
11
|
# File 'lib/provider_kit/settings.rb', line 9
def initialize(raw_data)
@data = build_data(raw_data)&.symbolize_keys || {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ 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
|
# File 'lib/provider_kit/settings.rb', line 29
def method_missing(method_name, *args)
super if data.nil?
if match = method_name.to_s.match(/^(?<attribute>[a-z0-9_]+)$/)
key = match[:attribute].to_sym
if Hash === data[key]
self.class.new(data[key])
else
data[key]
end
elsif match = method_name.to_s.match(/^(?<attribute>[a-z0-9_]+)=$/)
key = match[:attribute].to_sym
data[key] = args.first
elsif match = method_name.to_s.match(/^(?<attribute>[a-z0-9_]+)\?$/)
key = match[:attribute].to_sym
data.has_key?(key) && (data[key] == true || data[key] == "1")
end
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
7
8
9
|
# File 'lib/provider_kit/settings.rb', line 7
def data
@data
end
|
Class Method Details
.dump(data) ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/provider_kit/settings.rb', line 83
def self.dump(data)
case data
when self
data.to_json
else
new(data).to_json
end
end
|
.load(data = nil) ⇒ Object
92
93
94
|
# File 'lib/provider_kit/settings.rb', line 92
def self.load(data = nil)
new(data.presence || {})
end
|
Instance Method Details
#[](key) ⇒ Object
13
14
15
|
# File 'lib/provider_kit/settings.rb', line 13
def [](key)
data[key]
end
|
#[]=(key, value) ⇒ Object
17
18
19
|
# File 'lib/provider_kit/settings.rb', line 17
def []=(key, value)
data[key] = value
end
|
#include?(key) ⇒ Boolean
21
22
23
|
# File 'lib/provider_kit/settings.rb', line 21
def include?(key)
data.include?(key)
end
|
#inspect ⇒ Object
25
26
27
|
# File 'lib/provider_kit/settings.rb', line 25
def inspect
@data.inspect
end
|
#respond_to?(_) ⇒ Boolean
always yes so this gets picked up by ‘method_missing`
57
58
59
|
# File 'lib/provider_kit/settings.rb', line 57
def respond_to?(_)
true
end
|
#to_h ⇒ Object
73
74
75
|
# File 'lib/provider_kit/settings.rb', line 73
def to_h
data.presence || {}
end
|
#to_json ⇒ Object
77
78
79
80
81
|
# File 'lib/provider_kit/settings.rb', line 77
def to_json
if data.present?
data.to_json
end
end
|
#update(value = {}) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/provider_kit/settings.rb', line 61
def update(value = {})
existing = (data.presence || {}).symbolize_keys
value = value.try(:to_h) if value.respond_to?(:to_h)
value = nil unless value.is_a?(Hash)
value = {} unless value.present?
value = value.symbolize_keys
updated = existing.merge(value)
@data = build_data(updated)
end
|