settingson 1.6.1 → 1.7.0

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
  SHA1:
3
- metadata.gz: 99560087ec3a6364f4fc18fdd833c43f8038b548
4
- data.tar.gz: db460d9c07b8bdcaa7d3c9e124b5c30dc0d29ff8
3
+ metadata.gz: 35ba5f07c92b264c30cc01ae57b5d27dccd3976e
4
+ data.tar.gz: 5a260c492f899e609313ffff799a3bf772747124
5
5
  SHA512:
6
- metadata.gz: 377401a9ccb1dd80569739525309c6ae224977389bf99790b5803bd7632c2e0cc221162d42d6f5d04dcde9184dba7a3424c315b15c2fc566015fc4e70f86f5d6
7
- data.tar.gz: 2d77b76c3f4080ce229e043d8f58e11fb961224d4abd8846b18cecbc5b79eff9fb822eb13c0343e2d28044b3b304b663e754fe3daf970688870805f22d676a0f
6
+ metadata.gz: 31ee2d331f97bf54a69da8892e32ca847e035443556754238f82df806593213c30e8dcd0f21a5028bbd9ea89401854c43882f759cd0eab9ae903208082b2b769
7
+ data.tar.gz: 292dc86a675178fd6aeba5365851e41bc3f8fc7b49fc09b4530e2578357ceb584ebd5012f7cce7e3b364e7e2e47f14585bb5524d7d639867da9216e85efcbb21
data/README.md CHANGED
@@ -66,15 +66,21 @@ Settings.from_hash({hello: :world})
66
66
  Settings.hello # => :world
67
67
  Settings.hello? # => true
68
68
 
69
- Settings.not_found # => ""
70
- Settings.not_found? # => false
69
+ Settings[:greeting].welcome.message = 'Hello'
70
+ Settings[:greeting].welcome.message # => 'Hello'
71
+ Settings.greeting[:welcome].message # => 'Hello'
72
+ Settings.greeting.welcome[:message] # => 'Hello'
73
+ Settings.greeting.welcome['message'] # => 'Hello'
74
+
75
+ Settings.not_found.to_s # => ""
76
+ Settings.not_found.to_i # => 0
71
77
  Settings.not_found.nil? # => true
72
78
  Settings.not_found.empty? # => true
73
79
  Settings.not_found.blank? # => true
74
80
  Settings.not_found.present? # => false
75
81
 
76
82
  # but
77
- Settings.not_found.class # => Settings(id: integer, key: string, value: text, created_at: datetime, updated_at: datetime)
83
+ Settings.not_found.class # => Settingson::Store
78
84
 
79
85
  Settings.all # =>
80
86
  # [#<Settings id: 1, key: "server.host", value: "127.0.0.1", created_at: "2015-12-08 15:17:56", updated_at: "2015-12-08 15:17:56">,
@@ -109,18 +115,15 @@ end
109
115
  ```
110
116
 
111
117
  ## The initial values
112
- New way:
113
118
  in config/initializers/settingson.rb
114
119
  ```ruby
115
120
  Settings.defaults do |conf|
116
121
  conf.server.smtp.host = 'host'
117
122
  conf.server.smtp.port = 25
118
123
  end
