smerge 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 26dd9d275e84f9b95f4bdcf5d40a83b874435371
4
+ data.tar.gz: 89d15861537a97ab06da286556bbd44dde35a4a7
5
+ SHA512:
6
+ metadata.gz: 56c5f01a5934eef211a6ca13a5ff63a931ebd172aa817d15881722840a19fc91c4f382867e618c2ff640f140dd5eabd833418c8ba0d531bbee295cda56fbb1d2
7
+ data.tar.gz: ab7bd25ce1152fdb64209153ce9f276e7f965424fae52c454ba25c0c9ff272dfb6f45475c0f832635b6cb404d428b6d81bcd721d8ece423294fa828895986c31
@@ -0,0 +1,11 @@
1
+ require 'smerge/models/rule'
2
+
3
+ module Smerge
4
+ class Config
5
+ def rule(left, right, &block)
6
+ rules << Rule.new(left, right, block)
7
+ end
8
+
9
+ def rules; @rules ||= [] end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ module Smerge
2
+ class Engine
3
+ def initialize(rules)
4
+ @rules = rules
5
+ end
6
+
7
+ def call(left, right)
8
+ @rules.each do |rule|
9
+ next unless rule.match(left, right)
10
+ return rule.execute(left, right)
11
+ end
12
+
13
+ nil
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ require 'smerge'
2
+
3
+ class Object
4
+ def smerge(obj)
5
+ Smerge.engine.call(self, obj)
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ require 'smerge'
2
+
3
+ module Smerge
4
+ module Local
5
+ refine Object do
6
+ def smerge(obj)
7
+ Smerge.engine.call(self, obj)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Smerge
2
+ module Matchers
3
+ refine Class do
4
+ def match(obj); obj.kind_of? self end
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Smerge
2
+ module Matchers
3
+ refine Object do
4
+ def match(obj); self == obj end
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ require 'smerge/matchers/class'
2
+ require 'smerge/matchers/object'
3
+
4
+ module Smerge
5
+ class Rule
6
+ using Matchers
7
+
8
+ attr_accessor :left
9
+ attr_accessor :right
10
+ attr_accessor :block
11
+
12
+ def initialize(left, right, block)
13
+ self.left = left
14
+ self.right = right
15
+ self.block = block
16
+ end
17
+
18
+ def match(left, right)
19
+ self.left.match(left) &&
20
+ self.right.match(right)
21
+ end
22
+
23
+ def execute(left, right)
24
+ block.call(left, right)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ require 'smerge'
2
+ using Smerge::Local
3
+
4
+ Smerge.setup do |c|
5
+ c.rule Object, Object do |left, right|
6
+ right
7
+ end
8
+
9
+ c.rule Object, nil do |left, right|
10
+ left
11
+ end
12
+
13
+ c.rule Array, Array do |left, right|
14
+ (left + right).uniq
15
+ end
16
+
17
+ c.rule Hash, Hash do |left, right|
18
+ keys = left.keys + right.keys
19
+
20
+ keys.reduce({}) do |result, key|
21
+ l, r = left[key], right[key]
22
+ result[key] = l.smerge(r); result
23
+ end
24
+ end
25
+ end
data/lib/smerge.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'smerge/config'
2
+ require 'smerge/engine'
3
+
4
+ module Smerge
5
+ def self.setup; yield config end
6
+
7
+ def self.config
8
+ @config ||= Config.new
9
+ end
10
+
11
+ def self.engine
12
+ Engine.new(config.rules.reverse)
13
+ end
14
+ end
15
+
16
+ require 'smerge/imports/local'
17
+ require 'smerge/setup'
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smerge
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Thorner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Advanced merge for hashes and other Ruby objects.
14
+ email: benthorner@outlook.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/smerge.rb
20
+ - lib/smerge/config.rb
21
+ - lib/smerge/engine.rb
22
+ - lib/smerge/imports/global.rb
23
+ - lib/smerge/imports/local.rb
24
+ - lib/smerge/matchers/class.rb
25
+ - lib/smerge/matchers/object.rb
26
+ - lib/smerge/models/rule.rb
27
+ - lib/smerge/setup.rb
28
+ homepage: https://github.com/benthorner/smerge
29
+ licenses:
30
+ - GNU GPL v2.0
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.2
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.4.5
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Advanced merge for hashes and other Ruby objects
52
+ test_files: []