midwire_common 0.1.11 → 0.1.12

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: b2d9f52a8ea1e5c417dedc8b47d671080df6aacd
4
- data.tar.gz: 74b18476d79fd954b1318402323fe788bbee45da
3
+ metadata.gz: dcbd766fa404dee3f40d4d338117022cfa80fa0f
4
+ data.tar.gz: 18dcf1ba51b11d5ebadf7bd7f81d5d6e1261bdd8
5
5
  SHA512:
6
- metadata.gz: 918873e38253dbfcc7693b30af5af984d88c5042ae4c6f0c27334826512d9865d0c1f080adc39e95a11ca388f60a6a5b3350a4a84abae5c27556bf409e52115b
7
- data.tar.gz: a584a6861730bd4620fb8c431ad127513a05ae40e7b25c74a82f8968bf06a255a38169575f35672d9e9b032b0da40275c4f95d87eb963295126b69b6e88e3bf4
6
+ metadata.gz: c1dbf997be279cb4cc0756f500b05d6319a275871633f3d177aaa7520862f03ff6d64c2a17f70cc6b98588c93761597165999eaac9340a3081330a98547afeda
7
+ data.tar.gz: 94b5bf3534b408b44e791f3bad888cbded6af424f1c98ea4c26bb4676df24c733d092b6386c2a5d613b97bfd7de44811407d80e3c82657b901280f576451a004
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ *0.1.12* (December 22, 2014)
2
+
3
+ * Add diff, apply_diff and apply_diff! instance methods for Hash
4
+
1
5
  *0.1.11* (November 21, 2014)
2
6
 
3
7
  * Fixed a bad release in 0.1.10
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Midwire Common Gem
2
2
 
3
- **Version: 0.1.11**
3
+ **Version: 0.1.12**
4
4
 
5
5
  A handy Ruby library for Midwire development
6
6
 
@@ -6,6 +6,6 @@ require 'midwire_common/fixnum'
6
6
  require 'midwire_common/float'
7
7
  require 'midwire_common/hash'
8
8
  require 'midwire_common/string'
9
- require 'midwire_common/system_command'
9
+ # require 'midwire_common/system_command'
10
10
  require 'midwire_common/time'
11
11
  require 'midwire_common/time_tool'
@@ -6,6 +6,53 @@ class Hash
6
6
  end
7
7
  end
8
8
 
9
+ def diff(other)
10
+ (self.keys + other.keys).uniq.inject({}) do |memo, key|
11
+ unless self[key] == other[key]
12
+ if self[key].kind_of?(Hash) && other[key].kind_of?(Hash)
13
+ memo[key] = self[key].diff(other[key])
14
+ else
15
+ memo[key] = [self[key], other[key]]
16
+ end
17
+ end
18
+ memo
19
+ end
20
+ end
21
+
22
+ def apply_diff!(changes, direction = :right)
23
+ path = [[self, changes]]
24
+ pos, local_changes = path.pop
25
+ while local_changes
26
+ local_changes.each_pair {|key, change|
27
+ if change.kind_of?(Array)
28
+ pos[key] = (direction == :right) ? change[1] : change[0]
29
+ else
30
+ path.push([pos[key], change])
31
+ end
32
+ }
33
+ pos, local_changes = path.pop
34
+ end
35
+ self
36
+ end
37
+
38
+ def apply_diff(changes, direction = :right)
39
+ cloned = self.clone
40
+ path = [[cloned, changes]]
41
+ pos, local_changes = path.pop
42
+ while local_changes
43
+ local_changes.each_pair {|key, change|
44
+ if change.kind_of?(Array)
45
+ pos[key] = (direction == :right) ? change[1] : change[0]
46
+ else
47
+ pos[key] = pos[key].clone
48
+ path.push([pos[key], change])
49
+ end
50
+ }
51
+ pos, local_changes = path.pop
52
+ end
53
+ cloned
54
+ end
55
+
9
56
  # Usage { :a => 1, :b => 2, :c => 3}.except(:a) -> { :b => 2, :c => 3}
10
57
  def except(*keys)
11
58
  self.reject { |k,v|
@@ -1,6 +1,6 @@
1
1
  original_verbosity = $VERBOSE
2
2
  $VERBOSE = nil
3
3
  module MidwireCommon
4
- VERSION = "0.1.11"
4
+ VERSION = "0.1.12"
5
5
  end
6
6
  $VERBOSE = original_verbosity
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midwire_common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Blackburn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-21 00:00:00.000000000 Z
11
+ date: 2014-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec