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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 34ce3aa791e2071425e8e55d2d9e4e693de1cd51
4
- data.tar.gz: 49cbb9368ac5709dbddb7b0113c55fae2d65225b
3
+ metadata.gz: 8d70db0e0baed5e010cb81899fe4dec58953c0c9
4
+ data.tar.gz: 6cf9942db0b8e386869b86d57151b381b4adc28b
5
5
  SHA512:
6
- metadata.gz: e01a623f73b52b470ecd41e3a7f4332112c99d2cf9df2937601f462367387dc978a670c513d12f7b20f1b9985032d7934bbbaa54ee5c6cf01d63d468d1e08205
7
- data.tar.gz: 904a023b25fc9e1ffbf85dd657eb865ca4bdf8660b94c656420ebadbb091b5491a1c575efd0b536a2ee94f70d3fa4086fc432739dc73b3340c1ae6fc23e4a67a
6
+ metadata.gz: edfa8ac6772add3bfd15628eebb62f3ba5474b8b4a5aa510188579b4fdc99074ec38b2df0324caafb70906f52d3312aa2eaa5b6ff1a5d3932b152d20efbabc01
7
+ data.tar.gz: 50eaf5dc9deecb6ab9b572e7e71fcbf473dc15c23eab0431f94646c3470b7138e7f2e57f9010cdb55a87b93fba57496ab0cf889adc950aa4f4354b40b4576c87
@@ -1,9 +1,11 @@
1
1
  class Settingson::Config
2
2
  attr_accessor :cache
3
3
  attr_accessor :debug
4
+ attr_accessor :trace
4
5
 
5
6
  def initialize
6
7
  @debug = false
8
+ @trace = 0
7
9
  @cache = OpenStruct.new(expires_in: 60.seconds,
8
10
  race_condition_ttl: 10.seconds,
9
11
  enabled: true,
@@ -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("caller: #{caller(1).first}")
33
- __debug("caller: #{caller(2).first}")
34
- __debug("caller: #{caller(3).first}")
35
- __rescue_action(symbol.to_s, args)
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#%-24s: %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 __rescue_action(key, value)
49
- __debug("key: #{key}:#{key.class} value: #{value}:#{value.class} " +
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 '#{value.first}'")
54
- __get( __reference(value.first) )
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 '#{value.first}' '#{value.last}'")
57
- __set( __reference(value.first), value.last )
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}' value '#{value.first}'")
60
- __set($1, value.first)
61
- else # returns values or self
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 __reference(key)
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
- class_name = __underscore(key.class)
101
- ref_id = __reference_id(key)
102
- "#{class_name}_#{ref_id || 'new'}"
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'
@@ -1,3 +1,3 @@
1
1
  module Settingson
2
- VERSION = "1.7.3"
2
+ VERSION = "1.7.5"
3
3
  end
@@ -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.3
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-22 00:00:00.000000000 Z
11
+ date: 2016-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails