psychgus 1.2.0 → 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,77 +1,71 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  #--
5
5
  # This file is part of Psychgus.
6
- # Copyright (c) 2019 Jonathan Bradley Whited (@esotericpig)
7
- #
8
- # Psychgus is free software: you can redistribute it and/or modify
9
- # it under the terms of the GNU Lesser General Public License as published by
10
- # the Free Software Foundation, either version 3 of the License, or
11
- # (at your option) any later version.
12
- #
13
- # Psychgus is distributed in the hope that it will be useful,
14
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- # GNU Lesser General Public License for more details.
17
- #
18
- # You should have received a copy of the GNU Lesser General Public License
19
- # along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2019-2021 Jonathan Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
20
9
  #++
21
10
 
22
11
 
12
+ require 'delegate'
13
+
14
+
23
15
  module Psychgus
24
16
  class SuperSniffer
25
17
  ###
26
18
  # A container for the parent of a Psych::Nodes::Node.
27
- #
19
+ #
28
20
  # A parent is a Mapping, Sequence, or a Key (Scalar) of a Mapping.
29
- #
21
+ #
30
22
  # You can use the getters in this class in {Styler} to filter what to change.
31
- #
23
+ #
32
24
  # If a Node method has not been exposed, you can use {#node}:
33
25
  # if parent.node_of?(:scalar)
34
26
  # parent.value = 'FUBAR'
35
27
  # parent.node.value = 'FUBAR' # Same as above
36
- #
28
+ #
37
29
  # parent.fubar = true # NoMethodError
38
30
  # parent.node.fubar = true # Use some new Psych::Nodes::Node method not in this version
39
31
  # # of Psychgus or that is not exposed by Parent
40
32
  # end
41
- #
42
- # @author Jonathan Bradley Whited (@esotericpig)
33
+ #
34
+ # @author Jonathan Bradley Whited
43
35
  # @since 1.0.0
44
- #
36
+ #
45
37
  # @see SuperSniffer
46
38
  # @see SuperSniffer#start_parent SuperSniffer#start_parent
47
39
  # @see SuperSniffer#end_parent SuperSniffer#end_parent
48
40
  # @see Styler
49
41
  ###
50
- class Parent
42
+ class Parent < SimpleDelegator
51
43
  # Calling the getter is fine; calling the setter is *not* and could cause weird results.
52
- #
44
+ #
53
45
  # @return [Integer] the next child's position
54
46
  attr_accessor :child_position
55
-
47
+
56
48
  # Calling the getter is fine; calling the setter is *not* and could cause weird results.
57
- #
49
+ #
58
50
  # @return [nil,:key,:value] the next child's Mapping type, if {#node} is a Mapping
59
51
  attr_accessor :child_type
60
-
52
+
61
53
  # @return [:noface,Symbol,String] a tag (class name, value) for debugging; also used in {#to_s}
62
54
  attr_reader :debug_tag
63
-
55
+
64
56
  attr_reader :level # @return [Integer] the level of this Node in the YAML
65
57
  attr_reader :node # @return [Psych::Nodes::Node] the Node of this parent
66
58
  attr_reader :position # @return [Integer] the position of this Node in the YAML
67
-
59
+
68
60
  # Initialize this class with parent data.
69
- #
61
+ #
70
62
  # @param sniffer [SuperSniffer] the sniffer that contains this parent (not stored; used for data)
71
63
  # @param node [Psych::Nodes::Node] the node of this parent
72
64
  # @param debug_tag [:noface,Symbol,String] the tag (class name, value) used for debugging and in {#to_s}
73
65
  # @param child_type [nil,:key,:value] the next child's Mapping type, if +node+ is a Mapping
74
66
  def initialize(sniffer,node,debug_tag: nil,child_type: nil)
67
+ super(node)
68
+
75
69
  @child_position = 1
76
70
  @child_type = child_type
77
71
  @debug_tag = debug_tag
@@ -79,158 +73,63 @@ module Psychgus
79
73
  @node = node
80
74
  @position = sniffer.position
81
75
  end
