settingson 1.1.17 → 1.2.1

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: ae00ca72c0fde891e6b25543506539202312b58a
4
- data.tar.gz: 465f4a243cce987cd428454ff7d3184ea31adc95
3
+ metadata.gz: aec49dc850a44eb78c90e349546810759bfa4ef4
4
+ data.tar.gz: de34ec83bacbd07f598ca0523cb99c0ca3e4c4b2
5
5
  SHA512:
6
- metadata.gz: 436eed50f9a58f16271f91e4decd0c14f7b29f8ce03ddb6505df872d08d03a9f723bcbf0be0adffaf46403ea993639ad063644d60c15e356e61b487476f90d09
7
- data.tar.gz: 645a3d0003cbd4d4d6252f9483587ffd477178b6b138359fc083d48aef52163add2a0b048325687ff0a685e9b5d4f02528b40fd179e907349ea1cc570441db77
6
+ metadata.gz: 55f3b02731359aeab98f25d92bd3dc47e789c2ecea198678f0829206d2c227848d7e943e730f55a50c5a7b2278b39c19f38c408e452f0470a6bed5160b542ecd
7
+ data.tar.gz: d68db1634e51420cf1bdefbc22357cfb7bf006803264875dc820637886066d39036a0acb20d8c384f9cc21c9902ba1449406e451eece85ff3bf00027330966c2
data/README.md CHANGED
@@ -28,6 +28,12 @@ Settings.array # => [1, 2, 3, 4, 5]
28
28
  # Array of hashes
29
29
  Settings.array.of.hashes = [ { hello: :world}, {'glad' => :to}, {see: 'you'} ]
30
30
  Settings.array.of.hashes # => [{:hello=>:world}, {"glad"=>:to}, {:see=>"you"}]
31
+
32
+ Settings.server[:host, :port, :role] = [ '127.0.0.1', 3000, :web ]
33
+ Settings.server[:host, :port, :role] # => ["127.0.0.1", 3000, :web]
34
+ Settings.server.host # => '127.0.0.1'
35
+ Settings.server.port # => 3000
36
+ Settings.server.role # => :web
31
37
  ```
32
38
 
33
39
  ## Installation
@@ -2,15 +2,65 @@ module Settingson::Base
2
2
 
3
3
  extend ActiveSupport::Concern
4
4
 
5
- module ClassMethods
5
+ included do
6
+ attr_accessor :settingson
7
+ end
8
+
9
+ def method_missing(symbol, *args)
10
+ super
11
+ rescue NoMethodError
12
+
13
+ case symbol.to_s
14
+ when /(.+)=/ # setter
15
+
16
+ @settingson = "#{@settingson}.#{$1}"
17
+
18
+ if record = self.class.find_by(name: @settingson)
19
+ record.update(value: args.first.to_yaml)
20
+ else
21
+ self.class.create(name: @settingson, value: args.first.to_yaml)
22
+ end
23
+
24
+ else # getter
6
25
 
7
- def method_missing(string, *args)
8
- if result = find_by(name: string)
9
- YAML.load result.value
26
+ @settingson += ".#{symbol.to_s}"
27
+ if record = self.class.find_by(name: @settingson)
28
+ YAML.load(record.value)
10
29
  else
11
- ::Settingson::Store.new(self, string, *args)
30
+ self
12
31
  end
32
+
13
33
  end
34
+ end
35
+
36
+ module ClassMethods
37
+
38
+ def method_missing(symbol, *args)
39
+ super
40
+ rescue NoMethodError
41
+
42
+ case symbol.to_s
43
+ when /(.+)=/ # setter
44
+
45
+ @settingson = $1
46
+
47
+ if record = find_by(name: @settingson)
48
+ record.update(value: args.first.to_yaml)
49
+ else
50
+ create(name: @settingson, value: args.first.to_yaml, settingson: @settingson)
51
+ end
52
+
53
+ else # getter
54
+
55
+ if record = find_by(name: symbol.to_s)
56
+ YAML.load record.value
57
+ else
58
+ new(settingson: symbol.to_s)
59
+ end
60
+
61
+ end
62
+ end
63
+
14
64
 
15
65
  end
16
66
 
@@ -1,89 +1,4 @@
1
- class Settingson::Store
1
+ module Settingson::Store
2
2
 
3
- attr_accessor :name, :value
4
3
 
5
- extend ActiveModel::Naming
6
-
7
- def initialize(klass, string, *args)
8
- @klass = klass
9
- @value = parse(string, args.first)
10
- end
11
-
12
- def to_s
13
- @value
14
- end
15
-
16
- def to_ary
17
- [ @value ]
18
- end
19
-
20
- def nil?
21
- true
22
- end
23
-
24
- def [](*args)
25
- return nil if args.nil?
26
- if args.count == 1
27
- result = @klass.find_by(name: "#{@name}.#{args.first}").try(:value)
28
- result.nil? ? nil : YAML.load(result)
29
- else
30
- args.map do |subname|
31
- result = @klass.find_by(name: "#{@name}.#{subname}").try(:value)
32
- result.nil? ? nil : YAML.load(result)
33
- end
34
- end
35
- end
36
-
37
- def []=(*args)
38
- return nil if args.nil?
39
- if args.count == 2
40
- if record = @klass.find_by(name: "#{@name}.#{args.first}")
41
- record.update(value: args.last.to_yaml)
42
- else
43
- @klass.create(name: "#{@name}.#{args.first}", value: args.last.to_yaml)
44
- end
45
- args.last
46
- elsif args.count > 2
47
- values = args.pop
48
- args.map do |subname|
49
- value = values.shift
50
- if record = @klass.find_by(name: "#{@name}.#{subname}")
51
- record.update(value: value.to_yaml)
52
- else
53
- @klass.create(name: "#{@name}.#{subname}", value: value.to_yaml)
54
- end
55
- value
56
- end
57
- end
58
- end
59
-
60
- def method_missing(string, *args)
61
- @value = parse(string, args.first)
62
- end
63
-
64
- protected
65
- def parse(string, value)
66
- case string.to_s
67
- when /(.+)=/
68
- @name = @name.nil? ? $1 : @name + ".#{$1}"
69
- if record = @klass.find_by(name: @name)
70
- if value.nil?
71
- record.destroy
72
- else
73
- record.update(value: value.to_yaml)
74
- end
75
- value
76
- else
77
- @klass.create(name: @name, value: value.to_yaml) unless value.nil?
78
- value
79
- end
80
- else
81
- @name = @name.nil? ? string.to_s : @name + ".#{string.to_s}"
82
- if result = @klass.find_by(name: @name)
83
- YAML.load result.value
84
- else
85
- self
86
- end
87
- end
88
- end
89
4
  end
@@ -1,3 +1,3 @@
1
1
  module Settingson
2
- VERSION = "1.1.17"
2
+ VERSION = "1.2.1"
3
3
  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.1.17
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - dan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-03 00:00:00.000000000 Z
11
+ date: 2014-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails