Class: ActiveSetting::Setting
- Inherits:
-
Object
- Object
- ActiveSetting::Setting
- Defined in:
- lib/active_setting/setting.rb
Overview
< ActiveRecord::Base
Instance Attribute Summary collapse
-
#category ⇒ Object
Returns the value of attribute category.
-
#data_type ⇒ Object
Returns the value of attribute data_type.
-
#default ⇒ Object
Returns the value of attribute default.
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
Returns the value of attribute name.
-
#object_options ⇒ Object
writeonly
Sets the attribute object_options.
-
#options ⇒ Object
Returns the value of attribute options.
-
#raw_value ⇒ Object
Returns the value of attribute raw_value.
-
#subtype ⇒ Object
Returns the value of attribute subtype.
Class Method Summary collapse
- .convert_value(val, data_type) ⇒ Object
- .define_shortcut_method(setting) ⇒ Object
- .register(name, options) ⇒ Object
- .registered_settings ⇒ Object
Instance Method Summary collapse
-
#calculate_object_options ⇒ Object
DEPRECATED: Please use standard options instead.
-
#initialize(attr = {}) ⇒ Setting
constructor
A new instance of Setting.
- #objects_from_collection(collection, key, value) ⇒ Object
- #register ⇒ Object
- #setting ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(attr = {}) ⇒ Setting
Returns a new instance of Setting.
12 13 14 15 16 17 |
# File 'lib/active_setting/setting.rb', line 12 def initialize(attr = {}) attr.each do |key, value| setter = "#{key}=" send(setter, value) if respond_to?(setter) end end |
Instance Attribute Details
#category ⇒ Object
Returns the value of attribute category.
5 6 7 |
# File 'lib/active_setting/setting.rb', line 5 def category @category end |
#data_type ⇒ Object
Returns the value of attribute data_type.
6 7 8 |
# File 'lib/active_setting/setting.rb', line 6 def data_type @data_type end |
#default ⇒ Object
Returns the value of attribute default.
5 6 7 |
# File 'lib/active_setting/setting.rb', line 5 def default @default end |
#description ⇒ Object
Returns the value of attribute description.
5 6 7 |
# File 'lib/active_setting/setting.rb', line 5 def description @description end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/active_setting/setting.rb', line 5 def name @name end |
#object_options=(value) ⇒ Object (writeonly)
Sets the attribute object_options
60 61 62 |
# File 'lib/active_setting/setting.rb', line 60 def (value) @object_options = value end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/active_setting/setting.rb', line 6 def @options end |
#raw_value ⇒ Object
Returns the value of attribute raw_value.
5 6 7 |
# File 'lib/active_setting/setting.rb', line 5 def raw_value @raw_value end |
#subtype ⇒ Object
Returns the value of attribute subtype.
6 7 8 |
# File 'lib/active_setting/setting.rb', line 6 def subtype @subtype end |
Class Method Details
.convert_value(val, data_type) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/active_setting/setting.rb', line 92 def self.convert_value(val, data_type) case data_type when :boolean then ![nil, false, 'false', 0, '0'].include?(val) when :integer then val.to_i when :string then val.to_s when :symbol then val.to_sym when :decimal then BigDecimal(val) else val end end |
.define_shortcut_method(setting) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/active_setting/setting.rb', line 29 def self.define_shortcut_method(setting) class_eval <<-TEXT def self.#{setting.name} self.class.registered_settings[:#{setting.name}].value end def self.#{setting.name}=(value) self.class.registered_settings[:#{setting.name}].raw_value = value end TEXT end |
.register(name, options) ⇒ Object
19 20 21 |
# File 'lib/active_setting/setting.rb', line 19 def self.register(name, ) new(.merge(name: name)).register end |
.registered_settings ⇒ Object
8 9 10 |
# File 'lib/active_setting/setting.rb', line 8 def self.registered_settings @registered_settings ||= {} end |
Instance Method Details
#calculate_object_options ⇒ Object
DEPRECATED: Please use standard options instead.
63 64 65 66 67 68 69 70 71 |
# File 'lib/active_setting/setting.rb', line 63 def puts '[WARNING] ActiveSetting::Setting#object_options is deprecated'\ ' as it poses a serious security risk and will be removed in future versions' objects, key, value = @object_options.split(' ') value = key if value.nil? || value == '' # TODO: Remove this method, as it uses eval !!! objects_from_collection(eval(objects), key, value) end |
#objects_from_collection(collection, key, value) ⇒ Object
73 74 75 |
# File 'lib/active_setting/setting.rb', line 73 def objects_from_collection(collection, key, value) collection.map { |o| [o.send(key), o.send(value)] } end |
#register ⇒ Object
23 24 25 26 27 |
# File 'lib/active_setting/setting.rb', line 23 def register self.class.registered_settings[name.to_sym] = self Setting.define_shortcut_method(self) self end |
#setting ⇒ Object
40 41 42 |
# File 'lib/active_setting/setting.rb', line 40 def setting self.class.registered_settings[@name.to_sym] end |
#value ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/active_setting/setting.rb', line 82 def value v = raw_value v = default if raw_value.nil? # TODO: WHY IS the first line here return nil if v.nil? @value ||= build_value(v) end |