82
-
83
- # @see Psych::Nodes::Alias#anchor=
84
- # @see Psych::Nodes::Mapping#anchor=
85
- # @see Psych::Nodes::Scalar#anchor=
86
- # @see Psych::Nodes::Sequence#anchor=
87
- def anchor=(anchor)
88
- @node.anchor = anchor
89
- end
90
-
91
- # @see Psych::Nodes::Scalar#plain=
92
- def plain=(plain)
93
- @node.plain = plain
94
- end
95
-
96
- # @see Psych::Nodes::Scalar#quoted=
97
- def quoted=(quoted)
98
- @node.quoted = quoted
99
- end
100
-
101
- # @see Psych::Nodes::Mapping#style=
102
- # @see Psych::Nodes::Scalar#style=
103
- # @see Psych::Nodes::Sequence#style=
104
- def style=(style)
105
- @node.style = style
106
- end
107
-
108
- # @see Psych::Nodes::Node#tag=
109
- def tag=(tag)
110
- @node.tag = tag
111
- end
112
-
113
- # @see Psych::Nodes::Scalar#value=
114
- def value=(value)
115
- @node.value = value
116
- end
117
-
118
- # @see Psych::Nodes::Alias#anchor
119
- # @see Psych::Nodes::Mapping#anchor
120
- # @see Psych::Nodes::Scalar#anchor
121
- # @see Psych::Nodes::Sequence#anchor
122
- def anchor()
123
- return @node.anchor
76
+
77
+ # @api private
78
+ def __getobj__
79
+ return @node
124
80
  end
125
-
81
+
126
82
  # Check if the children of this parent are keys to a Mapping.
127
- #
83
+ #
128
84
  # @return [true,false] whether the children are keys to a Mapping
129
- #
85
+ #
130
86
  # @since 1.2.0
131
- def child_key?()
87
+ def child_key?
132
88
  return @child_type == :key
133
89
  end
134
-
90
+
135
91
  # Check if the children of this parent are values to a Mapping (i.e., values to a key).
136
- #
92
+ #
137
93
  # @return [true,false] whether the children are values to a Mapping (i.e., values to a key)
138
- #
94
+ #
139
95
  # @since 1.2.0
140
- def child_value?()
96
+ def child_value?
141
97
  return @child_type == :value
142
98
  end
143
-
144
- # @see Psych::Nodes::Stream#encoding
145
- def encoding()
146
- return @node.encoding
147
- end
148
-
149
- # @see Psych::Nodes::Node#end_column
150
- def end_column()
151
- return @node.end_column
152
- end
153
-
154
- # @see Psych::Nodes::Node#end_line
155
- def end_line()
156
- return @node.end_line
157
- end
158
-
99
+
159
100
  # @see Psych::Nodes::Document#implicit
160
101
  # @see Psych::Nodes::Mapping#implicit
161
102
  # @see Psych::Nodes::Sequence#implicit
162
- def implicit?()
103
+ def implicit?
163
104
  return @node.implicit
164
105
  end
165
-
106
+
166
107
  # @see Psych::Nodes::Document#implicit_end
167
- def implicit_end?()
108
+ def implicit_end?
168
109
  return @node.implicit_end
169
110
  end
170
-
111
+
171
112
  # (see Ext::NodeExt#node_of?)
172
113
  def node_of?(*names)
173
114
  return @node.node_of?(*names)
174
115
  end
175
-
116
+
176
117
  # @see Psych::Nodes::Scalar#plain
177
- def plain?()
118
+ def plain?
178
119
  return @node.plain
179
120
  end
180
-
121
+
181
122
  # @see Psych::Nodes::Scalar#quoted
182
- def quoted?()
123
+ def quoted?
183
124
  return @node.quoted
184
125
  end
185
-
186
- # @see Psych::Nodes::Node#start_column
187
- def start_column()
188
- return @node.start_column
189
- end
190
-
191
- # @see Psych::Nodes::Node#start_line
192
- def start_line()
193
- return @node.start_line
194
- end
195
-
196
- # @see Psych::Nodes::Mapping#style
197
- # @see Psych::Nodes::Scalar#style
198
- # @see Psych::Nodes::Sequence#style
199
- def style()
200
- return @node.style
201
- end
202
-
203
- # @see Psych::Nodes::Node#tag
204
- def tag()
205
- return @node.tag
206
- end
207
-
208
- # @see Psych::Nodes::Document#tag_directives
209
- def tag_directives()
210
- return @node.tag_directives
211
- end
212
-
213
- # @see Psych::Nodes::Scalar#value
214
- def value()
215
- return @node.value
216
- end
217
-
218
- # @see Psych::Nodes::Document#version
219
- def version()
220
- return @node.version
221
- end
222
-
126
+
223
127
  # @note If this method is modified, then tests will fail
