trickery 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,18 +3,18 @@ require 'ostruct'
3
3
  module Trickery
4
4
  module DeepOstruct
5
5
 
6
- def deep_ostruct(opts)
7
- OpenStruct.new.tap do |o|
8
- opts.each do |key, value|
9
- o.send key.to_s + '=',
10
- case value
11
- when Hash
6
+ def deep_ostruct(values)
7
+ case values
8
+ when Hash
9
+ OpenStruct.new.tap do |o|
10
+ values.each do |key, value|
11
+ o.send key.to_s + '=',
12
12
  deep_ostruct(value)
13
- when Array
14
- value.map { |v| deep_ostruct(v) }
15
- else value
16
- end
13
+ end
17
14
  end
15
+ when Array
16
+ values.map { |v| deep_ostruct(v) }
17
+ else values
18
18
  end
19
19
  end
20
20
 
@@ -0,0 +1,10 @@
1
+ class ::Hash
2
+ def deep_merge(*hashes)
3
+ merger = proc do |key, v1, v2|
4
+ Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
5
+ end
6
+ hashes.inject(self) do |result, other|
7
+ result.merge(other, &merger)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,21 @@
1
+ require 'ostruct'
2
+
3
+ class ::Hash
4
+
5
+ def deep_ostruct(values=nil)
6
+ values ||= self
7
+ case values
8
+ when Hash
9
+ OpenStruct.new.tap do |o|
10
+ values.each do |key, value|
11
+ o.send key.to_s + '=',
12
+ deep_ostruct(value)
13
+ end
14
+ end
15
+ when Array
16
+ values.map { |v| deep_ostruct(v) }
17
+ else values
18
+ end
19
+ end
20
+
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Trickery
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ require File.expand_path(File.join(%w(.. .. .. .. lib trickery hash deep_merge)), __FILE__)
4
+
5
+ describe Hash do
6
+
7
+ it 'responds to deep_merge' do
8
+ assert { {}.respond_to?(:deep_merge) }
9
+ end
10
+
11
+ it 'merges deeply' do
12
+ assert { { a: { b: 0 } } == { a: { b: 1 } }.deep_merge({ a: { b: 0 } }) }
13
+ end
14
+
15
+ it 'merges multiple' do
16
+ a = { a: { b: 0, c: 3 } }
17
+ b = { a: { b: 1, c: 4 } }
18
+ c = { a: { b: 2 } }
19
+ assert { { a: { b: 2, c: 4 } } == a.deep_merge(b, c) }
20
+ end
21
+
22
+ it 'merges nil' do
23
+ a = { a: { b: 0 } }
24
+ b = { a: { b: nil } }
25
+ assert { { a: { b: nil } } == a.deep_merge(b) }
26
+ end
27
+
28
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trickery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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: 2015-06-08 00:00:00.000000000 Z
12
+ date: 2015-10-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -107,9 +107,12 @@ files:
107
107
  - lib/trickery/cmd_runner.rb
108
108
  - lib/trickery/config.rb
109
109
  - lib/trickery/deep_ostruct.rb
110
+ - lib/trickery/hash/deep_merge.rb
111
+ - lib/trickery/hash/deep_ostruct.rb
110
112
  - lib/trickery/version.rb
111
113
  - spec/spec_helper.rb
112
114
  - spec/trickery/deep_ostruct_spec.rb
115
+ - spec/trickery/hash/deep_merge_spec.rb
113
116
  - trickery.gemspec
114
117
  homepage: https://github.com/branch14/trickery
115
118
  licenses:
@@ -126,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
129
  version: '0'
127
130
  segments:
128
131
  - 0
129
- hash: -4414155239521418392
132
+ hash: 3366191683089287710
130
133
  required_rubygems_version: !ruby/object:Gem::Requirement
131
134
  none: false
132
135
  requirements:
@@ -135,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
138
  version: '0'
136
139
  segments:
137
140
  - 0
138
- hash: -4414155239521418392
141
+ hash: 3366191683089287710
139
142
  requirements: []
140
143
  rubyforge_project:
141
144
  rubygems_version: 1.8.23
@@ -145,3 +148,4 @@ summary: Reusable ruby trickery.
145
148
  test_files:
146
149
  - spec/spec_helper.rb
147
150
  - spec/trickery/deep_ostruct_spec.rb
151
+ - spec/trickery/hash/deep_merge_spec.rb