psychgus 1.0.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -89,7 +89,7 @@ module Psychgus
89
89
  # Then the levels and positions will be as follows:
90
90
  # # (level:position):current_node - <parent:(parent_level:parent_position)>
91
91
  #
92
- # (1:1):Psych::Nodes::Stream - <nil>
92
+ # (1:1):Psych::Nodes::Stream - <root:(0:0)>
93
93
  # (1:1):Psych::Nodes::Document - <stream:(1:1)>
94
94
  # (1:1):Psych::Nodes::Mapping - <doc:(1:1)>
95
95
  # (2:1):Burgers - <map:(1:1)>
@@ -168,15 +168,18 @@ module Psychgus
168
168
  def initialize()
169
169
  @aliases = []
170
170
  @documents = []
171
- @level = 1
171
+ @level = 0
172
172
  @mappings = []
173
173
  @nodes = []
174
174
  @parent = nil
175
175
  @parents = []
176
- @position = 1
176
+ @position = 0
177
177
  @scalars = []
178
178
  @sequences = []
179
179
  @streams = []
180
+
181
+ # Do not pass in "top_level: true"
182
+ start_parent(nil,debug_tag: :root)
180
183
  end
181
184
 
182
185
  # Add a Psych::Nodes::Alias to this class only (not to the YAML).
@@ -406,7 +409,7 @@ module Psychgus
406
409
  @parent = Parent.new(self,node,**extra)
407
410
 
408
411
  @parents.push(@parent)
409
- @nodes.push(node)
412
+ @nodes.push(node) unless node.nil?()
410
413
 
411
414
  if top_level
412
415
  @level = 1
@@ -85,34 +85,34 @@ module Psychgus
85
85
  # @see Psych::Nodes::Scalar#anchor=
86
86
  # @see Psych::Nodes::Sequence#anchor=
87
87
  def anchor=(anchor)
88
- node.anchor = anchor
88
+ @node.anchor = anchor
89
89
  end
90
90
 
91
91
  # @see Psych::Nodes::Scalar#plain=
92
92
  def plain=(plain)
93
- node.plain = plain
93
+ @node.plain = plain
94
94
  end
95
95
 
96
96
  # @see Psych::Nodes::Scalar#quoted=
97
97
  def quoted=(quoted)
98
- node.quoted = quoted
98
+ @node.quoted = quoted
99
99
  end
100
100
 
101
101
  # @see Psych::Nodes::Mapping#style=
102
102
  # @see Psych::Nodes::Scalar#style=
103
103
  # @see Psych::Nodes::Sequence#style=
104
104
  def style=(style)
105
- node.style = style
105
+ @node.style = style
106
106
  end
107
107
 
108
108
  # @see Psych::Nodes::Node#tag=
109
109
  def tag=(tag)
110
- node.tag = tag
110
+ @node.tag = tag
111
111
  end
112
112
 
113
113
  # @see Psych::Nodes::Scalar#value=
114
114
  def value=(value)
115
- node.value = value
115
+ @node.value = value
116
116
  end
117
117
 
118
118
  # @see Psych::Nodes::Alias#anchor
@@ -120,86 +120,104 @@ module Psychgus
120
120
  # @see Psych::Nodes::Scalar#anchor
121
121
  # @see Psych::Nodes::Sequence#anchor
122
122
  def anchor()
123
- return node.anchor
123
+ return @node.anchor
124
+ end
125
+
126
+ # Check if the children of this parent are keys to a Mapping.
127
+ #
128
+ # @return [true,false] whether the children are keys to a Mapping
129
+ #
130
+ # @since 1.2.0
131
+ def child_key?()
132
+ return @child_type == :key
133
+ end
134
+
135
+ # Check if the children of this parent are values to a Mapping (i.e., values to a key).
136
+ #
137
+ # @return [true,false] whether the children are values to a Mapping (i.e., values to a key)
138
+ #
139
+ # @since 1.2.0
140
+ def child_value?()
141
+ return @child_type == :value
124
142
  end
125
143
 
126
144
  # @see Psych::Nodes::Stream#encoding
127
145
  def encoding()
128
- return node.encoding
146
+ return @node.encoding
129
147
  end
130
148
 
131
149
  # @see Psych::Nodes::Node#end_column
132
150
  def end_column()
133
- return node.end_column
151
+ return @node.end_column
134
152
  end
135
153
 
136
154
  # @see Psych::Nodes::Node#end_line
137
155
  def end_line()
138
- return node.end_line
156
+ return @node.end_line
139
157
  end
140
158
 