224
- #
128
+ #
225
129
  # @return [String] a String representation of this class for debugging and testing
226
- def to_s()
130
+ def to_s
227
131
  return "<#{@debug_tag}:(#{@level}:#{@position}):#{@child_type}:(:#{@child_position})>"
228
132
  end
229
-
230
- alias_method :implicit,:implicit?
231
- alias_method :implicit_end,:implicit_end?
232
- alias_method :plain,:plain?
233
- alias_method :quoted,:quoted?
234
133
  end
235
134
  end
236
135
  end
@@ -1,27 +1,15 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  #--
6
5
  # This file is part of Psychgus.
7
- # Copyright (c) 2017-2019 Jonathan Bradley Whited (@esotericpig)
8
- #
9
- # Psychgus is free software: you can redistribute it and/or modify
10
- # it under the terms of the GNU Lesser General Public License as published by
11
- # the Free Software Foundation, either version 3 of the License, or
12
- # (at your option) any later version.
13
- #
14
- # Psychgus is distributed in the hope that it will be useful,
15
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
- # GNU Lesser General Public License for more details.
18
- #
19
- # You should have received a copy of the GNU Lesser General Public License
20
- # along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2017-2021 Jonathan Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
21
9
  #++
22
10
 
23
11
 
24
12
  module Psychgus
25
13
  # Version of this gem in "#.#.#" format
26
- VERSION = '1.2.0'
14
+ VERSION = '1.3.4'
27
15
  end
data/psychgus.gemspec CHANGED
@@ -1,67 +1,64 @@
1
1
  # encoding: UTF-8
2
2
  # frozen_string_literal: true
3
3
 
4
- #--
5
- # This file is part of Psychgus.
6
- # Copyright (c) 2017-2019 Jonathan Bradley Whited (@esotericpig)
7
- #
8
- # Psychgus is free software: you can redistribute it and/or modify
9
- # it under the terms of the GNU Lesser General Public License as published by
10
- # the Free Software Foundation, either version 3 of the License, or
11
- # (at your option) any later version.
12
- #
13
- # Psychgus is distributed in the hope that it will be useful,
14
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- # GNU Lesser General Public License for more details.
17
- #
18
- # You should have received a copy of the GNU Lesser General Public License
19
- # along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
20
- #++
21
4
 
5
+ require_relative 'lib/psychgus/version'
22
6
 
23
- lib = File.expand_path('../lib',__FILE__)
24
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
25
-
26
- require 'psychgus/version'
27
7
 
28
8
  Gem::Specification.new do |spec|
29
9
  spec.name = 'psychgus'
30
10
  spec.version = Psychgus::VERSION
31
- spec.authors = ['Jonathan Bradley Whited (@esotericpig)']
32
- spec.email = ['bradley@esotericpig.com']
11
+ spec.authors = ['Jonathan Bradley Whited']
12
+ spec.email = ['code@esotericpig.com']
33
13
  spec.licenses = ['LGPL-3.0-or-later']
34
14
  spec.homepage = 'https://github.com/esotericpig/psychgus'
35
- spec.summary = %q(Easily style YAML files using Psych, like Sequence/Mapping Flow style.)
36
- spec.description = %q(Easily style YAML files using Psych, like Sequence/Mapping Flow style.)
37
-
15
+ spec.summary = 'Easily style YAML files using Psych.'
16
+ spec.description = 'Easily style YAML files using Psych, like Sequence/Mapping Flow style.'
17
+
38
18
  spec.metadata = {
19
+ 'homepage_uri' => 'https://github.com/esotericpig/psychgus',
20
+ 'source_code_uri' => 'https://github.com/esotericpig/psychgus',
39
21
  'bug_tracker_uri' => 'https://github.com/esotericpig/psychgus/issues',
40
22
  'changelog_uri' => 'https://github.com/esotericpig/psychgus/blob/master/CHANGELOG.md',
41
23
  'documentation_uri' => 'https://esotericpig.github.io/docs/psychgus/yardoc/index.html',
42
- 'homepage_uri' => 'https://github.com/esotericpig/psychgus',
43
- 'source_code_uri' => 'https://github.com/esotericpig/psychgus'
44
24
  }
