ez-settings 0.2.2 → 0.2.3

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: 66561fe692db0aab77ef9f297dc74afb7f14bc2d86d99c0b64799fc37623e7a4
4
- data.tar.gz: 11799222bb160c1b7cdd19fe2dea75436cadb3feee340b0544d0b926183ed2b6
3
+ metadata.gz: 1f55bd8dda018755a4db33fd94e9232669074afdc7af2029de8e5728e92ff80a
4
+ data.tar.gz: c0f36d36db6adeae4880bdb7ef4598c1c9ba4bbaadb1f8bd9ca52c0e27d868f4
5
5
  SHA512:
6
- metadata.gz: 315ef0bcaa2828278dd3395747920a38da72a664108722847768ae38c2b7c761fdd0c50b1e0d1d9505fd4327597e58b4a97255ccbd4c55f7707514582af35694
7
- data.tar.gz: e81791809179424aba13bbdc0bbabc7bf58d8e126f9d367a783a513af62f2263cbf6f611ef2cb2e76768fec4a429137d19d20054c88babd05335df3cad1062ae
6
+ metadata.gz: 5a50de648b5c2d1ce8ef1773f9fee0f9967352ef91775d76535fc2e652b358110d90c0fa6d9e2f802df5f17d09c4b4fbd0ad35af785926b26cb31a47d2358d3f
7
+ data.tar.gz: 73a21c257b75f87b41708795dc57a1b2167d4c3f37412448afa79d4b5d55933ba0438a6f200d4cce1d003376d05b8ae4490e25f7b734283be42355cedbc1b425
data/README.md CHANGED
@@ -175,13 +175,25 @@ GET/PUT: /admin/settings/showcase - showcase group settings
175
175
  ```
176
176
 
177
177
  ### Settings accessors
178
- In your app you could access to settings values through:
178
+ To access your settings use template `Ez::Settings['interface', 'group', 'key']`.
179
+
180
+ Access interface:
181
+ ```ruby
182
+ Ez::Settings[:app]
183
+ #=> <Ez::Settings::Interface:0x007ff1ea7d3168 @groups=[...], @keys=[], @name=:app>
184
+ ```
185
+
186
+ Access group:
179
187
  ```ruby
180
- Ez::Settings[:all, :general, :app_title]
181
- Ez::Settings[:all, :admin, :app_title]
182
- Ez::Settings[:all, :showcase, :select]
188
+ Ez::Settings[:app, :general]
189
+ #=> <Ez::Settings::Interface::Group:0x007ff1ea7d2f88 @interface=:app, @keys=[...], @name=:general, @options={}>
190
+ ```
191
+
192
+ Access setting value:
193
+ ```ruby
194
+ Ez::Settings[:app, :general, :app_title]
195
+ # => 'Main app title'
183
196
  ```
184
- Just use template: `Ez::Settings['interface', 'group', 'key']`
185
197
 
186
198
  In the case of missing interface/group/key you will receive one of exceptions:
187
199
  ```
@@ -189,6 +201,7 @@ NotRegistredInterfaceError
189
201
  NotRegistredGroupError
190
202
  NotRegistredKeyError
191
203
  ```
204
+
192
205
  Be careful ;)
193
206
 
194
207
  ### I18n
@@ -4,28 +4,62 @@ module Ez::Settings
4
4
  NotRegistredKeyError = Class.new(StandardError)
5
5
 
6
6
  module Accessors
7
- def [](interface_name, group_name, key_name)
8
- interface = Ez::Registry.data(:settings_interfaces).find do |interface|
9
- interface.name == interface_name.to_sym
10
- end
11
-
12
- unless interface
13
- raise NotRegistredInterfaceError, "Interface #{interface_name} is not registred!"
14
- end
7
+ def [](interface_name, group_name = nil, key_name = nil)
8
+ interface = interface(interface_name)
15
9
 
16
- group = interface.groups.find { |g| g.name == group_name.to_sym }
10
+ not_registered_interface!(interface_name) unless interface
17
11
 
18
- unless group
19
- raise NotRegistredGroupError, "Group #{group_name} is not registred for #{interface_name} interface"
20
- end
12
+ if only_interface?(interface, group_name, key_name)
13
+ interface
14
+ else
15
+ group = group(interface, group_name)
21
16
 
22
- store = Ez::Settings::Store.new(group, interface.config.backend)
17
+ not_registered_group!(interface_name, group_name) unless group
23
18
 
24
- begin
25
- store.send(key_name.to_sym)
26
- rescue NoMethodError
27
- raise NotRegistredKeyError, "Key #{key_name} is not registred for #{interface_name} interface, #{group_name} group"
19
+ if interface_and_group?(interface, group_name, key_name)
20
+ group(interface, group_name)
21
+ else
22
+ begin
23
+ store(interface, group).send(key_name.to_sym)
24
+ rescue NoMethodError
25
+ not_registered_key!(interface_name, group_name, key_name)
26
+ end
27
+ end
28
28
  end
29
29
  end
30
30
  end
31
+
32
+ private_class_method def self.interface(interface_name)
33
+ Ez::Registry.data(:settings_interfaces).find do |interface|
34
+ interface.name == interface_name.to_sym
35
+ end
36
+ end
37
+
38
+ private_class_method def self.group(interface, group_name)
39
+ interface.groups.find { |g| g.name == group_name.to_sym }
40
+ end
41
+
42
+ private_class_method def self.store(interface, group)
43
+ Ez::Settings::Store.new(group, interface.config.backend)
44
+ end
45
+
46
+ private_class_method def self.only_interface?(interface, group_name, key_name)
47
+ interface && group_name.nil? && key_name.nil?
48
+ end
49
+
50
+ private_class_method def self.interface_and_group?(interface, group_name, key_name)
51
+ interface && group_name && key_name.nil?
52
+ end
53
+
54
+ private_class_method def self.not_registered_interface!(interface_name)
55
+ raise NotRegistredInterfaceError, "Interface #{interface_name} is not registred!"
56
+ end
57
+
58
+ private_class_method def self.not_registered_group!(interface_name, group_name)
59
+ raise NotRegistredGroupError, "Group #{group_name} is not registred for #{interface_name} interface"
60
+ end
61
+
62
+ private_class_method def self.not_registered_key!(interface_name, group_name, key_name)
63
+ raise NotRegistredKeyError, "Key #{key_name} is not registred for #{interface_name} interface, #{group_name} group"
64
+ end
31
65
  end
@@ -1,5 +1,5 @@
1
1
  module Ez
2
2
  module Settings
3
- VERSION = '0.2.2'
3
+ VERSION = '0.2.3'
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.2
4
+ version: 0.2.3
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-26 00:00:00.000000000 Z
11
+ date: 2018-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ez-core