chozo 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/chozo.rb CHANGED
@@ -5,6 +5,15 @@ module Chozo
5
5
  autoload :Platform, 'chozo/platform'
6
6
  autoload :RubyEngine, 'chozo/ruby_engine'
7
7
  autoload :VariaModel, 'chozo/varia_model'
8
+
9
+ class << self
10
+ # Path to the root directory of the Chozo application
11
+ #
12
+ # @return [Pathname]
13
+ def app_root
14
+ @app_root ||= Pathname.new(File.expand_path('../', File.dirname(__FILE__)))
15
+ end
16
+ end
8
17
  end
9
18
 
10
19
  require 'chozo/core_ext'
@@ -22,23 +22,9 @@ module Chozo
22
22
  mass_assign(attributes)
23
23
  end
24
24
 
25
- def [](key)
26
- self.attributes[key]
27
- end
28
-
29
- def []=(key, value)
30
- self.attributes[key] = value
31
- end
32
-
33
25
  def to_hash
34
26
  self.attributes.to_hash.deep_symbolize_keys
35
27
  end
36
-
37
- protected
38
-
39
- def mass_assign(new_attributes = {})
40
- attributes.deep_merge!(new_attributes)
41
- end
42
28
  end
43
29
  end
44
30
  end
@@ -1,5 +1,4 @@
1
1
  require 'chozo/errors'
2
- require 'multi_json'
3
2
 
4
3
  module Chozo
5
4
  module Config
@@ -27,23 +26,13 @@ module Chozo
27
26
  end
28
27
  end
29
28
 
30
- # @param (see MultiJson.encode) options
31
- #
32
- # @return [String]
33
- def to_json(options = {})
34
- MultiJson.encode(self.attributes, options)
35
- end
36
- alias_method :as_json, :to_json
37
-
38
- # @param (see MultiJson.decode) json
39
- # @param (see MultiJson.decode) options
29
+ # @see {VariaModel#from_json}
40
30
  #
41
31
  # @raise [Chozo::Errors::InvalidConfig]
42
32
  #
43
- # @return [~Chozo::Config::JSON]
44
- def from_json(json, options = {})
45
- mass_assign(MultiJson.decode(json, options))
46
- self
33
+ # @return [Chozo::Config::JSON]
34
+ def from_json(*args)
35
+ super
47
36
  rescue MultiJson::DecodeError => e
48
37
  raise Chozo::Errors::InvalidConfig, e
49
38
  end
@@ -77,6 +77,8 @@ class Hash
77
77
  #
78
78
  # @return [Object, nil]
79
79
  def dig(path)
80
+ return nil unless path.present?
81
+
80
82
  parts = path.split('.', 2)
81
83
  match = (self[parts[0].to_s] || self[parts[0].to_sym])
82
84
  if !parts[1] or match.nil?
@@ -95,7 +97,7 @@ class Hash
95
97
  #
96
98
  # @return [Array<String>]
97
99
  def dotted_paths(source = self, acc = Array.new, namespace = Array.new)
98
- if source.is_a?(Hash)
100
+ if source.is_a?(Hash) && !source.empty?
99
101
  source.each do |key, value|
100
102
  branch = namespace.dup
101
103
  branch << key
@@ -1,3 +1,4 @@
1
+ require 'multi_json'
1
2
  require 'chozo/core_ext'
2
3
  require 'chozo/hashie_ext'
3
4
 
@@ -170,18 +171,60 @@ module Chozo
170
171
  @errors ||= HashWithIndifferentAccess.new
171
172
  end
172
173
 
174
+ def mass_assign(new_attrs = {})
175
+ attributes.dotted_paths.each do |dotted_path|
176
+ value = new_attrs.dig(dotted_path)
177
+ next if value.nil?
178
+
179
+ set_attribute(dotted_path, value)
180
+ end
181
+ end
182
+ alias_method :attributes=, :mass_assign
183
+
173
184
  # @param [#to_s] key
174
185
  #
175
186
  # @return [Object]
176
187
  def get_attribute(key)
177
188
  self.attributes.dig(key.to_s)
178
189
  end
190
+ alias_method :[], :get_attribute
179
191
 
180
192
  # @param [#to_s] key
181
193
  # @param [Object] value
182
194
  def set_attribute(key, value)
183
195
  self.attributes.deep_merge!(attributes.class.from_dotted_path(key.to_s, value))
184
196
  end
197
+ alias_method :[]=, :set_attribute
198
+
199
+ # @param [#to_hash] hash
200
+ #
201
+ # @return [self]
202
+ def from_hash(hash)
203
+ mass_assign(hash.to_hash)
204
+ self
205
+ end
206
+
207
+ # @param [String] data
208
+ #
209
+ # @return [self]
210
+ def from_json(data)
211
+ mass_assign(MultiJson.decode(data))
212
+ self
213
+ end
214
+
215
+ # @return [Hash]
216
+ def to_hash
217
+ self.attributes
218
+ end
219
+
220
+ # @option options [Boolean] :symbolize_keys
221
+ # @option options [Class, Symbol, String] :adapter
222
+ #
223
+ # @return [String]
224
+ def to_json(options = {})
225
+ MultiJson.encode(self.attributes, options)
226
+ end
227
+ alias_method :as_json, :to_json
185
228
 
186
229
  protected
187
230
 
data/lib/chozo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Chozo
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -32,6 +32,10 @@ if mri? && ENV['CI'] != 'true'
32
32
 
33
33
  Spork.each_run do
34
34
  require 'chozo'
35
+
36
+ Dir["#{Chozo.app_root}/lib/chozo/core_ext/**/*.rb"].each do |ext|
37
+ load(ext)
38
+ end
35
39
  end
36
40
  else
37
41
  require 'chozo'
@@ -73,6 +73,10 @@ describe Hash do
73
73
  subject.dig("we.found.something").should eql(:symbol_value)
74
74
  end
75
75
  end
76
+
77
+ it "returns nil if given a blank string" do
78
+ subject.dig("").should be_nil
79
+ end
76
80
  end
77
81
 
78
82
  describe "#dotted_paths" do
@@ -93,6 +97,19 @@ describe Hash do
93
97
  end
94
98
  end
95
99
 
100
+ context "given a hash with empty hashes as values" do
101
+ subject do
102
+ {
103
+ "one" => Hash.new,
104
+ "two" => Hash.new
105
+ }
106
+ end
107
+
108
+ it "returns an array of the top level keys as strings" do
109
+ subject.dotted_paths.should eql(["one", "two"])
110
+ end
111
+ end
112
+
96
113
  context "given a hash with nested keys" do
97
114
  subject do
