chozo 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -85,4 +85,26 @@ class Hash
85
85
  match.dig(parts[1])
86
86
  end
87
87
  end
88
+
89
+ # Returns an array of dotted paths from the keys, values of this Hash. Values which are
90
+ # nested Hashes will also recurred into and their paths will be added properly.
91
+ #
92
+ # @param [Hash] source
93
+ # @param [Array] acc
94
+ # @param [Array] namespace
95
+ #
96
+ # @return [Array<String>]
97
+ def dotted_paths(source = self, acc = Array.new, namespace = Array.new)
98
+ if source.is_a?(Hash)
99
+ source.each do |key, value|
100
+ branch = namespace.dup
101
+ branch << key
102
+ dotted_paths(value, acc, branch)
103
+ end
104
+ else
105
+ acc << namespace.join('.')
106
+ end
107
+
108
+ acc
109
+ end
88
110
  end
@@ -168,6 +168,19 @@ module Chozo
168
168
  @errors ||= HashWithIndifferentAccess.new
169
169
  end
170
170
 
171
+ # @param [#to_s] key
172
+ #
173
+ # @return [Object]
174
+ def get_attribute(key)
175
+ self.attributes.dig(key.to_s)
176
+ end
177
+
178
+ # @param [#to_s] key
179
+ # @param [Object] value
180
+ def set_attribute(key, value)
181
+ self.attributes.deep_merge!(attributes.class.from_dotted_path(key.to_s, value))
182
+ end
183
+
171
184
  protected
172
185
 
173
186
  # @param [String] attribute
@@ -1,3 +1,3 @@
1
1
  module Chozo
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -74,4 +74,45 @@ describe Hash do
74
74
  end
75
75
  end
76
76
  end
77
+
78
+ describe "#dotted_paths" do
79
+ it "returns an array" do
80
+ subject.dotted_paths.should be_a(Array)
81
+ end
82
+
83
+ context "given a hash with only top level keys" do
84
+ subject do
85
+ {
86
+ "one" => "val",
87
+ "two" => "val"
88
+ }
89
+ end
90
+
91
+ it "returns an array of the top level keys as strings" do
92
+ subject.dotted_paths.should eql(["one", "two"])
93
+ end
94
+ end
95
+
96
+ context "given a hash with nested keys" do
97
+ subject do
98
+ {
99
+ "one" => {
100
+ "nested" => {
101
+ "attribute" => "hello"
102
+ }
103
+ },
104
+ "two" => {
105
+ "nested" => {
106
+ "attribute" => "other_hello",
107
+ "other_attr" => "world"
108
+ }
109
+ }
110
+ }
111
+ end
112
+
113
+ it "returns an array of dotted paths including the nested Hash keys" do
114
+ subject.dotted_paths.should eql(["one.nested.attribute", "two.nested.attribute", "two.nested.other_attr"])
115
+ end
116
+ end
117
+ end
77
118
  end
@@ -392,4 +392,45 @@ describe Chozo::VariaModel do
392
392
  end
393
393
  end
394
394
  end
395
+
396
+ describe "#set_attribute" do
397
+ subject do
398
+ Class.new do
399
+ include Chozo::VariaModel
400
+
401
+ attribute 'brooke.winsor', type: String, default: 'sister'
402
+ attribute 'brooke.costantini', type: String, default: 'sister'
403
+ end.new
404
+ end
405
+
406
+ it "sets the value of the given attribute" do
407
+ subject.set_attribute('brooke.winsor', 'rhode island')
408
+
409
+ subject.brooke.winsor.should eql('rhode island')
410
+ end
411
+
412
+ it "does not disturb the other attributes" do
413
+ subject.set_attribute('brooke.winsor', 'rhode island')
414
+
415
+ subject.brooke.costantini.should eql('sister')
416
+ end
417
+ end
418
+
419
+ describe "#get_attribute" do
420
+ subject do
421
+ Class.new do
422
+ include Chozo::VariaModel
423
+
424
+ attribute 'brooke.winsor', type: String, default: 'sister'
425
+ end.new
426
+ end
427
+
428
+ it "returns the value of the given dotted path" do
429
+ subject.get_attribute('brooke.winsor').should eql('sister')
430
+ end
431
+
432
+ it "returns nil if the dotted path matches no attributes" do
433
+ subject.get_attribute('brooke.costantini').should be_nil
434
+ end
435
+ end
395
436
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chozo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-31 00:00:00.000000000 Z
12
+ date: 2012-11-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
135
  version: '0'
136
136
  segments:
137
137
  - 0
138
- hash: 3079662827452475923
138
+ hash: 534389849781461156
139
139
  requirements: []
140
140
  rubyforge_project:
141
141
  rubygems_version: 1.8.23