Class: Ez::Settings::Store
- Inherits:
-
Object
- Object
- Ez::Settings::Store
- Defined in:
- lib/ez/settings/store.rb
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
Instance Method Summary collapse
-
#initialize(group, backend) ⇒ Store
constructor
A new instance of Store.
- #invalid? ⇒ Boolean
- #schema ⇒ Object
- #update(params) ⇒ Object
- #valid? ⇒ Boolean
- #validate ⇒ Object
Constructor Details
#initialize(group, backend) ⇒ Store
Returns a new instance of Store.
11 12 13 14 15 16 17 18 19 |
# File 'lib/ez/settings/store.rb', line 11 def initialize(group, backend) @group = group @errors = ActiveModel::Errors.new(self) @backend = backend define_accessors keys.each { |key| default_or_exists_value(data, key) } end |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
9 10 11 |
# File 'lib/ez/settings/store.rb', line 9 def backend @backend end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
9 10 11 |
# File 'lib/ez/settings/store.rb', line 9 def errors @errors end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
9 10 11 |
# File 'lib/ez/settings/store.rb', line 9 def group @group end |
Instance Method Details
#invalid? ⇒ Boolean
33 34 35 |
# File 'lib/ez/settings/store.rb', line 33 def invalid? !valid? end |
#schema ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/ez/settings/store.rb', line 48 def schema { group.name => group.keys.map(&:name).each_with_object({}) do |key_name, schema| schema[key_name] = send(key_name) end } end |
#update(params) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ez/settings/store.rb', line 37 def update(params) params.each { |key, value| self.public_send("#{key}=", value) } validate return self unless errors.empty? backend.write(schema) self end |
#valid? ⇒ Boolean
29 30 31 |
# File 'lib/ez/settings/store.rb', line 29 def valid? errors.empty? end |
#validate ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/ez/settings/store.rb', line 21 def validate @errors = ActiveModel::Errors.new(self) group.keys.select(&:required?).each do |key| errors.add(key.name, "can't be blank") if self.send(key.name).blank? end end |