| Module | ActsAsHavingSettings::ClassMethods |
| In: |
lib/acts_as_having_settings.rb
|
# File lib/acts_as_having_settings.rb, line 4
4: def acts_as_having_settings(*args)
5: options = args.last.is_a?(Hash) ? args.pop : {}
6:
7: settings_field = options[:field] || 'settings'
8:
9: class_eval "serialize :\#{settings_field}, Hash\ndef self.settings_field\n\#{settings_field.inspect}\nend\ndef \#{settings_field}\nself[:\#{settings_field}] ||= Hash.new\nend\n"
10: settings_items(*args)
11: end
# File lib/acts_as_having_settings.rb, line 42
42: def acts_as_having_settings_type_cast(value, type)
43: # FIXME creating a new instance at every call, will the garbage collector
44: # be able to cope with it?
45: ActiveRecord::ConnectionAdapters::Column.new(:dummy, nil, type.to_s).type_cast(value)
46: end
# File lib/acts_as_having_settings.rb, line 22
22: def settings_items(*names)
23:
24: options = names.last.is_a?(Hash) ? names.pop : {}
25: default = (!options[:default].nil?) ? options[:default].inspect : "val"
26: data_type = options[:type] || :string
27:
28: names.each do |setting|
29: class_eval "def \#{setting}\nval = send(self.class.settings_field)[:\#{setting}]\nval.nil? ? \#{default} : val\nend\ndef \#{setting}=(value)\nsend(self.class.settings_field)[:\#{setting}] = self.class.acts_as_having_settings_type_cast(value, \#{data_type.inspect})\nend\n"
30: end
31: end