settingson 1.7.3 → 1.7.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/settingson/config.rb +2 -0
- data/lib/settingson/store.rb +44 -20
- data/lib/settingson/version.rb +1 -1
- data/spec/models/settings_spec.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d70db0e0baed5e010cb81899fe4dec58953c0c9
|
4
|
+
data.tar.gz: 6cf9942db0b8e386869b86d57151b381b4adc28b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edfa8ac6772add3bfd15628eebb62f3ba5474b8b4a5aa510188579b4fdc99074ec38b2df0324caafb70906f52d3312aa2eaa5b6ff1a5d3932b152d20efbabc01
|
7
|
+
data.tar.gz: 50eaf5dc9deecb6ab9b572e7e71fcbf473dc15c23eab0431f94646c3470b7138e7f2e57f9010cdb55a87b93fba57496ab0cf889adc950aa4f4354b40b4576c87
|
data/lib/settingson/config.rb
CHANGED
data/lib/settingson/store.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
class Settingson::Store
|
2
2
|
|
3
|
+
# extend ActiveModel::Naming
|
4
|
+
# include ActiveModel::Conversion
|
5
|
+
#
|
3
6
|
def initialize(klass:, path: nil)
|
4
7
|
@__klass = klass
|
5
8
|
@__path = path
|
@@ -29,36 +32,52 @@ class Settingson::Store
|
|
29
32
|
alias to_ary to_a
|
30
33
|
|
31
34
|
def method_missing(symbol, *args)
|
32
|
-
__debug
|
33
|
-
__debug("
|
34
|
-
|
35
|
-
|
35
|
+
__debug
|
36
|
+
__debug("from\n\t#{caller[1..@__klass.configure.trace].join("\n\t")}") if
|
37
|
+
@__klass.configure.trace > 0
|
38
|
+
|
39
|
+
__references_action(symbol, *args) or __rescue_action(symbol.to_s, *args)
|
40
|
+
# __rescue_action(symbol.to_s, *args)
|
36
41
|
end # method_missing
|
37
42
|
|
38
43
|
protected
|
39
44
|
# TODO: move all methods to support class
|
40
|
-
def __debug(message)
|
41
|
-
message = sprintf("%s
|
45
|
+
def __debug(message="")
|
46
|
+
message = sprintf("%s#%20s: %s",
|
42
47
|
self.class.name,
|
43
48
|
caller_locations.first.label,
|
44
49
|
message)
|
45
50
|
Rails.logger.debug(message) if @__klass.configure.debug
|
46
51
|
end
|
47
52
|
|
48
|
-
def
|
49
|
-
|
53
|
+
def __references_action(symbol, *args)
|
54
|
+
# Proxy pass only one method
|
55
|
+
# return nil
|
56
|
+
# return nil unless ['model_name', 'to_model'].include?(symbol.to_s)
|
57
|
+
if @__klass and @__klass.respond_to?(symbol)
|
58
|
+
__debug("#{@__klass.to_s} know what to do with #{symbol}")
|
59
|
+
@__klass.send(symbol, *args)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def __rescue_action(key, *args)
|
64
|
+
__debug("key: #{key}:#{key.class} args: #{args}:#{args.class} " +
|
50
65
|
"path: '#{@__path}'")
|
51
66
|
case key
|
52
|
-
when '[]' # object reference
|
53
|
-
__debug("reference '#{
|
54
|
-
__get(
|
67
|
+
when '[]' # object reference[, with :field]
|
68
|
+
__debug("reference '#{args}'")
|
69
|
+
__get( __with_reference(args[0], args[1]) )
|
55
70
|
when '[]=' # object reference setter
|
56
|
-
__debug("reference setter '#{
|
57
|
-
|
71
|
+
__debug("reference setter '#{args}'")
|
72
|
+
if args.size == 3 # [@setting, :key]= form
|
73
|
+
__set( __with_reference(args[0], args[1]), args[2] )
|
74
|
+
else # [@settings]= form
|
75
|
+
__set( __with_reference(args.first), args.last )
|
76
|
+
end
|
58
77
|
when /(.+)=/ # setter
|
59
|
-
__debug("set '#{$1}'
|
60
|
-
__set($1,
|
61
|
-
else # returns
|
78
|
+
__debug("set '#{$1}' args '#{args.first}'")
|
79
|
+
__set($1, args.first)
|
80
|
+
else # returns result or self
|
62
81
|
__debug("get '#{key}'")
|
63
82
|
__get(key)
|
64
83
|
end
|
@@ -90,16 +109,21 @@ class Settingson::Store
|
|
90
109
|
|
91
110
|
# @profile = Profile.first # any ActiveRecord::Base object
|
92
111
|
# Settings[@profile].some.host = 'value'
|
93
|
-
def
|
112
|
+
def __with_reference(key, field=nil)
|
94
113
|
case key
|
95
114
|
when String
|
96
115
|
key
|
97
116
|
when Symbol
|
98
117
|
key.to_s
|
99
118
|
when ActiveRecord::Base
|
100
|
-
|
101
|
-
|
102
|
-
|
119
|
+
@__reference = key
|
120
|
+
if field.nil?
|
121
|
+
class_name = __underscore(key.class)
|
122
|
+
ref_id = __reference_id(key)
|
123
|
+
"#{class_name}_#{ref_id || 'new'}"
|
124
|
+
else
|
125
|
+
key.send(field.to_sym)
|
126
|
+
end
|
103
127
|
else
|
104
128
|
raise ArgumentError.new(
|
105
129
|
'String/Symbol/ActiveRecord::Base variable required'
|
data/lib/settingson/version.rb
CHANGED
@@ -152,6 +152,11 @@ describe Settings do
|
|
152
152
|
Settings[:say] = 'hello'
|
153
153
|
expect( Settings.say ).to eq('hello')
|
154
154
|
end
|
155
|
+
it 'Settings[@setting, :key] form' do
|
156
|
+
setting = Settings.create!(key: 'test_key', value: 'test_value')
|
157
|
+
Settings[setting, :value] = 'hello'
|
158
|
+
expect( Settings.test_value ).to eq('hello')
|
159
|
+
end
|
155
160
|
it 'Settings[:say].hello == Settings.say[:hello]' do
|
156
161
|
Settings[:say].hello = 'hello'
|
157
162
|
expect( Settings.say[:hello] ).to eq('hello')
|
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.7.
|
4
|
+
version: 1.7.5
|
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-
|
11
|
+
date: 2016-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|