98
115
  {
@@ -451,4 +451,122 @@ describe Chozo::VariaModel do
451
451
  subject.get_attribute('brooke.costantini').should be_nil
452
452
  end
453
453
  end
454
+
455
+ describe "#mass_assign" do
456
+ subject do
457
+ Class.new do
458
+ include Chozo::VariaModel
459
+
460
+ attribute 'brooke.winsor', type: String, default: 'sister'
461
+ attribute 'jamie.winsor', type: String, default: 'brother'
462
+ attribute 'gizmo', type: String, default: 'dog'
463
+ end.new
464
+ end
465
+
466
+ it "sets the values of all matching defined attributes" do
467
+ new_attrs = {
468
+ brooke: {
469
+ winsor: "other"
470
+ },
471
+ jamie: {
472
+ winsor: "other_two"
473
+ }
474
+ }
475
+
476
+ subject.mass_assign(new_attrs)
477
+ subject.brooke.winsor.should eql("other")
478
+ subject.jamie.winsor.should eql("other_two")
479
+ end
480
+
481
+ it "leaves the values of untouched attributes" do
482
+ new_attrs = {
483
+ brooke: {
484
+ winsor: "other"
485
+ },
486
+ jamie: {
487
+ winsor: "other_two"
488
+ }
489
+ }
490
+
491
+ subject.mass_assign(new_attrs)
492
+ subject.gizmo.should eql("dog")
493
+ end
494
+
495
+ it "ignores values which are not defined attributes" do
496
+ new_attrs = {
497
+ undefined_attribute: "value"
498
+ }
499
+
500
+ subject.mass_assign(new_attrs)
501
+ subject.should_not respond_to(:undefined_attribute)
502
+ end
503
+ end
504
+
505
+ describe "#from_json" do
506
+ subject do
507
+ Class.new do
508
+ include Chozo::VariaModel
509
+
510
+ attribute 'first_name', type: String
511
+ attribute 'nick', type: String
512
+ end.new
513
+ end
514
+
515
+ it "returns self" do
516
+ subject.from_json(MultiJson.encode(first_name: "jamie", nick: "reset")).should be_a(described_class)
517
+ end
518
+
519
+ it "updates self from JSON data" do
520
+ subject.from_json(MultiJson.encode(first_name: "jamie", nick: "reset"))
521
+
522
+ subject.first_name.should eql("jamie")
523
+ subject.nick.should eql("reset")
524
+ end
525
+ end
526
+
527
+ describe "#from_hash" do
528
+ subject do
529
+ Class.new do
530
+ include Chozo::VariaModel
531
+
532
+ attribute 'first_name', type: String
533
+ attribute 'nick', type: String
534
+ end.new
535
+ end
536
+
537
+ it "returns self" do
538
+ subject.from_hash(first_name: "jamie", nick: "reset").should be_a(described_class)
539
+ end
540
+
541
+ it "updates and returns self from a Hash" do
542
+ subject.from_hash(first_name: "jamie", nick: "reset")
543
+
544
+ subject.first_name.should eql("jamie")
545
+ subject.nick.should eql("reset")
546
+ end
547
+ end
548
+
549
+ describe "#to_json" do
550
+ subject do
551
+ Class.new do
552
+ include Chozo::VariaModel
553
+
554
+ attribute 'first_name', type: String
555
+ attribute 'nick', type: String
556
+ end.new
557
+ end
558
+
559
+ it "returns a JSON string containin the serialized attributes" do
560
+ subject.first_name = "brooke"
561
+ subject.nick = "leblanc"
562
+
563
+ subject.to_json.should eql(MultiJson.encode(first_name: "brooke", nick: "leblanc"))
564
+ end
565
+ end
566
+
567
+ describe "#to_hash" do
568
+ it "returns the attributes" do
569
+ subject.to_hash.should eql(subject.attributes)
570
+ end
571
+ end
454
572
  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.3.0
4
+ version: 0.4.0
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-12-27 00:00:00.000000000 Z
12
+ date: 2013-01-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -100,7 +100,7 @@ files:
100
100
  - spec/support/matchers/each.rb
101
101
  - spec/unit/chozo/config/abstract_spec.rb
102
102
  - spec/unit/chozo/config/json_spec.rb
103
- - spec/unit/chozo/core_ext/hash_spec.rb
103
+ - spec/unit/chozo/core_ext/hash/dotted_paths_spec.rb
104
104
  - spec/unit/chozo/platform_spec.rb
105
105
  - spec/unit/chozo/ruby_engine_spec.rb
106
106
  - spec/unit/chozo/varia_model_spec.rb
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  version: '0'
126
126
  segments:
127
127
  - 0
128
- hash: 3239723575146630686
128
+ hash: -4510203401888880792
129
129
  requirements: []
130
130
  rubyforge_project:
131
131
  rubygems_version: 1.8.23
@@ -139,7 +139,7 @@ test_files:
139
139
  - spec/support/matchers/each.rb
140
140
  - spec/unit/chozo/config/abstract_spec.rb
141
141
  - spec/unit/chozo/config/json_spec.rb
142
- - spec/unit/chozo/core_ext/hash_spec.rb
142
+ - spec/unit/chozo/core_ext/hash/dotted_paths_spec.rb
143
143
  - spec/unit/chozo/platform_spec.rb
144
144
  - spec/unit/chozo/ruby_engine_spec.rb
145
145
  - spec/unit/chozo/varia_model_spec.rb