changit 0.2.0 → 0.2.2

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: d9bf79ac4c29a50011f960a3087ae267347014be
4
- data.tar.gz: 43c7ec5b8cfad72701db0b6f04b12c3b2b1e1e51
3
+ metadata.gz: 372f010fb7fc1efe5ddaaba7a9ff3a7f7e498d0d
4
+ data.tar.gz: ec1d0128d5a9918ad1edb7605bcce4c523582c13
5
5
  SHA512:
6
- metadata.gz: 342448f498b87b8a5834782e0ceaa217e5d8ec58d023539094f858093b5f143477da2abfe2d67590ad194fcd627ac23d4e93400b7fb6deab097cf3b80e5b4b51
7
- data.tar.gz: a6a1a5af52b4df8c79419b0bf03d02ba80648155f5c6e3cec0d6ac835fb4a24c6323533d916007fd2b3e5b4514b18c65ff5c21fea0d195b175a8e408c3d922fa
6
+ metadata.gz: 19fe3ed541cc2c97c30f59593d256aad6b5d665d5ea961d9039c1f30df7c3a6180ef5a7a2bc853c7145051fec50813c2e8a80980be7acd855ee84ec3472a5aa3
7
+ data.tar.gz: af5b46096c6b14c07bdf661799687887327702e3678e6fe7b86a1b1bd86d94b9250687ec232e6c36a79439b737a059d8ea4a7051eff1bbd3bdb755e32be264c8
@@ -0,0 +1,38 @@
1
+ module Changit
2
+ class ConfigMerger
3
+ def initialize(src_config, target_config)
4
+ @src = src_config
5
+ @target = target_config
6
+ end
7
+
8
+ # comapre and merge src_config into target_config into a lexer
9
+ def merge
10
+ src_lexer = Lexer.new(@src)
11
+ src_token_hash = src_lexer.token_hash
12
+
13
+ target_lexer = Lexer.new(@target)
14
+ target_token_hash = target_lexer.token_hash
15
+
16
+ if target_token_hash.empty?
17
+ target_lexer = src_lexer
18
+ else
19
+ target_token_hash.each do |k, v|
20
+ if src_token_hash[k]
21
+ target_token_hash[k].merge!(src_token_hash[k])
22
+ end
23
+ end
24
+
25
+ config_difference = hash_difference(src_token_hash, target_token_hash)
26
+ target_token_hash.merge!(config_difference)
27
+ target_lexer.reconstruct_tokens_from_hash!
28
+ end
29
+
30
+ target_lexer
31
+ end
32
+
33
+ # return the difference between two hashes
34
+ def hash_difference(src_token_hash, target_token_hash)
35
+ src_token_hash.dup.delete_if { |k, _| target_token_hash.key?(k) }
36
+ end
37
+ end
38
+ end
@@ -6,36 +6,12 @@ module Changit
6
6
  @config = config
7
7
  @config_files = config_files
8
8
  end
9
-
10
- def write
11
- config_lexer = Lexer.new(@config)
12
- config_token_hash = config_lexer.token_hash
13
9
 
10
+ def write
14
11
  @config_files.each do |path|
15
- file_lexer = Lexer.new(File.read(path))
16
- file_token_hash = file_lexer.token_hash
17
-
18
- unless file_token_hash.empty?
19
- file_token_hash.each do |k, v|
20
- if config_token_hash[k]
21
- # calling `sort!` changes the order of the key_value pairs
22
- config_token_hash[k].sort!
23
- file_token_hash[k].sort!
24
-
25
- config_token_hash[k].each_with_index do |e, i|
26
- if v.include?(e)
27
- # update it
28
- file_token_hash[k][i].rhs = e.rhs
29
- else
30
- # push it
31
- file_token_hash[k].push(e)
32
- end
33
- end
34
- end
35
- end
36
- end
12
+ merged_config = ConfigMerger.new(@config, File.read(path)).merge
37
13
 
38
- File.open(path, 'w') { |f| f.write(config_lexer.to_s); f.close }
14
+ File.open(path, 'w') { |f| f.write(merged_config.to_s); f.close }
39
15
  end
40
16
  end
41
17
  end
@@ -1,8 +1,6 @@
1
1
  module Changit
2
2
  class Lexer
3
3
  class KeyValueToken
4
- include Comparable
5
-
6
4
  attr_reader :lhs # left-hand side
7
5
  attr_accessor :rhs # right-hand side
8
6
 
@@ -13,10 +11,6 @@ module Changit
13
11
  def to_s
14
12
  "\t#{@lhs} = #{@rhs}\n"
15
13
  end
16
-
17
- def <=>(another)
18
- self.lhs <=> another.lhs
19
- end
20
14
  end
21
15
  end
22
16
  end
@@ -1,7 +1,7 @@
1
1
  module Changit
2
2
  class Lexer
3
3
  class SectionToken
4
- attr_reader :value
4
+ attr_accessor :section
5
5
 
6
6
  def initialize(section)
7
7
  @section = section
data/lib/changit/lexer.rb CHANGED
@@ -17,6 +17,16 @@ module Changit
17
17
  @tokens.map(&:to_s).join
18
18
  end
19
19
 
20
+ def reconstruct_tokens_from_hash!
21
+ tokens = []
22
+ @token_hash.each do |section, key_value|
23
+ tokens << SectionToken.new(section)
24
+ key_value.each { |lhs, rhs| tokens << KeyValueToken.new("#{lhs} = #{rhs}") }
25
+ end
26
+
27
+ @tokens = tokens
28
+ end
29
+
20
30
  private
21
31
 
22
32
  def tokenize
@@ -42,9 +52,9 @@ module Changit
42
52
 
43
53
  @tokens.map do |token|
44
54
  if token.class == SectionToken
45
- token_hash[token] = []
55
+ token_hash[token.section] = {}
46
56
  else
47
- token_hash[token_hash.to_a.last[0]].push(token)
57
+ token_hash[token_hash.to_a.last[0]][token.lhs] = token.rhs
48
58
  end
49
59
  end
50
60
 
@@ -1,3 +1,3 @@
1
1
  module Changit
2
- VERSION='0.2.0'
2
+ VERSION='0.2.2'
3
3
  end
data/lib/changit.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  require 'changit/version'
2
- require 'changit/git_dir_finder'
3
- require 'changit/config_reader'
4
2
  require 'changit/lexer/section_token'
5
3
  require 'changit/lexer/key_value_token'
6
4
  require 'changit/lexer'
5
+ require 'changit/git_dir_finder'
6
+ require 'changit/config_merger'
7
+ require 'changit/config_reader'
7
8
  require 'changit/config_writer'
8
9
 
9
10
  module Changit
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: changit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - aaooki
@@ -27,6 +27,7 @@ files:
27
27
  - changit.gemspec
28
28
  - demo.gif
29
29
  - lib/changit.rb
30
+ - lib/changit/config_merger.rb
30
31
  - lib/changit/config_reader.rb
31
32
  - lib/changit/config_writer.rb
32
33
  - lib/changit/git_dir_finder.rb