Class: RMSettings

Inherits:
Object
  • 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)
  # Initializes the available settings and options
  # See rmsettable.rb for details on the various options
  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)
  # Split the method to componenet parts to do our magic.
  if m = method.to_s.match(/^([^\?\=]+)([\?\=])?$/)
    base_key        = m[1].to_sym
    method_modifier = m[2]
    # Check if we want to do something here.
    if valid_key?(base_key)
      return  case method_modifier
              when nil
                # Call the reader
                setting_for(base_key)
              when '='
                # Call the writer
                save_setting(base_key, [*args].flatten.first)
              when '?'
                # Call the boolean check
                setting_for?(base_key)
              else
                # Nothing to do, so let's fail
                super
              end
    end
  end
  super
end

Instance Attribute Details

#availableObject

Returns the value of attribute available.



5
6
7
# File 'lib/motion-settings/rmsettings.rb', line 5

def available
  @available
end

#keysObject

Returns the value of attribute keys.



5
6
7
# File 'lib/motion-settings/rmsettings.rb', line 5

def keys
  @keys
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/motion-settings/rmsettings.rb', line 5

def options
  @options
end

#settingsObject

Returns the value of attribute settings.



5
6
7
# File 'lib/motion-settings/rmsettings.rb', line 5

def settings
  @settings
end

Instance Method Details

#autosaveObject



38
39
40
41
# File 'lib/motion-settings/rmsettings.rb', line 38

def autosave
  # Automatically saves the data unless autosave is false.
  save if autosave?
end

#autosave?Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/motion-settings/rmsettings.rb', line 43

def autosave?
  # Check to see if the autosave option is selected
  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)
  # Determine the appropriate type for reading and writing item
  ((@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)
  # Resets the key to it's default value and returns that value
  # Aliased as delete and reset
  settings.removeObjectForKey key
  autosave
  setting_for key
end

#saveObject



31
32
33
34
35
36
# File 'lib/motion-settings/rmsettings.rb', line 31

def save
  # Save all current settings to the device.
  # Can be called manually but is generally called automatically
  # after updating a key unless the :autosave option is false.
  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)
  # Save the contents of value to the key.
  # Returns the value of the key after setting.
  #
  # Generally called dynamically from the method_missing function.
  #
  #    settings.save_setting :name, 'Karl Pilkington'
  #
  #    settings.head_shape = 'Orange'
  #
  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

  # return the current value
  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)
  # Read the contents of a given key.
  #
  # Generally called dynamically from the method_missing function.
  #
  #    settings.setting_for :nickname
  #
  #    settings.nickname
  #
  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

Returns:

  • (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)
  # Call to check boolean status of a particular key.
  # Generally called dynamically from the method_missing function.
  #
  #    settings.setting_for? :tutorial_completed
  #
  #    settings.tutorial_completed?
  #
  value = setting_for(key)
  case default_type_for(key)
  when :string, :array
    value.empty?
  else
    !!value
  end
end

#valid_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
154
# File 'lib/motion-settings/rmsettings.rb', line 150

def valid_key?(key)
  # Checks to see if the method_missing key has been declared
  # unless that functionality has been turned off in the settings
  @options[:lenient_keys] or keys.include?(key)
end