mry 0.48.1.2 → 0.48.1.3

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: 5b48a06c411f93ea868e4e94aeaec3f076286b05
4
- data.tar.gz: c6a065c83a75edcae15ec6f6c59ac0a8943616c4
3
+ metadata.gz: d415c40a4beeefad120b1b85cb2ced332c3ebb68
4
+ data.tar.gz: 8f55ba2c7857a4616021713e8791adf244e2f6c5
5
5
  SHA512:
6
- metadata.gz: d7cd89207a7efcb82fc6bc572064850173425aaf018ad8cd5bd7b5e13bae4b4742cf9da5ee8fecac94feb749b2e4598e25cb9c49855f3c2fcd25b7f73ce73f1f
7
- data.tar.gz: c635916d93fb0b6e3c74b7027f16dcb0e8e17bcbcc2680a970b2e634cc0dc0de8e37b52042cb2f8f519cd44030ae60e12f8ef607cc45ac55974c59d0e3120479
6
+ metadata.gz: 7deb835c75e56e136700d681fc5ef5eb19c631bf802e2d9ec93dbb4673b1b34a5dc4b9bbb408875ce329dac685934825a73a34f6d7c3a178dcb44bc458cd5482
7
+ data.tar.gz: 3f7cc6aab1e6f9ac26d7f4efcc31f9c0960766dd36ca2c04aa9d92c619b3e7bba48aa13cf2075c6cd9e1ff6c5f70719d547d330c39db08d099cc85436578731a
data/CHANGELOG.md CHANGED
@@ -2,11 +2,17 @@
2
2
 
3
3
  ## master (unreleased)
4
4
 