45
-
46
- spec.files = Dir.glob(File.join('{lib,test,yard}','**','*.{erb,rb}')) +
47
- %w(
48
- CHANGELOG.md
49
- Gemfile
50
- LICENSE.txt
51
- psychgus.gemspec
52
- Rakefile
53
- README.md
54
- )
55
- spec.require_paths = ['lib']
56
-
25
+
57
26
  spec.required_ruby_version = '>= 2.1.10'
58
-
59
- spec.add_runtime_dependency 'psych','>= 2.0.5'
60
-
61
- spec.add_development_dependency 'bundler' ,'~> 1.16'
62
- spec.add_development_dependency 'minitest' ,'~> 5.11' # For testing
63
- spec.add_development_dependency 'rake' ,'~> 12.3'
64
- spec.add_development_dependency 'rdoc' ,'~> 6.1' # For RDoc for YARD (*.rb)
65
- spec.add_development_dependency 'redcarpet','~> 3.4' # For Markdown for YARD (*.md)
66
- spec.add_development_dependency 'yard' ,'~> 0.9' # For documentation
27
+ spec.require_paths = ['lib']
28
+ spec.bindir = 'bin'
29
+
30
+ spec.files = [
31
+ Dir.glob(File.join("{#{spec.require_paths.join(',')}}",'**','*.{erb,rb}')),
32
+ Dir.glob(File.join(spec.bindir,'*')),
33
+ Dir.glob(File.join('{samples,test,yard}','**','*.{erb,rb}')),
34
+ %W[ Gemfile #{spec.name}.gemspec Rakefile .yardopts ],
35
+ %w[ LICENSE.txt CHANGELOG.md README.md ],
36
+ ].flatten
37
+
38
+ # Test using different Gem versions:
39
+ # GST=1 bundle update && bundle exec rake test_all
40
+ gemspec_test = ENV.fetch('GST','').to_s.strip
41
+ psych_gemv = false
42
+
43
+ if !gemspec_test.empty?
44
+ case gemspec_test
45
+ when '1' then psych_gemv = '~> 3.0'
46
+ end
47
+
48
+ puts 'Using Gem versions:'
49
+ puts " psych: #{psych_gemv.inspect}"
50
+ end
51
+
52
+ # 3.0 is needed for this issue:
53
+ # - https://bugs.ruby-lang.org/issues/13115
54
+ # - https://github.com/ruby/psych/commit/712a65a53f3c15105cd86e8ad3ee3c779050ada4
55
+ spec.add_runtime_dependency 'psych',psych_gemv || '>= 3.0'
56
+
57
+ spec.add_development_dependency 'bundler' ,'~> 2.2'
58
+ spec.add_development_dependency 'minitest' ,'~> 5.14' # For testing
59
+ spec.add_development_dependency 'rake' ,'~> 13.0'
60
+ spec.add_development_dependency 'rdoc' ,'~> 6.3' # For RDoc for YARD (*.rb)
61
+ spec.add_development_dependency 'redcarpet' ,'~> 3.5' # For Markdown for YARD (*.md)
62
+ spec.add_development_dependency 'yard' ,'~> 0.9' # For documentation
63
+ spec.add_development_dependency 'yard_ghurt','~> 1.2' # For YARD GitHub rake tasks
67
64
  end
@@ -1,22 +1,11 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  #--
5
5
  # This file is part of Psychgus.
6
- # Copyright (c) 2019 Jonathan Bradley Whited (@esotericpig)
7
- #
8
- # Psychgus is free software: you can redistribute it and/or modify
9
- # it under the terms of the GNU Lesser General Public License as published by
10
- # the Free Software Foundation, either version 3 of the License, or
11
- # (at your option) any later version.
12
- #
13
- # Psychgus is distributed in the hope that it will be useful,
14
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- # GNU Lesser General Public License for more details.
17
- #
18
- # You should have received a copy of the GNU Lesser General Public License
19
- # along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2019-2021 Jonathan Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
20
9
  #++
21
10
 
22
11
 
@@ -24,23 +13,23 @@ require 'psychgus_tester'
24
13
 
25
14
  class Burger
26
15
  include Psychgus::Blueberry
27
-
16
+
28
17
  attr_accessor :bun
29
18
  attr_accessor :cheese
30
19
  attr_accessor :sauce
31
-
20
+
32
21
  def initialize(sauce,cheese,bun)
33
22
  @bun = bun
34
23
  @cheese = cheese
35
24
  @sauce = sauce
36
25
  end
37
-
26
+
38
27
  def encode_with(coder)
39
28
  coder['Bun'] = @bun
40
29
  coder['Cheese'] = @cheese
41
30
  coder['Sauce'] = @sauce
42
31
  end
43
-
32
+
44
33
  def psychgus_stylers(sniffer)
45
34
  return BurgerStyler.new(sniffer)
46
35
  end
@@ -49,21 +38,21 @@ end
49
38
  class Burgers
50
39
  attr_accessor :burgers
51
40
  attr_accessor :toppings
52
-
53
- def initialize()
41
+
42
+ def initialize
54
43
  @burgers = {
55
44
  'Classic' => Burger.new(['Ketchup','Mustard'],'American','Sesame Seed'),
56
45
  'BBQ' => Burger.new('Honey BBQ','Cheddar','Kaiser'),
57
46
  'Fancy' => Burger.new('Spicy Wasabi','Smoked Gouda','Hawaiian')
58
47
  }
59
-
48
+
60
49
  @toppings = [
61
50
  'Mushrooms',
62
- %w(Lettuce Onions Pickles Tomatoes),
63
- [%w(Ketchup Mustard),%w(Salt Pepper)]
51
+ %w[Lettuce Onions Pickles Tomatoes],
52
+ [%w[Ketchup Mustard],%w[Salt Pepper]]
64
53
  ]
65
54
  end
66
-
55
+
67
56
  def encode_with(coder)
68
57
  coder['Burgers'] = @burgers
69
58
  coder['Toppings'] = @toppings
@@ -72,46 +61,46 @@ end
72
61
 
73
62
  class BurgerStyler
74
63
  include Psychgus::Styler
75
-
64
+
76
65
  def initialize(sniffer)
77
66
  @level = sniffer.level
78
67
  @position = sniffer.position
79
68
  end
80
-
69
+
81
70
  def style(sniffer,node)
82
71
  # Remove ugly and unsafe "!ruby/object:Burger"
83
72
  node.tag = nil if node.respond_to?(:tag)
84
73
  end
85
-
74
+
86
75
  def style_mapping(sniffer,node)
87
76
  parent = sniffer.parent
88
-
89
- if !parent.nil?()
77
+
78
+ if !parent.nil?
90
79
  # BBQ
91
80
  node.style = Psychgus::MAPPING_FLOW if parent.respond_to?(:value) && parent.value.casecmp('BBQ') == 0
92
81
  end
93
82
  end
94
-
83
+
95
84
  def style_scalar(sniffer,node)
96
85
  # Only for Burgers
97
86
  node.style = Psychgus::SCALAR_SINGLE_QUOTED
98
87
  end
99
-
88
+
100
89
  def style_sequence(sniffer,node)
101
90
  relative_level = (sniffer.level - @level) + 1
102
-
91
+
103
92
  # [Ketchup, Mustard]
104
93
  node.style = Psychgus::SEQUENCE_FLOW if relative_level == 3
105
94
  end
106
95
  end
107
96
 
108
97
  class BlueberryTest < PsychgusTester
109
- def setup()
110
- @burgers = Burgers.new()
98
+ def setup
99
+ @burgers = Burgers.new
111
100
  end
112
-
113
- def test_blueberry()
114
- expected = <<-EOY
101
+
102
+ def test_blueberry
103
+ expected = <<-YAML
115
104
  |--- !ruby/object:Burgers
116
105
  |Burgers:
117
106
  | Classic:
@@ -133,9 +122,9 @@ class BlueberryTest < PsychgusTester
133
122
  | - Mustard
134
123
  | - - Salt
135
124
  | - Pepper
136
- EOY
125
+ YAML
137
126
  expected = self.class.lstrip_pipe(expected)
138
-
139
- assert_equal expected,@burgers.to_yaml()
127
+
128
+ assert_equal expected,@burgers.to_yaml
140
129
  end
141
130
  end