119
- ```
120
124
 
121
- ```ruby
122
- conf.server.smtp.host # => 'host'
123
- conf.server.smtp.port # => 25
125
+ Settings.server.smtp.host # => 'host'
126
+ Settings.server.smtp.port # => 25
124
127
  ```
125
128
 
126
129
  ## Contributing
@@ -53,7 +53,7 @@ module Settingson::Base
53
53
  def method_missing(symbol, *args)
54
54
  super
55
55
  rescue NameError, NoMethodError
56
- Settingson::Store.new(klass: self).send(symbol, args.first)
56
+ Settingson::Store.new(klass: self).send(symbol, *args)
57
57
  end
58
58
 
59
59
  end # module ClassMethods
@@ -6,25 +6,30 @@ class Settingson::Store
6
6
  end
7
7
 
8
8
  def to_s
9
- self.new_record? ? '' : super
9
+ ''
10
10
  end
11
11
 
12
12
  def to_i
13
- self.new_record? ? 0 : super
13
+ 0
14
14
  end
15
15
 
16
16
  def nil?
17
- self.new_record? ? true : super
17
+ true
18
+ end
19
+
20
+ def to_a
21
+ []
18
22
  end
19
23
 
20
24
  alias empty? nil?
25
+ alias to_ary to_a
21
26
 
22
27
  def method_missing(symbol, *args)
23
- __rescue_action(symbol.to_s, args.first)
28
+ __rescue_action(symbol.to_s, args)
24
29
  end # method_missing
25
30
 
26
31
  protected
27
-
32
+ # TODO: move all methods to support class
28
33
  def __debug(message)
29
34
  message = sprintf("%s#%-32s: %s",
30
35
  self.class.name,
@@ -34,12 +39,20 @@ class Settingson::Store
34
39
  end
35
40
 
36
41
  def __rescue_action(key, value)
42
+ __debug("key: #{key}:#{key.class} value: #{value}:#{value.class} " +
43
+ "path: '#{@__path}'")
37
44
  case key
45
+ when '[]' # object reference
46
+ __debug("reference '#{value.first}'")
47
+ __get( __reference(value.first) )
48
+ when '[]=' # object reference setter
49
+ __debug("reference setter '#{value.first}' '#{value.last}'")
50
+ __set( __reference(value.first), value.last )
38
51
  when /(.+)=/ # setter
39
- __debug("set '#{$1}' value '#{value}' path '#{@__path}'")
40
- __set($1, value)
41
- else # returns values or self
42
- __debug("get '#{key}' value '#{value}' path '#{@__path}'")
52
+ __debug("set '#{$1}' value '#{value.first}'")
53
+ __set($1, value.first)
54
+ else # returns values or self
55
+ __debug("get '#{key}'")
43
56
  __get(key)
44
57
  end
45
58
  end
@@ -68,6 +81,39 @@ class Settingson::Store
68
81
  end
69
82
  end
70
83
 
84
+ # @profile = Profile.first # any ActiveRecord::Base object
85
+ # Settings[@profile].some.host = 'value'
86
+ def __reference(key)
87
+ case key
88
+ when String
89
+ key
90
+ when Symbol
91
+ key.to_s
92
+ when ActiveRecord::Base
93
+ class_name = __underscore(key.class)
94
+ ref_id = __reference_id(key)
95
+ "#{class_name}_#{ref_id || 'new'}"
96
+ else
97
+ raise ArgumentError.new(
98
+ 'String/Symbol/ActiveRecord::Base variable required'
99
+ )
100
+ end
101
+ end
102
+
103
+ def __underscore(camel_cased_word)
104
+ word = camel_cased_word.to_s.dup
105
+ word.gsub!(/::/, '_')
106
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
107
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
108
+ word.tr!('-', '_')
109
+ word.downcase!
110
+ word
111
+ end
112
+
113
+ def __reference_id(key)
114
+ key.try(:to_key).try(:join, '_') || key.id
115
+ end
116
+
71
117
  def __update_search_path(key)
72
118
  @__path = [@__path, key].compact.join('.')
73
119
  end
File without changes
@@ -1,3 +1,3 @@
1
1
  module Settingson
2
- VERSION = "1.6.1"
2
+ VERSION = "1.7.0"
3
3
  end
@@ -42,8 +42,14 @@ describe Settings do
42
42
  Settings.number = 100
43
43
  expect( Settings.number ).to eq(100)
44
44
  end
45
- it 'returns 0 when key not found' do
45
+ it 'pass simple checks' do
46
+ expect( Settings.not_found.to_s ).to eq("")
46
47
  expect( Settings.not_found.to_i ).to eq(0)
48
+ expect( Settings.not_found.to_a ).to eq([])
49
+ expect( Settings.not_found.nil? ).to eq(true)
50
+ expect( Settings.not_found.empty? ).to eq(true)
51
+ expect( Settings.not_found.blank? ).to eq(true)
52
+ expect( Settings.not_found.present? ).to eq(false)
47
53
  end
48
54
  it 'returns same String' do
49
55
  word = Faker::Lorem.word
@@ -125,4 +131,41 @@ describe Settings do
125
131
  end
126
132
  end
127
133
 
134
+ describe '::[]' do
135
+ it 'raises error with unknown class' do
136
+ expect{ Settings[Time.new] }.to raise_error(ArgumentError)
137
+ end
138
+ it 'returns Settingson::Store instance' do
139
+ expect( Settings['hello'] ).to be_a(Settingson::Store)
140
+ end
141
+ it 'returns instance with search path "hello" for simple' do
142
+ settings = Settings['hello']
143
+ expect( settings.instance_variable_get(:@__path) ).to eq('hello')
144
+ end
145
+ it 'returns instance with search path "hello.message" for complex #1' do
146
+ settings = Settings[:hello].message
147
+ expect( settings.instance_variable_get(:@__path) ).to eq('hello.message')
148
+ end
149
+
150
+ it 'Settings[:say] form' do
151
+ Settings[:say] = 'hello'
152
+ expect( Settings.say ).to eq('hello')
153
+ end
154
+ it 'Settings[:say].hello == Settings.say[:hello]' do
155
+ Settings[:say].hello = 'hello'
156
+ expect( Settings.say[:hello] ).to eq('hello')
157
+ end
158
+ it 'works with complex check' do
159
+ Settings[:greeting].welcome.message = 'Hello'
160
+ expect( Settings[:greeting].welcome.message ).to eq('Hello')
161
+ expect( Settings.greeting[:welcome].message ).to eq('Hello')
162
+ expect( Settings.greeting.welcome[:message] ).to eq('Hello')
163
+ expect( Settings.greeting.welcome['message'] ).to eq('Hello')
164
+ end
165
+ it 'returns instance with search path "settings_1"' do
166
+ s = Settings.create(key: 'hello', value: 'world')
167
+ expect( Settings[s].instance_variable_get(:@__path) ).to eq("settings_1")
168
+ end
169
+ end
170
+
128
171
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: settingson
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - dan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-19 00:00:00.000000000 Z
11
+ date: 2016-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -179,6 +179,7 @@ files:
179
179
  - lib/settingson/config.rb
180
180
  - lib/settingson/engine.rb
181
181
  - lib/settingson/store.rb
182
+ - lib/settingson/utils.rb
182
183
  - lib/settingson/version.rb
183
184
  - settingson.gemspec
184
185
  - spec/dummy/README.rdoc