Class: Ez::Settings::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/ez/settings/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#backendObject (readonly)

Returns the value of attribute backend.



9
10
11
# File 'lib/ez/settings/store.rb', line 9

def backend
  @backend
end

#errorsObject (readonly)

Returns the value of attribute errors.



9
10
11
# File 'lib/ez/settings/store.rb', line 9

def errors
  @errors
end

#groupObject (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

Returns:

  • (Boolean)


33
34
35
# File 'lib/ez/settings/store.rb', line 33

def invalid?
  !valid?
end

#schemaObject



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

Returns:

  • (Boolean)


29
30
31
# File 'lib/ez/settings/store.rb', line 29

def valid?
  errors.empty?
end

#validateObject



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