psychgus 1.3.5 → 1.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +0 -2
- data/CHANGELOG.md +15 -4
- data/Gemfile +5 -5
- data/README.md +164 -173
- data/Rakefile +9 -12
- data/lib/psychgus/blueberry.rb +2 -2
- data/lib/psychgus/ext/core_ext.rb +6 -5
- data/lib/psychgus/ext/yaml_tree_ext.rb +1 -2
- data/lib/psychgus/stylables.rb +7 -7
- data/lib/psychgus/styled_document_stream.rb +1 -1
- data/lib/psychgus/styled_tree_builder.rb +2 -2
- data/lib/psychgus/stylers.rb +19 -15
- data/lib/psychgus/version.rb +1 -1
- data/lib/psychgus.rb +47 -306
- data/psychgus.gemspec +13 -10
- data/test/blueberry_test.rb +57 -58
- data/test/psychgus_test.rb +35 -36
- data/test/sniffer_test.rb +6 -6
- data/test/styler_test.rb +32 -33
- data/test/stylers_test.rb +25 -25
- data/test/{psychgus_tester.rb → test_helper.rb} +8 -8
- metadata +6 -6
- data/yard/templates/default/layout/html/footer.erb +0 -5
data/lib/psychgus/blueberry.rb
CHANGED
@@ -24,7 +24,7 @@ module Psychgus
|
|
24
24
|
# attr_reader :my_hash
|
25
25
|
#
|
26
26
|
# def initialize()
|
27
|
-
# @my_hash = {:
|
27
|
+
# @my_hash = {key1: 'val1',key2: 'val2'}
|
28
28
|
# end
|
29
29
|
#
|
30
30
|
# def psychgus_stylers(sniffer)
|
@@ -88,7 +88,7 @@ module Psychgus
|
|
88
88
|
# class/module/etc.
|
89
89
|
#
|
90
90
|
# @return [Styler,Array<Styler>,nil] {Styler}(s) for this class/module/etc.
|
91
|
-
def psychgus_stylers(
|
91
|
+
def psychgus_stylers(_sniffer)
|
92
92
|
return nil
|
93
93
|
end
|
94
94
|
end
|
@@ -30,10 +30,11 @@ module Psychgus
|
|
30
30
|
# end
|
31
31
|
#
|
32
32
|
# my_obj = {
|
33
|
-
# :
|
34
|
-
# :
|
35
|
-
# :
|
36
|
-
#
|
33
|
+
# Foods: {
|
34
|
+
# Fruits: %w[Apple Banana Blueberry Pear],
|
35
|
+
# Veggies: %w[Bean Carrot Celery Pea],
|
36
|
+
# }
|
37
|
+
# }
|
37
38
|
#
|
38
39
|
# puts my_obj.to_yaml(indentation: 5,stylers: MyStyler.new)
|
39
40
|
#
|
@@ -51,7 +52,7 @@ module Psychgus
|
|
51
52
|
# @return [String] the YAML generated from this Object
|
52
53
|
#
|
53
54
|
# @see Psychgus.dump
|
54
|
-
def to_yaml(options={})
|
55
|
+
def to_yaml(options = {})
|
55
56
|
# NOTE: This method signature must use old-style `options={}` instead of `**options`!
|
56
57
|
# Because some Gems, like `Moneta`, depend on this.
|
57
58
|
|
@@ -33,7 +33,6 @@ module Psychgus
|
|
33
33
|
else # 5.1.2+
|
34
34
|
return unless key?(target)
|
35
35
|
|
36
|
-
@targets.delete(target)
|
37
36
|
@obj_to_node.delete(target)
|
38
37
|
end
|
39
38
|
end
|
@@ -81,7 +80,7 @@ module Psychgus
|
|
81
80
|
@st.psychgus_unregister(target) if @emitter.deref_aliases?
|
82
81
|
end
|
83
82
|
|
84
|
-
result = super(target)
|
83
|
+
result = super(target) # rubocop:disable Style/SuperArguments
|
85
84
|
|
86
85
|
# Check styler_count because @emitter may not be a StyledTreeBuilder and target may not be a Blueberry
|
87
86
|
@emitter.pop_styler(styler_count) if styler_count > 0
|
data/lib/psychgus/stylables.rb
CHANGED
@@ -38,7 +38,7 @@ module Psychgus
|
|
38
38
|
# @param min_level [Integer] the minimum level (inclusive) to style
|
39
39
|
# @param new_style [Integer] the new style to set the nodes to
|
40
40
|
# @param kargs [Hash] capture extra keyword args, so no error for undefined args
|
41
|
-
def initialize(min_level=0,new_style: nil,**
|
41
|
+
def initialize(min_level = 0,new_style: nil,**_kargs)
|
42
42
|
@min_level = min_level
|
43
43
|
@new_style = new_style
|
44
44
|
end
|
@@ -67,7 +67,7 @@ module Psychgus
|
|
67
67
|
# @param new_delim [nil,String] the replacement for each +delim+ if not nil
|
68
68
|
# @param delim [String,Regexp] the delimiter to split on
|
69
69
|
# @param kargs [Hash] capture extra keyword args, so no error for undefined args
|
70
|
-
def initialize(each_word: true,new_delim: nil,delim: /[\s_\-]/,**
|
70
|
+
def initialize(each_word: true,new_delim: nil,delim: /[\s_\-]/,**_kargs)
|
71
71
|
delim = Regexp.quote(delim.to_s) unless delim.is_a?(Regexp)
|
72
72
|
|
73
73
|
@delim = Regexp.new("(#{delim})")
|
@@ -95,7 +95,7 @@ module Psychgus
|
|
95
95
|
#
|
96
96
|
# @see cap_word
|
97
97
|
# @see Styler#style_scalar
|
98
|
-
def style_scalar(
|
98
|
+
def style_scalar(_sniffer,node)
|
99
99
|
if !@each_word || node.value.nil? || node.value.empty?
|
100
100
|
node.value = cap_word(node.value)
|
101
101
|
return
|
@@ -128,7 +128,7 @@ module Psychgus
|
|
128
128
|
# @param io [IO] the IO to write to
|
129
129
|
# @param verbose [true,false] whether to be more verbose (e.g., write child info)
|
130
130
|
# @param kargs [Hash] capture extra keyword args, so no error for undefined args
|
131
|
-
def initialize(io: StringIO.new,verbose: false,**
|
131
|
+
def initialize(io: StringIO.new,verbose: false,**_kargs)
|
132
132
|
@io = io
|
133
133
|
@verbose = verbose
|
134
134
|
end
|
@@ -199,14 +199,14 @@ module Psychgus
|
|
199
199
|
|
200
200
|
# @param cap [true,false] whether to capitalize the symbol
|
201
201
|
# @param kargs [Hash] capture extra keyword args, so no error for undefined args
|
202
|
-
def initialize(cap: true,**
|
202
|
+
def initialize(cap: true,**_kargs)
|
203
203
|
@cap = cap
|
204
204
|
end
|
205
205
|
|
206
206
|
# If +node.value+ is a symbol, change it into a string and capitalize it.
|
207
207
|
#
|
208
208
|
# @see Styler#style_scalar
|
209
|
-
def style_scalar(
|
209
|
+
def style_scalar(_sniffer,node)
|
210
210
|
return if node.value.nil? || node.value.empty?
|
211
211
|
return if node.value[0] != ':'
|
212
212
|
|
@@ -224,7 +224,7 @@ module Psychgus
|
|
224
224
|
# If +node.tag+ is settable, set it to nil.
|
225
225
|
#
|
226
226
|
# @see Styler#style
|
227
|
-
def style(
|
227
|
+
def style(_sniffer,node)
|
228
228
|
node.tag = nil if node.respond_to?(:tag=)
|
229
229
|
end
|
230
230
|
end
|
@@ -35,7 +35,7 @@ module Psychgus
|
|
35
35
|
# This mimics the behavior of Psych::Handlers::DocumentStream#end_document.
|
36
36
|
#
|
37
37
|
# @see Psych::Handlers::DocumentStream#end_document
|
38
|
-
def end_document(implicit_end
|
38
|
+
def end_document(implicit_end = !streaming?)
|
39
39
|
@last.implicit_end = implicit_end
|
40
40
|
@block.call(pop)
|
41
41
|
end
|
@@ -37,7 +37,7 @@ module Psychgus
|
|
37
37
|
# @param stylers [Styler] {Styler}(s) to use for styling this TreeBuilder
|
38
38
|
# @param deref_aliases [true,false] whether to dereference aliases; output the actual value
|
39
39
|
# instead of the alias
|
40
|
-
def initialize(*stylers,deref_aliases: false,**
|
40
|
+
def initialize(*stylers,deref_aliases: false,**_options)
|
41
41
|
super()
|
42
42
|
|
43
43
|
@deref_aliases = deref_aliases
|
@@ -143,7 +143,7 @@ module Psychgus
|
|
143
143
|
# @param count [Integer] the optional amount of tail elements to pop
|
144
144
|
#
|
145
145
|
# @return [Styler,Array<Styler>,nil] the last {Styler}(s), or if empty or count==0, nil
|
146
|
-
def pop_styler(count=1)
|
146
|
+
def pop_styler(count = 1)
|
147
147
|
return nil if count == 0
|
148
148
|
return @stylers.pop if count == 1
|
149
149
|
|
data/lib/psychgus/stylers.rb
CHANGED
@@ -20,21 +20,23 @@ module Psychgus
|
|
20
20
|
# class EggCarton
|
21
21
|
# def initialize
|
22
22
|
# @eggs = {
|
23
|
-
# :
|
24
|
-
# :
|
23
|
+
# styles: ['fried', 'scrambled', ['BBQ', 'ketchup & mustard']],
|
24
|
+
# colors: ['brown', 'white', ['blue', 'green']],
|
25
25
|
# }
|
26
26
|
# end
|
27
27
|
# end
|
28
28
|
#
|
29
29
|
# hierarchy = Psychgus::HierarchyStyler.new(io: $stdout)
|
30
30
|
#
|
31
|
-
# puts EggCarton.new.to_yaml(
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
31
|
+
# puts EggCarton.new.to_yaml(
|
32
|
+
# stylers: [
|
33
|
+
# Psychgus::NoSymStyler.new,
|
34
|
+
# Psychgus::NoTagStyler.new,
|
35
|
+
# Psychgus::CapStyler.new,
|
36
|
+
# Psychgus::FlowStyler.new(4),
|
37
|
+
# hierarchy,
|
38
|
+
# ]
|
39
|
+
# )
|
38
40
|
#
|
39
41
|
# # Output:
|
40
42
|
# # ---
|
@@ -78,8 +80,9 @@ module Psychgus
|
|
78
80
|
# 'BBQ eggs',
|
79
81
|
# 'hard-boiled eggs',
|
80
82
|
# 'soft_boiled eggs',
|
81
|
-
# 'fried@eggs'
|
82
|
-
#
|
83
|
+
# 'fried@eggs',
|
84
|
+
# ]
|
85
|
+
# }
|
83
86
|
#
|
84
87
|
# seq_flow = Psychgus::SeqFlowStyler.new
|
85
88
|
#
|
@@ -205,10 +208,11 @@ module Psychgus
|
|
205
208
|
# require 'psychgus'
|
206
209
|
#
|
207
210
|
# data = {
|
208
|
-
# :
|
209
|
-
# :
|
210
|
-
# :
|
211
|
-
#
|
211
|
+
# eggs: {
|
212
|
+
# styles: ['Fried', 'Scrambled', ['BBQ', 'Ketchup']],
|
213
|
+
# colors: ['Brown', 'White', ['Blue', 'Green']],
|
214
|
+
# }
|
215
|
+
# }
|
212
216
|
#
|
213
217
|
# flow = Psychgus::FlowStyler.new(4)
|
214
218
|
#
|
data/lib/psychgus/version.rb
CHANGED
data/lib/psychgus.rb
CHANGED
@@ -22,7 +22,6 @@ require 'psychgus/version'
|
|
22
22
|
require 'psychgus/ext/core_ext'
|
23
23
|
require 'psychgus/ext/node_ext'
|
24
24
|
require 'psychgus/ext/yaml_tree_ext'
|
25
|
-
|
26
25
|
require 'psychgus/super_sniffer/parent'
|
27
26
|
|
28
27
|
###
|
@@ -31,267 +30,6 @@ require 'psychgus/super_sniffer/parent'
|
|
31
30
|
# Thank you to the people that worked and continue to work hard on that project.
|
32
31
|
#
|
33
32
|
# The name comes from the well-styled character Gus from the TV show Psych.
|
34
|
-
#
|
35
|
-
# == Create a Styler
|
36
|
-
#
|
37
|
-
# First, we will create a {Styler}.
|
38
|
-
#
|
39
|
-
# All you need to do is add +include Psychgus::Styler+ to a class.
|
40
|
-
#
|
41
|
-
# Here is a complex {Styler} for the examples below:
|
42
|
-
# require 'psychgus'
|
43
|
-
#
|
44
|
-
# class BurgerStyler
|
45
|
-
# # Mix in methods needed for styling
|
46
|
-
# include Psychgus::Styler
|
47
|
-
#
|
48
|
-
# def initialize(sniffer=nil)
|
49
|
-
# if sniffer.nil?()
|
50
|
-
# @class_level = 0
|
51
|
-
# @class_position = 0
|
52
|
-
# else
|
53
|
-
# # For the Class Example
|
54
|
-
# @class_level = sniffer.level
|
55
|
-
# @class_position = sniffer.position
|
56
|
-
# end
|
57
|
-
# end
|
58
|
-
#
|
59
|
-
# # Style all nodes (Psych::Nodes::Node)
|
60
|
-
# def style(sniffer,node)
|
61
|
-
# # Remove "!ruby/object:..." for classes
|
62
|
-
# node.tag = nil if node.node_of?(:mapping,:scalar,:sequence)
|
63
|
-
#
|
64
|
-
# # This is another way to do the above
|
65
|
-
# #node.tag = nil if node.respond_to?(:tag=)
|
66
|
-
# end
|
67
|
-
#
|
68
|
-
# # Style aliases (Psych::Nodes::Alias)
|
69
|
-
# def style_alias(sniffer,node)
|
70
|
-
# end
|
71
|
-
#
|
72
|
-
# # Style maps (Psych::Nodes::Mapping)
|
73
|
-
# # - Hashes (key/value pairs)
|
74
|
-
# # - Example: "Burgers: Classic {}"
|
75
|
-
# def style_mapping(sniffer,node)
|
76
|
-
# parent = sniffer.parent
|
77
|
-
#
|
78
|
-
# if !parent.nil?()
|
79
|
-
# # BBQ
|
80
|
-
# node.style = Psychgus::MAPPING_FLOW if parent.node_of?(:scalar) &&
|
81
|
-
# parent.value.casecmp('BBQ') == 0
|
82
|
-
# end
|
83
|
-
# end
|
84
|
-
#
|
85
|
-
# # Style scalars (Psych::Nodes::Scalar)
|
86
|
-
# # - Any text (non-alias)
|
87
|
-
# def style_scalar(sniffer,node)
|
88
|
-
# parent = sniffer.parent
|
89
|
-
#
|
90
|
-
# # Single quote scalars that are not keys to a map
|
91
|
-
# # - "child_key?" is the same as "child_type == :key"
|
92
|
-
# node.style = Psychgus::SCALAR_SINGLE_QUOTED unless parent.child_key?()
|
93
|
-
#
|
94
|
-
# # Remove colon (change symbols into strings)
|
95
|
-
# node.value = node.value.sub(':','')
|
96
|
-
#
|
97
|
-
# # Change lettuce to spinach
|
98
|
-
# node.value = 'Spinach' if node.value.casecmp('Lettuce') == 0
|
99
|
-
#
|
100
|
-
# # Capitalize each word
|
101
|
-
# node.value = node.value.split(' ').map do |v|
|
102
|
-
# if v.casecmp('BBQ') == 0
|
103
|
-
# v.upcase()
|
104
|
-
# else
|
105
|
-
# v.capitalize()
|
106
|
-
# end
|
107
|
-
# end.join(' ')
|
108
|
-
# end
|
109
|
-
#
|
110
|
-
# # Style sequences (Psych::Nodes::Sequence)
|
111
|
-
# # - Arrays
|
112
|
-
# # - Example: "[Lettuce, Onions, Pickles, Tomatoes]"
|
113
|
-
# def style_sequence(sniffer,node)
|
114
|
-
# relative_level = (sniffer.level - @class_level) + 1
|
115
|
-
#
|
116
|
-
# node.style = Psychgus::SEQUENCE_FLOW if sniffer.level >= 4
|
117
|
-
#
|
118
|
-
# # Make "[Ketchup, Mustard]" a block for the Class Example
|
119
|
-
# node.style = Psychgus::SEQUENCE_BLOCK if relative_level == 7
|
120
|
-
# end
|
121
|
-
# end
|
122
|
-
#
|
123
|
-
# @example Hash example
|
124
|
-
# require 'psychgus'
|
125
|
-
#
|
126
|
-
# burgers = {
|
127
|
-
# :Burgers => {
|
128
|
-
# :Classic => {
|
129
|
-
# :Sauce => %w(Ketchup Mustard),
|
130
|
-
# :Cheese => 'American',
|
131
|
-
# :Bun => 'Sesame Seed'
|
132
|
-
# },
|
133
|
-
# :BBQ => {
|
134
|
-
# :Sauce => 'Honey BBQ',
|
135
|
-
# :Cheese => 'Cheddar',
|
136
|
-
# :Bun => 'Kaiser'
|
137
|
-
# },
|
138
|
-
# :Fancy => {
|
139
|
-
# :Sauce => 'Spicy Wasabi',
|
140
|
-
# :Cheese => 'Smoked Gouda',
|
141
|
-
# :Bun => 'Hawaiian'
|
142
|
-
# }
|
143
|
-
# },
|
144
|
-
# :Toppings => [
|
145
|
-
# 'Mushrooms',
|
146
|
-
# %w(Lettuce Onions Pickles Tomatoes),
|
147
|
-
# [%w(Ketchup Mustard), %w(Salt Pepper)]
|
148
|
-
# ]
|
149
|
-
# }
|
150
|
-
# burgers[:Favorite] = burgers[:Burgers][:BBQ] # Alias
|
151
|
-
#
|
152
|
-
# puts burgers.to_yaml(indent: 3,stylers: BurgerStyler.new,deref_aliases: true)
|
153
|
-
#
|
154
|
-
# # Output:
|
155
|
-
# # ---
|
156
|
-
# # Burgers:
|
157
|
-
# # Classic:
|
158
|
-
# # Sauce: ['Ketchup', 'Mustard']
|
159
|
-
# # Cheese: 'American'
|
160
|
-
# # Bun: 'Sesame Seed'
|
161
|
-
# # BBQ: {Sauce: 'Honey BBQ', Cheese: 'Cheddar', Bun: 'Kaiser'}
|
162
|
-
# # Fancy:
|
163
|
-
# # Sauce: 'Spicy Wasabi'
|
164
|
-
# # Cheese: 'Smoked Gouda'
|
165
|
-
# # Bun: 'Hawaiian'
|
166
|
-
# # Toppings:
|
167
|
-
# # - 'Mushrooms'
|
168
|
-
# # - ['Spinach', 'Onions', 'Pickles', 'Tomatoes']
|
169
|
-
# # - [['Ketchup', 'Mustard'], ['Salt', 'Pepper']]
|
170
|
-
# # Favorite:
|
171
|
-
# # Sauce: 'Honey BBQ'
|
172
|
-
# # Cheese: 'Cheddar'
|
173
|
-
# # Bun: 'Kaiser'
|
174
|
-
#
|
175
|
-
# @example Class example
|
176
|
-
# require 'psychgus'
|
177
|
-
#
|
178
|
-
# class Burger
|
179
|
-
# attr_accessor :bun
|
180
|
-
# attr_accessor :cheese
|
181
|
-
# attr_accessor :sauce
|
182
|
-
#
|
183
|
-
# def initialize(sauce,cheese,bun)
|
184
|
-
# @bun = bun
|
185
|
-
# @cheese = cheese
|
186
|
-
# @sauce = sauce
|
187
|
-
# end
|
188
|
-
#
|
189
|
-
# # You can still use Psych's encode_with(), no problem
|
190
|
-
# #def encode_with(coder)
|
191
|
-
# # coder['Bun'] = @bun
|
192
|
-
# # coder['Cheese'] = @cheese
|
193
|
-
# # coder['Sauce'] = @sauce
|
194
|
-
# #end
|
195
|
-
# end
|
196
|
-
#
|
197
|
-
# class Burgers
|
198
|
-
# include Psychgus::Blueberry
|
199
|
-
#
|
200
|
-
# attr_accessor :burgers
|
201
|
-
# attr_accessor :toppings
|
202
|
-
# attr_accessor :favorite
|
203
|
-
#
|
204
|
-
# def initialize()
|
205
|
-
# @burgers = {
|
206
|
-
# 'Classic' => Burger.new(['Ketchup','Mustard'],'American','Sesame Seed'),
|
207
|
-
# 'BBQ' => Burger.new('Honey BBQ','Cheddar','Kaiser'),
|
208
|
-
# 'Fancy' => Burger.new('Spicy Wasabi','Smoked Gouda','Hawaiian')
|
209
|
-
# }
|
210
|
-
#
|
211
|
-
# @toppings = [
|
212
|
-
# 'Mushrooms',
|
213
|
-
# %w(Lettuce Onions Pickles Tomatoes),
|
214
|
-
# [%w(Ketchup Mustard),%w(Salt Pepper)]
|
215
|
-
# ]
|
216
|
-
#
|
217
|
-
# @favorite = @burgers['BBQ'] # Alias
|
218
|
-
# end
|
219
|
-
#
|
220
|
-
# def psychgus_stylers(sniffer)
|
221
|
-
# return BurgerStyler.new(sniffer)
|
222
|
-
# end
|
223
|
-
#
|
224
|
-
# # You can still use Psych's encode_with(), no problem
|
225
|
-
# #def encode_with(coder)
|
226
|
-
# # coder['Burgers'] = @burgers
|
227
|
-
# # coder['Toppings'] = @toppings
|
228
|
-
# # coder['Favorite'] = @favorite
|
229
|
-
# #end
|
230
|
-
# end
|
231
|
-
#
|
232
|
-
# burgers = Burgers.new
|
233
|
-
# puts burgers.to_yaml(indent: 3,deref_aliases: true)
|
234
|
-
#
|
235
|
-
# # Output:
|
236
|
-
# # ---
|
237
|
-
# # Burgers:
|
238
|
-
# # Classic:
|
239
|
-
# # Bun: 'Sesame Seed'
|
240
|
-
# # Cheese: 'American'
|
241
|
-
# # Sauce:
|
242
|
-
# # - 'Ketchup'
|
243
|
-
# # - 'Mustard'
|
244
|
-
# # BBQ: {Bun: 'Kaiser', Cheese: 'Cheddar', Sauce: 'Honey BBQ'}
|
245
|
-
# # Fancy:
|
246
|
-
# # Bun: 'Hawaiian'
|
247
|
-
# # Cheese: 'Smoked Gouda'
|
248
|
-
# # Sauce: 'Spicy Wasabi'
|
249
|
-
# # Toppings:
|
250
|
-
# # - 'Mushrooms'
|
251
|
-
# # - ['Spinach', 'Onions', 'Pickles', 'Tomatoes']
|
252
|
-
# # - [['Ketchup', 'Mustard'], ['Salt', 'Pepper']]
|
253
|
-
# # Favorite:
|
254
|
-
# # Bun: 'Kaiser'
|
255
|
-
# # Cheese: 'Cheddar'
|
256
|
-
# # Sauce: 'Honey BBQ'
|
257
|
-
#
|
258
|
-
# @example Emitting / Parsing examples
|
259
|
-
# styler = BurgerStyler.new()
|
260
|
-
# options = {:indentation=>3,:stylers=>styler,:deref_aliases=>true}
|
261
|
-
# yaml = burgers.to_yaml(options)
|
262
|
-
#
|
263
|
-
# # High-level emitting
|
264
|
-
# Psychgus.dump(burgers,options)
|
265
|
-
# Psychgus.dump_file('burgers.yaml',burgers,options)
|
266
|
-
# burgers.to_yaml(options)
|
267
|
-
#
|
268
|
-
# # High-level parsing
|
269
|
-
# # - Because to_ruby() will be called, just use Psych:
|
270
|
-
# # - load(), load_file(), load_stream(), safe_load()
|
271
|
-
#
|
272
|
-
# # Mid-level emitting
|
273
|
-
# stream = Psychgus.parse_stream(yaml,stylers: styler,deref_aliases: true)
|
274
|
-
#
|
275
|
-
# stream.to_yaml()
|
276
|
-
#
|
277
|
-
# # Mid-level parsing
|
278
|
-
# Psychgus.parse(yaml,stylers: styler,deref_aliases: true)
|
279
|
-
# Psychgus.parse_file('burgers.yaml',stylers: styler,deref_aliases: true)
|
280
|
-
# Psychgus.parse_stream(yaml,stylers: styler,deref_aliases: true)
|
281
|
-
#
|
282
|
-
# # Low-level emitting
|
283
|
-
# tree_builder = Psychgus::StyledTreeBuilder.new(styler,deref_aliases: true)
|
284
|
-
# visitor = Psych::Visitors::YAMLTree.create(options,tree_builder)
|
285
|
-
#
|
286
|
-
# visitor << burgers
|
287
|
-
# visitor.tree.to_yaml
|
288
|
-
#
|
289
|
-
# # Low-level parsing
|
290
|
-
# parser = Psychgus.parser(stylers: styler,deref_aliases: true)
|
291
|
-
#
|
292
|
-
# parser.parse(yaml)
|
293
|
-
# parser.handler
|
294
|
-
# parser.handler.root
|
295
33
|
###
|
296
34
|
module Psychgus
|
297
35
|
# Include these in the top namespace for convenience (i.e., less typing).
|
@@ -332,7 +70,7 @@ module Psychgus
|
|
332
70
|
# @return [Integer,Object] the constant value from the class (usually an int)
|
333
71
|
#
|
334
72
|
# @see .node_class
|
335
|
-
def self.node_const(class_name,const_name,lenient
|
73
|
+
def self.node_const(class_name,const_name,lenient: true)
|
336
74
|
node_class = node_class(class_name)
|
337
75
|
const_name = const_name.to_sym.upcase
|
338
76
|
|
@@ -378,7 +116,7 @@ module Psychgus
|
|
378
116
|
#
|
379
117
|
# @see .dump_stream
|
380
118
|
# @see Psych.dump_stream
|
381
|
-
def self.dump(object,io=nil,**options)
|
119
|
+
def self.dump(object,io = nil,**options)
|
382
120
|
return dump_stream(object,io: io,**options)
|
383
121
|
end
|
384
122
|
|
@@ -443,7 +181,7 @@ module Psychgus
|
|
443
181
|
# then options will be set to the Hash, instead of objects.
|
444
182
|
#
|
445
183
|
# For example, the below will be stored in options, not objects:
|
446
|
-
# - dump_stream({:
|
184
|
+
# - dump_stream({coffee: {roast: [],style: []}})
|
447
185
|
#
|
448
186
|
# This if-statement is guaranteed because dump_stream([]) and dump_stream(nil)
|
449
187
|
# will produce [[]] and [nil], which are not empty.
|
@@ -467,13 +205,15 @@ module Psychgus
|
|
467
205
|
end
|
468
206
|
end
|
469
207
|
|
470
|
-
visitor = Psych::Visitors::YAMLTree.create(
|
471
|
-
|
208
|
+
visitor = Psych::Visitors::YAMLTree.create(
|
209
|
+
options,
|
210
|
+
StyledTreeBuilder.new(*stylers,deref_aliases: deref_aliases)
|
211
|
+
)
|
472
212
|
|
473
213
|
if objects.empty?
|
474
214
|
# Else, will throw a cryptic NoMethodError:
|
475
|
-
#
|
476
|
-
#
|
215
|
+
# psych/tree_builder.rb:in `set_end_location':
|
216
|
+
# undefined method `end_line=' for nil:NilClass (NoMethodError)
|
477
217
|
#
|
478
218
|
# This should never occur because of the if-statement at the top of this method.
|
479
219
|
visitor << nil
|
@@ -494,21 +234,21 @@ module Psychgus
|
|
494
234
|
# require 'psychgus'
|
495
235
|
#
|
496
236
|
# burgers = {
|
497
|
-
# :
|
498
|
-
# :
|
499
|
-
#
|
500
|
-
#
|
501
|
-
# :
|
502
|
-
#
|
503
|
-
#
|
504
|
-
# :
|
505
|
-
#
|
506
|
-
#
|
237
|
+
# burgers: {
|
238
|
+
# classic: {sauce: %w[Ketchup Mustard],
|
239
|
+
# cheese: 'American',
|
240
|
+
# bun: 'Sesame Seed'},
|
241
|
+
# bbq: {sauce: 'Honey BBQ',
|
242
|
+
# cheese: 'Cheddar',
|
243
|
+
# bun: 'Kaiser'},
|
244
|
+
# fancy: {sauce: 'Spicy Wasabi',
|
245
|
+
# cheese: 'Smoked Gouda',
|
246
|
+
# bun: 'Hawaiian'},
|
507
247
|
# },
|
508
|
-
# :
|
248
|
+
# toppings: [
|
509
249
|
# 'Mushrooms',
|
510
|
-
# %w
|
511
|
-
# [%w
|
250
|
+
# %w[Lettuce Onions Pickles Tomatoes],
|
251
|
+
# [%w[Ketchup Mustard], %w[Salt Pepper]],
|
512
252
|
# ]
|
513
253
|
# }
|
514
254
|
#
|
@@ -635,17 +375,17 @@ module Psychgus
|
|
635
375
|
# and the styler(s) will do nothing for you.
|
636
376
|
#
|
637
377
|
# @example
|
638
|
-
# burgers =
|
639
|
-
#
|
640
|
-
#
|
641
|
-
#
|
642
|
-
#
|
643
|
-
#
|
644
|
-
#
|
645
|
-
#
|
646
|
-
#
|
647
|
-
#
|
648
|
-
#
|
378
|
+
# burgers = <<~EOY
|
379
|
+
# ---
|
380
|
+
# Burgers:
|
381
|
+
# Classic:
|
382
|
+
# BBQ: {Sauce: Honey BBQ, Cheese: Cheddar, Bun: Kaiser}
|
383
|
+
# ---
|
384
|
+
# Toppings:
|
385
|
+
# - [Mushrooms, Mustard]
|
386
|
+
# - [Salt, Pepper, Pickles]
|
387
|
+
# ---
|
388
|
+
# `Invalid`
|
649
389
|
# EOY
|
650
390
|
#
|
651
391
|
# i = 0
|
@@ -681,8 +421,9 @@ module Psychgus
|
|
681
421
|
# @see Psych::SyntaxError
|
682
422
|
def self.parse_stream(yaml,filename: nil,stylers: nil,deref_aliases: false,**options,&block)
|
683
423
|
if block_given?
|
684
|
-
parser = Psych::Parser.new(
|
685
|
-
|
424
|
+
parser = Psych::Parser.new(
|
425
|
+
StyledDocumentStream.new(*stylers,deref_aliases: deref_aliases,**options,&block)
|
426
|
+
)
|
686
427
|
|
687
428
|
return parser.parse(yaml,filename)
|
688
429
|
else
|
@@ -704,16 +445,16 @@ module Psychgus
|
|
704
445
|
# end
|
705
446
|
# end
|
706
447
|
#
|
707
|
-
# coffee =
|
708
|
-
#
|
709
|
-
#
|
710
|
-
#
|
711
|
-
#
|
712
|
-
#
|
713
|
-
#
|
714
|
-
#
|
715
|
-
#
|
716
|
-
#
|
448
|
+
# coffee = <<~EOY
|
449
|
+
# Coffee:
|
450
|
+
# Roast:
|
451
|
+
# - Light
|
452
|
+
# - Medium
|
453
|
+
# - Dark
|
454
|
+
# Style:
|
455
|
+
# - Cappuccino
|
456
|
+
# - Latte
|
457
|
+
# - Mocha
|
717
458
|
# EOY
|
718
459
|
#
|
719
460
|
# parser = Psychgus.parser(stylers: CoffeeStyler.new)
|
@@ -749,7 +490,7 @@ module Psychgus
|
|
749
490
|
#
|
750
491
|
# Private methods of Psych are not defined.
|
751
492
|
#
|
752
|
-
# @note For devs/hacking: because extend is used, do not prefix methods with
|
493
|
+
# @note For devs/hacking: because extend is used, do not prefix methods with `self.`.
|
753
494
|
###
|
754
495
|
module PsychDropIn
|
755
496
|
# @see Psych.add_builtin_type
|