Class: SettingsManager::Base
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SettingsManager::Base
- Includes:
- Default, Validation
- Defined in:
- lib/settings-manager/base.rb
Instance Attribute Summary collapse
-
#base_obj ⇒ Object
readonly
Returns the value of attribute base_obj.
Class Method Summary collapse
- .[](key) ⇒ Object
- .[]=(key, value) ⇒ Object
- .base_query ⇒ Object
- .destroy!(key) ⇒ Object
- .get_all ⇒ Object
- .method_missing(method, *args) ⇒ Object
- .object(key) ⇒ Object
- .set(settings = {}) ⇒ Object
Instance Method Summary collapse
Methods included from Validation
Instance Attribute Details
#base_obj ⇒ Object (readonly)
Returns the value of attribute base_obj.
10 11 12 |
# File 'lib/settings-manager/base.rb', line 10 def base_obj @base_obj end |
Class Method Details
.[](key) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/settings-manager/base.rb', line 23 def [](key) if key_allowed?(key) object = object(key) if object.present? object.value else default_setting_for(key) end else raise Errors::KeyInvalidError.new(key) end end |
.[]=(key, value) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/settings-manager/base.rb', line 37 def []=(key, value) key = key.to_s unless key_allowed?(key) raise Errors::KeyInvalidError.new(key) end attributes = { :key => key } if @base_obj attributes[:base_obj_id] = @base_obj.id attributes[:base_obj_type] = @base_obj.class.base_class.to_s end record = object(key) || self.new(attributes) record.value = value record.save! value rescue ActiveRecord::RecordInvalid => e new_exception = Errors::InvalidError.new new_exception.errors << e.record.errors raise new_exception end |
.base_query ⇒ Object
125 126 127 |
# File 'lib/settings-manager/base.rb', line 125 def base_query where(:base_obj_id => nil, :base_obj_type => nil) end |
.destroy!(key) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/settings-manager/base.rb', line 63 def destroy!(key) record = object(key.to_s) if record.present? record.destroy! else raise Errors::SettingNotFoundError, "setting for `#{key.to_s}` not found" end end |
.get_all ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/settings-manager/base.rb', line 73 def get_all result = default_settings base_query.each do |record| result[record.key] = record.value end result end |
.method_missing(method, *args) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/settings-manager/base.rb', line 83 def method_missing(method, *args) method_name = method.to_s super(method, *args) rescue NoMethodError => e if method_name[-1] == "=" key = method_name.sub("=", "") value = args.first self[key] = value else self[method_name] end end |
.object(key) ⇒ Object
97 98 99 100 101 |
# File 'lib/settings-manager/base.rb', line 97 def object(key) return nil unless Rails.application.initialized? && table_exists? base_query.find_by(:key => key.to_s) end |
.set(settings = {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/settings-manager/base.rb', line 103 def set(settings = {}) ActiveRecord::Base.transaction do exception = nil settings.each do |key, value| begin self[key.to_s] = value rescue Errors::KeyInvalidError => e exception ||= Errors::InvalidError.new exception.errors << e. rescue Errors::InvalidError => e exception ||= Errors::InvalidError.new e.errors.each { |error| exception.errors << error } end end raise exception if exception.present? end self.get_all end |
Instance Method Details
#value ⇒ Object
14 15 16 |
# File 'lib/settings-manager/base.rb', line 14 def value YAML.load(self[:value]) if self[:value].present? end |
#value=(new_value) ⇒ Object
18 19 20 |
# File 'lib/settings-manager/base.rb', line 18 def value=(new_value) self[:value] = new_value.to_yaml end |