ez-settings 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1c689251af29504b287651f0ea45d8a205f7346818060ab3f3c2feef3c72f732
4
- data.tar.gz: 23d9764daf214809324a94799455a384c3beb5a51271bf0144a55b698e796c1e
3
+ metadata.gz: 66561fe692db0aab77ef9f297dc74afb7f14bc2d86d99c0b64799fc37623e7a4
4
+ data.tar.gz: 11799222bb160c1b7cdd19fe2dea75436cadb3feee340b0544d0b926183ed2b6
5
5
  SHA512:
6
- metadata.gz: 5a677cde3449caf666be6b1d9be206bef43157eecc617f3c97facb8fb814ec489c735d7ebad1b6f0e01a0172b7a03fa10a3f4318861af6812bca533d1e07bde6
7
- data.tar.gz: fad68463acafde3b937f7bd2cfa98e14f9d816b44e4b2511489f01eea114006e03bb7b98cc59ca8dda16076a36d4562e591af302d971ff96bd34f4c8974c2b93
6
+ metadata.gz: 315ef0bcaa2828278dd3395747920a38da72a664108722847768ae38c2b7c761fdd0c50b1e0d1d9505fd4327597e58b4a97255ccbd4c55f7707514582af35694
7
+ data.tar.gz: e81791809179424aba13bbdc0bbabc7bf58d8e126f9d367a783a513af62f2263cbf6f611ef2cb2e76768fec4a429137d19d20054c88babd05335df3cad1062ae
data/README.md CHANGED
@@ -59,7 +59,7 @@ app = Ez::Settings::Interface.define :app do # :app - interface name
59
59
  end
60
60
 
61
61
  # If you want to see all power of the engine, add this showcase:
62
- group :showcase do
62
+ group :showcase, on_change: ->(changes) { YourHandler.call(changes) } do
63
63
  key :string, default: -> { 'simple string' }
64
64
  key :bool, type: :boolean, default: -> { true }
65
65
  key :integer, type: :integer, default: -> { 777 }
@@ -67,6 +67,8 @@ app = Ez::Settings::Interface.define :app do # :app - interface name
67
67
  key :not_validate, required: false, presence: false
68
68
  key :not_for_ui, required: false, ui: false
69
69
  end
70
+ # Group could have:
71
+ # :on_change - closure to call after the group is changed
70
72
  # Keys could have:
71
73
  # :type (string by default), now ez-settings supports only: string, boolean, integer and select
72
74
  # :default value (as callable objects)
@@ -36,8 +36,8 @@ module Ez::Settings
36
36
  self.instance_eval(&block)
37
37
  end
38
38
 
39
- def group(name, &block)
40
- find_or_initialize_group(name, &block)
39
+ def group(name, options = {}, &block)
40
+ find_or_initialize_group(name, options, &block)
41
41
  end
42
42
 
43
43
  def keys
@@ -46,13 +46,13 @@ module Ez::Settings
46
46
 
47
47
  private
48
48
 
49
- def find_or_initialize_group(name, &block)
49
+ def find_or_initialize_group(name, options = {}, &block)
50
50
  existing_group = groups.find { |g| g.name == name }
51
51
 
52
52
  if existing_group
53
53
  existing_group.instance_eval(&block)
54
54
  else
55
- add_group(Group.new(name, self.name, &block))
55
+ add_group(Group.new(name, self.name, options, &block))
56
56
  end
57
57
  end
58
58
 
@@ -29,7 +29,7 @@ module Ez::Settings
29
29
  end
30
30
 
31
31
  def store(backend)
32
- Ez::Settings::Store.new(self, backend)
32
+ Ez::Settings::Store.new(self, backend, options)
33
33
  end
34
34
 
35
35
  private
@@ -6,12 +6,13 @@ module Ez::Settings
6
6
  class Store
7
7
  delegate :keys, to: :group
8
8
 
9
- attr_reader :group, :errors, :backend
9
+ attr_reader :group, :errors, :backend, :on_change
10
10
 
11
- def initialize(group, backend)
12
- @group = group
13
- @errors = ActiveModel::Errors.new(self)
14
- @backend = backend
11
+ def initialize(group, backend, options = {})
12
+ @group = group
13
+ @errors = ActiveModel::Errors.new(self)
14
+ @backend = backend
15
+ @on_change = options.fetch(:on_change, nil)
15
16
 
16
17
  define_accessors
17
18
 
@@ -40,6 +41,8 @@ module Ez::Settings
40
41
  validate
41
42
  return self unless errors.empty?
42
43
 
44
+ on_change.call(changes(params)) if on_change
45
+
43
46
  backend.write(schema)
44
47
 
45
48
  self
@@ -76,5 +79,16 @@ module Ez::Settings
76
79
  end
77
80
  end
78
81
  end
82
+
83
+ def changes(params)
84
+ new_data =
85
+ if params.is_a?(Hash)
86
+ params.symbolize_keys
87
+ elsif params.is_a?(ActionController::Parameters)
88
+ params.permit(keys.map(&:name)).to_h.symbolize_keys
89
+ end
90
+
91
+ new_data.reject { |k, v| data[k] == v }
92
+ end
79
93
  end
80
94
  end
@@ -1,5 +1,5 @@
1
1
  module Ez
2
2
  module Settings
3
- VERSION = '0.2.1'
3
+ VERSION = '0.2.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ez-settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volodya Sveredyuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-18 00:00:00.000000000 Z
11
+ date: 2018-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ez-core