5
+ ## 0.48.1.3 (2017-05-15)
6
+
7
+ ### New Features
8
+
9
+ - [#10](https://github.com/pocke/mry/pull/10): Support downgrading RuboCop version. ([@pocke][])
10
+
5
11
  ## 0.48.1.2 (2017-05-5)
6
12
 
7
13
  ### Bug fixes
8
14
 
9
- - [#7](https://github.com/pocke/mry/issues/7): Prevent mry command from breaking without `--target` option ([@pocke][])
15
+ - [#7](https://github.com/pocke/mry/issues/7): Prevent mry command from breaking without `--target` option. ([@pocke][])
10
16
 
11
17
 
12
18
 
data/lib/mry/runner.rb CHANGED
@@ -8,23 +8,31 @@ module Mry
8
8
 
9
9
  class << self
10
10
  def run(files, target)
11
- rewriters =
12
- if target == :master
13
- (Rewriters.values + [Rewriter_Master]).reverse
14
- else
15
- Rewriters
16
- .select{|key, _value| target >= key}
17
- .values.reverse
18
- end
11
+ rewriters, reverse_rewriters = *rewriters(target)
19
12
 
20
13
  files.each do |file|
21
14
  yaml = File.read(file)
15
+ reverse_rewriters.each do |r|
16
+ yaml = r.new(yaml, reverse: true).rewrite
17
+ end
22
18
  rewriters.each do |r|
23
19
  yaml = r.new(yaml).rewrite
24
20
  end
25
21
  File.write(file, yaml)
26
22
  end
27
23
  end
24
+
25
+
26
+ private
27
+
28
+ # @return [Array<Array<Rewriter>>]
29
+ def rewriters(target)
30
+ return [(Rewriters.values + [Rewriter_Master]).reverse, []] if target == :master
31
+
32
+ Rewriters
33
+ .partition{|key, _value| target >= key}
34
+ .map{|rewriters| rewriters.map{|k, v| v}.reverse}
35
+ end
28
36
  end
29
37
  end
30
38
  end
data/lib/mry/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mry
2
- VERSION = "0.48.1.2"
2
+ VERSION = "0.48.1.3"
3
3
  end
@@ -1,15 +1,33 @@
1
1
  module YAMLRewriter
2
+ # Usage:
3
+ # class MyRewriter < YAMLRewriter::Rewriter
4
+ # define_rule ['foo' => 'bar']
5
+ # define_rule ['one', 'two', 'threeee' => 'three']
6
+ # end
7
+ #
8
+ # MyRewriter.new('foo: baz').rewrite # => 'bar: baz'
9
+ #
10
+ # rewriter = MyRewriter.new(<<-END)
11
+ # one:
12
+ # two:
13
+ # threeee: 123
14
+ # END
15
+ # rewriter.rewrite # => one:
16
+ # two:
17
+ # three: 123
2
18
  class Rewriter
3
19
  using ScalarWithMark
4
20
  # @param yaml [String]
5
- def initialize(yaml)
21
+ # @param reverse [true|false]
22
+ def initialize(yaml, reverse: false)
6
23
  @yaml = yaml.dup
24
+ @reverse = reverse
7
25
  @offset = 0
8
26
  end
9
27
 
10
28
  # @param rule [Array]
11
29
  def self.define_rule(rule)
12
- rules.push(rule)
30
+ rules.push(Rule.new(rule))
13
31
  end
14
32
 
15
33
  def self.rules
@@ -42,20 +60,15 @@ module YAMLRewriter
42
60
 
43
61
  def rewrite_yaml(path, key)
44
62
  self.class.rules.each do |rule|
45
- next unless match_rule(rule, path)
63
+ next unless rule.match?(path, reverse: @reverse)
46
64
 
47
65
  index = key.mark.index + @offset
48
66
  prev = key.value
49
- new = rule.last[prev]
67
+ new = rule.replacement(prev, reverse: @reverse)
50
68
  start_index = @yaml.rindex(prev, index)
51
69
  @yaml[start_index..(start_index+prev.size-1)] = new
52
70
  @offset += new.size-prev.size
53
71
  end
54
72
  end
55
-
56
- def match_rule(rule, path)
57
- rule_path = rule[0..-2] + [rule.last.keys.first]
58
- rule_path == (path[(path.size-rule.size)..-1])
59
- end
60
73
  end
61
74
  end
@@ -0,0 +1,19 @@
1
+ module YAMLRewriter
2
+ class Rule
3
+ def initialize(rule)
4
+ @rule = rule
5
+ end
6
+
7
+ def match?(path, reverse:)
8
+ rule_path = @rule[0..-2] +
9
+ (reverse ? [@rule.last.values.first] : [@rule.last.keys.first])
10
+ rule_path == (path[(path.size-@rule.size)..-1])
11
+ end
12
+
13
+ def replacement(key, reverse:)
14
+ reverse ?
15
+ @rule.last.invert[key] :
16
+ @rule.last[key]
17
+ end
18
+ end
19
+ end
data/lib/yaml_rewriter.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'psych'
2
+ require 'yaml_rewriter/rule'
2
3
  require 'yaml_rewriter/scalar_with_mark'
3
4
  require 'yaml_rewriter/mark_handler'
4
5
  require 'yaml_rewriter/parser'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.48.1.2
4
+ version: 0.48.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masataka Kuwabara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-05 00:00:00.000000000 Z
11
+ date: 2017-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -139,8 +139,8 @@ files:
139
139
  - lib/yaml_rewriter/mark_handler.rb
140
140
  - lib/yaml_rewriter/parser.rb
141
141
  - lib/yaml_rewriter/rewriter.rb
142
+ - lib/yaml_rewriter/rule.rb
142
143
  - lib/yaml_rewriter/scalar_with_mark.rb
143
- - lib/yaml_rewriter/traverser.rb
144
144
  - mry.gemspec
145
145
  homepage: https://github.com/pocke/mry
146
146
  licenses:
@@ -1,7 +0,0 @@
1
- module YAMLRewriter
2
- class Traverser
3
- def traverse(tree, path = [])
4
-
5
- end
6
- end
7
- end