Class: RMSettings
- Inherits:
-
Object
show all
- Defined in:
- lib/motion-settings/rmsettings.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ RMSettings
Returns a new instance of RMSettings.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/motion-settings/rmsettings.rb', line 7
def initialize(*args)
args = args.flatten.compact.uniq
@available = {}
@settings ||= NSUserDefaults.standardUserDefaults
user_options = (args.select{|a| a.is_a? Hash}.first || {}).delete(:options) || {}
@options = { autosave: true,
lenient_keys: false,
default_type: :object}.merge(user_options)
args.flatten.each do |item|
if item.is_a?(Symbol)
@available[item] = {type: @options[:default_type]}
elsif item.is_a?(Hash)
item.each do |key, value|
@available[key] = value
end
end
end
@keys = @available.keys
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/motion-settings/rmsettings.rb', line 156
def method_missing(method, *args)
if m = method.to_s.match(/^([^\?\=]+)([\?\=])?$/)
base_key = m[1].to_sym
method_modifier = m[2]
if valid_key?(base_key)
return case method_modifier
when nil
setting_for(base_key)
when '='
save_setting(base_key, [*args].flatten.first)
when '?'
setting_for?(base_key)
else
super
end
end
end
super
end
|
Instance Attribute Details
#available ⇒ Object
Returns the value of attribute available.
5
6
7
|
# File 'lib/motion-settings/rmsettings.rb', line 5
def available
@available
end
|
#keys ⇒ Object
Returns the value of attribute keys.
5
6
7
|
# File 'lib/motion-settings/rmsettings.rb', line 5
def keys
@keys
end
|
#options ⇒ Object
Returns the value of attribute options.
5
6
7
|
# File 'lib/motion-settings/rmsettings.rb', line 5
def options
@options
end
|
#settings ⇒ Object
Returns the value of attribute settings.
5
6
7
|
# File 'lib/motion-settings/rmsettings.rb', line 5
def settings
@settings
end
|
Instance Method Details
#autosave ⇒ Object
38
39
40
41
|
# File 'lib/motion-settings/rmsettings.rb', line 38
def autosave
save if autosave?
end
|
#autosave? ⇒ Boolean
43
44
45
46
|
# File 'lib/motion-settings/rmsettings.rb', line 43
def autosave?
options[:autosave]
end
|
#default_type_for(key) ⇒ Object
48
49
50
51
|
# File 'lib/motion-settings/rmsettings.rb', line 48
def default_type_for(key)
((@available[key] || {})[:type] || @options[:default_type])
end
|
#remove(key) ⇒ Object
Also known as:
delete, reset
140
141
142
143
144
145
146
|
# File 'lib/motion-settings/rmsettings.rb', line 140
def remove(key)
settings.removeObjectForKey key
autosave
setting_for key
end
|
#save ⇒ Object
31
32
33
34
35
36
|
# File 'lib/motion-settings/rmsettings.rb', line 31
def save
settings.synchronize
end
|
#save_setting(key, value) ⇒ Object
Also known as:
[]=
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/motion-settings/rmsettings.rb', line 70
def save_setting(key, value)
case default_type_for(key)
when :boolean
settings.setBool value, forKey: key
when :double
settings.setDouble value, forKey: key
when :float
settings.setFloat value, forKey: key
when :integer
settings.setInteger value, forKey: key
when :object
settings.setObject value, forKey: key
when :url
settings.setURL value, forKey: key
else
settings.setObject value, forKey: key
end
autosave
setting_for key
end
|
#setting_for(key) ⇒ Object
Also known as:
[]
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/motion-settings/rmsettings.rb', line 104
def setting_for(key)
case default_type_for(key)
when :array
settings.arrayForKey key
when :boolean
settings.boolForKey key
when :data
settings.dataForKey key
when :dictionary
settings.dictionaryForKey key
when :double
settings.doubleForKey key
when :float
settings.floatForKey key
when :integer
settings.integerForKey key
when :object
settings.objectForKey key
when :string_array
settings.stringArrayForKey key
when :string
settings.stringForKey key
when :url
settings.URLForKey key
end
end
|
#setting_for?(key) ⇒ Boolean
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/motion-settings/rmsettings.rb', line 53
def setting_for?(key)
value = setting_for(key)
case default_type_for(key)
when :string, :array
value.empty?
else
!!value
end
end
|
#valid_key?(key) ⇒ Boolean
150
151
152
153
154
|
# File 'lib/motion-settings/rmsettings.rb', line 150
def valid_key?(key)
@options[:lenient_keys] or keys.include?(key)
end
|