141
159
  # @see Psych::Nodes::Document#implicit
142
160
  # @see Psych::Nodes::Mapping#implicit
143
161
  # @see Psych::Nodes::Sequence#implicit
144
162
  def implicit?()
145
- return node.implicit
163
+ return @node.implicit
146
164
  end
147
165
 
148
166
  # @see Psych::Nodes::Document#implicit_end
149
167
  def implicit_end?()
150
- return node.implicit_end
168
+ return @node.implicit_end
151
169
  end
152
170
 
153
171
  # (see Ext::NodeExt#node_of?)
154
172
  def node_of?(*names)
155
- return node.node_of?(*names)
173
+ return @node.node_of?(*names)
156
174
  end
157
175
 
158
176
  # @see Psych::Nodes::Scalar#plain
159
177
  def plain?()
160
- return node.plain
178
+ return @node.plain
161
179
  end
162
180
 
163
181
  # @see Psych::Nodes::Scalar#quoted
164
182
  def quoted?()
165
- return node.quoted
183
+ return @node.quoted
166
184
  end
167
185
 
168
186
  # @see Psych::Nodes::Node#start_column
169
187
  def start_column()
170
- return node.start_column
188
+ return @node.start_column
171
189
  end
172
190
 
173
191
  # @see Psych::Nodes::Node#start_line
174
192
  def start_line()
175
- return node.start_line
193
+ return @node.start_line
176
194
  end
177
195
 
178
196
  # @see Psych::Nodes::Mapping#style
179
197
  # @see Psych::Nodes::Scalar#style
180
198
  # @see Psych::Nodes::Sequence#style
181
199
  def style()
182
- return node.style
200
+ return @node.style
183
201
  end
184
202
 
185
203
  # @see Psych::Nodes::Node#tag
186
204
  def tag()
187
- return node.tag
205
+ return @node.tag
188
206
  end
189
207
 
190
208
  # @see Psych::Nodes::Document#tag_directives
191
209
  def tag_directives()
192
- return node.tag_directives
210
+ return @node.tag_directives
193
211
  end
194
212
 
195
213
  # @see Psych::Nodes::Scalar#value
196
214
  def value()
197
- return node.value
215
+ return @node.value
198
216
  end
199
217
 
200
218
  # @see Psych::Nodes::Document#version
201
219
  def version()
202
- return node.version
220
+ return @node.version
203
221
  end
204
222
 
205
223
  # @note If this method is modified, then tests will fail
@@ -23,5 +23,5 @@
23
23
 
24
24
  module Psychgus
25
25
  # Version of this gem in "#.#.#" format
26
- VERSION = '1.0.1'
26
+ VERSION = '1.2.0'
27
27
  end
@@ -26,30 +26,32 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
26
26
  require 'psychgus/version'
27
27
 
28
28
  Gem::Specification.new do |spec|
29
- spec.name = 'psychgus'
30
- spec.version = Psychgus::VERSION
31
- spec.authors = ['Jonathan Bradley Whited (@esotericpig)']
32
- spec.email = ['bradley@esotericpig.com']
33
- spec.licenses = ['LGPL-3.0-or-later']
29
+ spec.name = 'psychgus'
30
+ spec.version = Psychgus::VERSION
31
+ spec.authors = ['Jonathan Bradley Whited (@esotericpig)']
32
+ spec.email = ['bradley@esotericpig.com']
33
+ spec.licenses = ['LGPL-3.0-or-later']
34
+ 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.)
34
37
 
35
38
  spec.metadata = {
36
39
  'bug_tracker_uri' => 'https://github.com/esotericpig/psychgus/issues',
40
+ 'changelog_uri' => 'https://github.com/esotericpig/psychgus/blob/master/CHANGELOG.md',
37
41
  'documentation_uri' => 'https://esotericpig.github.io/docs/psychgus/yardoc/index.html',
38
42
  'homepage_uri' => 'https://github.com/esotericpig/psychgus',
39
43
  'source_code_uri' => 'https://github.com/esotericpig/psychgus'
40
44
  }
41
45
 
42
- spec.homepage = 'https://github.com/esotericpig/psychgus'
43
- spec.summary = %q(Easily style YAML files using Psych, like Sequence/Mapping Flow style.)
44
- spec.description = %q(Easily style YAML files using Psych, like Sequence/Mapping Flow style.)
45
-
46
- spec.files = Dir.glob("{lib,test,yard}/**/*") + %w(
47
- Gemfile
48
- LICENSE.txt
49
- psychgus.gemspec
50
- Rakefile
51
- README.md
52
- )
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
+ )
53
55
  spec.require_paths = ['lib']
54
56
 
55
57
  spec.required_ruby_version = '>= 2.1.10'
@@ -24,46 +24,20 @@ require 'psychgus_tester'
24
24
 
25
25
  require 'stringio'
26
26
 
27
- class IOStyler
28
- include Psychgus::Styler
29
-
30
- attr_reader :io
31
-
32
- def initialize(io=StringIO.new())
33
- @io = io
34
- end
35
-
36
- def style(sniffer,node)
37
- (1...sniffer.level).each do
38
- @io.print ' '
39
- end
40
-
41
- name = node.node_of?(:scalar) ? node.value : node.class.name
42
- parent = sniffer.parent
43
-
44
- @io.print "(#{sniffer.level}:#{sniffer.position}):#{name} - "
45
- @io.print parent.nil?() ? '<nil>' : parent
46
- @io.puts
47
- end
48
- end
49
-
50
27
  class SnifferTest < PsychgusTester
51
28
  def setup()
52
29
  end
53
30
 
54
- def assert_hierarchy(data,expected)
31
+ def assert_hierarchy(*data,expected)
55
32
  expected = self.class.lstrip_pipe(expected)
56
- io_styler = IOStyler.new()
57
-
58
- data.to_yaml(stylers: io_styler)
59
- hierarchy = io_styler.io.string
33
+ hierarchy = Psychgus.hierarchy(*data,verbose: true)
60
34
 
61
35
  assert_equal expected,hierarchy
62
36
  end
63
37
 
64
38
  def test_multi_doc()
65
- expected = <<-EOH
66
- |(1:1):Psych::Nodes::Stream - <nil>
39
+ assert_hierarchy(BURGERS_DATA,COURSES_DATA,DOLPHINS_DATA,<<-EOH
40
+ |(1:1):Psych::Nodes::Stream - <root:(0:0)::(:1)>
67
41
  |(1:1):Psych::Nodes::Document - <stream:(1:1)::(:1)>
68
42
  |(1:1):Psych::Nodes::Mapping - <doc:(1:1)::(:1)>
69
43
  | (2:1):Burgers - <map:(1:1):key:(:1)>
@@ -180,19 +154,12 @@ class SnifferTest < PsychgusTester
180
154
  | (4:1):Psych::Nodes::Alias - <seq:(3:1)::(:1)>
181
155
  | (4:2):Psych::Nodes::Alias - <seq:(3:1)::(:2)>
182
156
  EOH
183
-
184
- expected = self.class.lstrip_pipe(expected)
185
- io_styler = IOStyler.new()
186
-
187
- Psychgus.dump_stream(BURGERS_DATA,COURSES_DATA,DOLPHINS_DATA,stylers: io_styler)
188
- hierarchy = io_styler.io.string
189
-
190
- assert_equal expected,hierarchy
157
+ )
191
158
  end
192
159
 
193
160
  def test_single_docs()
194
161
  assert_hierarchy(BURGERS_DATA,<<-EOH
195
- |(1:1):Psych::Nodes::Stream - <nil>
162
+ |(1:1):Psych::Nodes::Stream - <root:(0:0)::(:1)>
196
163
  |(1:1):Psych::Nodes::Document - <stream:(1:1)::(:1)>
197
164
  |(1:1):Psych::Nodes::Mapping - <doc:(1:1)::(:1)>
198
165
  | (2:1):Burgers - <map:(1:1):key:(:1)>
@@ -242,7 +209,7 @@ class SnifferTest < PsychgusTester
242
209
  )
243
210
 
244
211
  assert_hierarchy(COURSES_DATA,<<-EOH
245
- |(1:1):Psych::Nodes::Stream - <nil>
212
+ |(1:1):Psych::Nodes::Stream - <root:(0:0)::(:1)>
246
213
  |(1:1):Psych::Nodes::Document - <stream:(1:1)::(:1)>
247
214
  |(1:1):Psych::Nodes::Mapping - <doc:(1:1)::(:1)>
248
215
  | (2:1):Courses - <map:(1:1):key:(:1)>
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ #--
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/>.
20
+ #++
21
+
22
+
23
+ require 'psychgus_tester'
24
+
25
+ class EggCarton
26
+ include Psychgus::Blueberry
27
+
28
+ def initialize()
29
+ @eggs = {
30
+ :styles => ['omelette','BBQ eggs','hard-boiled eggs','soft_boiled eggs','fried@eggs'],
31
+ :colors => ['brown','white',['blue','green']]
32
+ }
33
+ end
34
+
35
+ def psychgus_stylers(sniffer)
36
+ return Psychgus::FlowStyler.new(4)
37
+ end
38
+ end
39
+
40
+ ###
41
+ # @since 1.2.0
42
+ ###
43
+ class StylersTest < PsychgusTester
44
+ def setup()
45
+ @egg_carton = EggCarton.new()
46
+ end
47
+
48
+ def test_capstyler()
49
+ actual = @egg_carton.to_yaml(stylers: Psychgus::CapStyler.new(each_word: false))
50
+ expected = <<-EOY
51
+ |--- !ruby/object:EggCarton
52
+ |Eggs:
53
+ | :styles: [Omelette, BBQ eggs, Hard-boiled eggs, Soft_boiled eggs, Fried@eggs]
54
+ | :colors: [Brown, White, [Blue, Green]]
55
+ EOY
56
+
57
+ assert_equal self.class.lstrip_pipe(expected),actual
58
+
59
+ actual = @egg_carton.to_yaml(stylers: Psychgus::CapStyler.new(new_delim: '+',delim: /[\s_\-@]/))
60
+ expected = <<-EOY
61
+ |--- !ruby/object:EggCarton
62
+ |Eggs:
63
+ | :styles: [Omelette, BBQ+Eggs, Hard+Boiled+Eggs, Soft+Boiled+Eggs, Fried+Eggs]
64
+ | :colors: [Brown, White, [Blue, Green]]
65
+ EOY
66
+
67
+ assert_equal self.class.lstrip_pipe(expected),actual
68
+ end
69
+
70
+ def test_nosymstyler()
71
+ actual = @egg_carton.to_yaml(stylers: Psychgus::NoSymStyler.new())
72
+ expected = <<-EOY
73
+ |--- !ruby/object:EggCarton
74
+ |eggs:
75
+ | Styles: [omelette, BBQ eggs, hard-boiled eggs, soft_boiled eggs, fried@eggs]
76
+ | Colors: [brown, white, [blue, green]]
77
+ EOY
78
+
79
+ assert_equal self.class.lstrip_pipe(expected),actual
80
+ end
81
+
82
+ def test_notagstyler()
83
+ actual = @egg_carton.to_yaml(stylers: Psychgus::NoTagStyler.new())
84
+ expected = <<-EOY
85
+ |---
86
+ |eggs:
87
+ | :styles: [omelette, BBQ eggs, hard-boiled eggs, soft_boiled eggs, fried@eggs]
88
+ | :colors: [brown, white, [blue, green]]
89
+ EOY
90
+
91
+ assert_equal self.class.lstrip_pipe(expected),actual
92
+ end
93
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: psychgus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Bradley Whited (@esotericpig)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-03 00:00:00.000000000 Z
11
+ date: 2019-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: psych
@@ -115,6 +115,7 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
+ - CHANGELOG.md
118
119
  - Gemfile
119
120
  - LICENSE.txt
120
121
  - README.md
@@ -125,9 +126,11 @@ files:
125
126
  - lib/psychgus/ext/core_ext.rb
126
127
  - lib/psychgus/ext/node_ext.rb
127
128
  - lib/psychgus/ext/yaml_tree_ext.rb
129
+ - lib/psychgus/stylables.rb
128
130
  - lib/psychgus/styled_document_stream.rb
129
131
  - lib/psychgus/styled_tree_builder.rb
130
132
  - lib/psychgus/styler.rb
133
+ - lib/psychgus/stylers.rb
131
134
  - lib/psychgus/super_sniffer.rb
132
135
  - lib/psychgus/super_sniffer/parent.rb
133
136
  - lib/psychgus/version.rb
@@ -137,12 +140,14 @@ files:
137
140
  - test/psychgus_tester.rb
138
141
  - test/sniffer_test.rb
139
142
  - test/styler_test.rb
143
+ - test/stylers_test.rb
140
144
  - yard/templates/default/layout/html/footer.erb
141
145
  homepage: https://github.com/esotericpig/psychgus
142
146
  licenses:
143
147
  - LGPL-3.0-or-later
144
148
  metadata:
145
149
  bug_tracker_uri: https://github.com/esotericpig/psychgus/issues
150
+ changelog_uri: https://github.com/esotericpig/psychgus/blob/master/CHANGELOG.md
146
151
  documentation_uri: https://esotericpig.github.io/docs/psychgus/yardoc/index.html
147
152
  homepage_uri: https://github.com/esotericpig/psychgus
148
153
  source_code_uri: https://github.com/esotericpig/psychgus