yps 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43d89afcf4628612d4c00e6d902eb95bd92757e755b79297c72d73eac901ad53
4
- data.tar.gz: e55453abcb53201a61704a790966ea0f7765e75e40856cf91f0365acd355a729
3
+ metadata.gz: bcd62aefae6b26fa2093fc9c156c016a52a60d9c8d8d91898901031cfc283580
4
+ data.tar.gz: 51d5d6b8ffa294355bbdf66d6d382931db6f8e8737bd014653bde7f5b1e87421
5
5
  SHA512:
6
- metadata.gz: 29070662eff8e6b0d3210d444e88466b6c8fed647c0ca9742d1b4ca5196a195b90740d154836d8439eec89117fbcbfd99f1a67bab6d627c18508a7add5e754d1
7
- data.tar.gz: aa948cb48fac9b1b83b306c093fa8df9c573b676928a00848fd7e8c03a012fab040f2829b2e2ac584ee103ff3018be9e22c2f34645e985606a67a3731bd45bc6
6
+ metadata.gz: 9dfe215b77ff8502f76ffe904bd7bf350fd0f38c1ff6ba75ed97feba8cb2600d19830dcf4912fcbc97d7ada69d2cc365971acde95052760ebcf94fa957da8399
7
+ data.tar.gz: b48ee98f7435e2fa1991b1a607833ee873a841d96b8056c74256103a32206e4e943979df9fc6d4a4365764f6fff092f9cc9ad7b51ae9036ee2ecb3977c03c1b3
data/README.md CHANGED
@@ -28,11 +28,25 @@ gem install yps
28
28
 
29
29
  You can use the methods below to load a YAML code into Ruby objects with their position information (file name, line, and column).
30
30
 
31
- * `YPS.safe_load`/`YPS.load`
31
+ * `YPS.safe_load`/`YPS.load`/`YPS.safe_load_stram`/`YPS.load_stream`
32
32
  * Load the given YAML string into Ruby objects with position information.
33
- * `YPS.safe_load_file`/`YPS.load_file`
33
+ * `YPS.safe_load_file`/`YPS.load_file`/`YPS.safe_load_stram_file`/`YPS.load_stream_file`
34
34
  * Load the YAML code read from the given file path into Ruby objects with position information.
35
35
 
36
+ For a YAML code that contains multiple documents, folowing methods load the 1st document only.
37
+
38
+ * `YPS.safe_load`
39
+ * `YPS.load`
40
+ * `YPS.safe_load_file`
41
+ * `YPS.load_file`
42
+
43
+ On the other hand, following methods load all given documents and return them as a list.
44
+
45
+ * `YPS.safe_load_stram`
46
+ * `YPS.load_stream`
47
+ * `YPS.safe_load_stram_file`
48
+ * `YPS.load_stream_file`
49
+
36
50
  Parsed objects, except for hash keys, have their own position information.
37
51
  You can use the `position` method to get position information in the original YAML of the receiver object.
38
52
 
@@ -57,6 +71,25 @@ yaml['children'].each do |child|
57
71
  puts "#{key}: #{value} (#{value.position})"
58
72
  end
59
73
  end
74
+
75
+ yaml = YPS.load_stream(<<~'YAML')
76
+ - 0
77
+ - 1
78
+ ---
79
+ - foo
80
+ - bar
81
+ YAML
82
+
83
+ # output
84
+ # 0 (filename: unknown line 1 column 3)
85
+ # 1 (filename: unknown line 2 column 3)
86
+ # foo (filename: unknown line 4 column 3)
87
+ # bar (filename: unknown line 5 column 3)
88
+ yaml.each do |list|
89
+ list.each do |item|
90
+ puts "#{item} (#{item.position})"
91
+ end
92
+ end
60
93
  ```
61
94
 
62
95
  ## Contributing
data/lib/yps/parser.rb CHANGED
@@ -25,6 +25,10 @@ module YPS # :nodoc: all
25
25
  end
26
26
 
27
27
  class Parser < Psych::Parser
28
+ def self.parse(yaml, filename, &)
29
+ new(&).parse(yaml, filename)
30
+ end
31
+
28
32
  def initialize(&)
29
33
  super(Handler.new(&))
30
34
  end
data/lib/yps/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  module YPS
4
4
  # The version of YPS
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YPS # :nodoc: all
4
- module Visitors
4
+ module Visitor
5
5
  using NodeExtension
6
6
 
7
7
  module Common
@@ -33,5 +33,22 @@ module YPS # :nodoc: all
33
33
  class NoAliasRuby < Psych::Visitors::NoAliasRuby
34
34
  include Common
35
35
  end
36
+
37
+ def self.create( # rubocop:disable Metrics/ParameterLists
38
+ permitted_classes, permitted_symbols, aliases,
39
+ symbolize_names, freeze, strict_integer, value_class
40
+ )
41
+ class_loader = Psych::ClassLoader::Restricted.new(
42
+ permitted_classes.map(&:to_s), permitted_symbols.map(&:to_s)
43
+ )
44
+ scanner =
45
+ if RUBY_VERSION >= '3.2.0'
46
+ Psych::ScalarScanner.new(class_loader, strict_integer:)
47
+ else
48
+ Psych::ScalarScanner.new(class_loader)
49
+ end
50
+ (aliases && ToRuby || NoAliasRuby)
51
+ .new(scanner, class_loader, value_class, symbolize_names:, freeze:)
52
+ end
36
53
  end
37
54
  end
data/lib/yps.rb CHANGED
@@ -7,7 +7,7 @@ require_relative 'yps/version'
7
7
  require_relative 'yps/value'
8
8
  require_relative 'yps/node_extension'
9
9
  require_relative 'yps/parser'
10
- require_relative 'yps/visitors'
10
+ require_relative 'yps/visitor'
11
11
 
12
12
  ##
13
13
  # = YPS: YAML Positioning Sysmte
@@ -22,6 +22,8 @@ module YPS
22
22
  # Safely load the YAML string in +yaml+ and add position information (file name line and column)
23
23
  # to each parsed objects except for hash keys.
24
24
  #
25
+ # Load the 1st documetns only if the given YAML contains multiple documents.
26
+ #
25
27
  # Parsed objects will be wrapped by YPS::Value class to add the accessor returning the position information.
26
28
  # You can use the +value_class+ to specify your own wrapper class.
27
29
  #
@@ -59,27 +61,16 @@ module YPS
59
61
  filename: nil, fallback: nil, symbolize_names: false, freeze: false,
60
62
  strict_integer: false, value_class: Value
61
63
  )
62
- result = parse(yaml, filename)
63
- return fallback unless result
64
-
65
- class_loader =
66
- Psych::ClassLoader::Restricted.new(
67
- permitted_classes.map(&:to_s), permitted_symbols.map(&:to_s)
68
- )
69
- scanner =
70
- if RUBY_VERSION >= '3.2.0'
71
- Psych::ScalarScanner.new(class_loader, strict_integer:)
72
- else
73
- Psych::ScalarScanner.new(class_loader)
74
- end
75
- visitor =
76
- if aliases
77
- Visitors::ToRuby.new(scanner, class_loader, value_class, symbolize_names:, freeze:)
78
- else
79
- Visitors::NoAliasRuby.new(scanner, class_loader, value_class, symbolize_names:, freeze:)
80
- end
64
+ Parser.parse(yaml, filename) do |node|
65
+ visitor =
66
+ Visitor.create(
67
+ permitted_classes, permitted_symbols, aliases,
68
+ symbolize_names, freeze, strict_integer, value_class
69
+ )
70
+ return visitor.accept(node)
71
+ end
81
72
 
82
- visitor.accept(result)
73
+ fallback
83
74
  end
84
75
 
85
76
  ##
@@ -95,9 +86,7 @@ module YPS
95
86
  #
96
87
  # See also YPS.safe_load
97
88
  def safe_load_file(filename, **kwargs)
98
- File.open(filename, 'r:bom|utf-8') do |f|
99
- safe_load(f, filename:, **kwargs)
100
- end
89
+ open_file(filename) { |f| safe_load(f, filename:, **kwargs) }
101
90
  end
102
91
 
103
92
  ##
@@ -105,19 +94,67 @@ module YPS
105
94
  #
106
95
  # See also YPS.load
107
96
  def load_file(filename, **kwargs)
108
- File.open(filename, 'r:bom|utf-8') do |f|
109
- load(f, filename:, **kwargs)
97
+ open_file(filename) { |f| load(f, filename:, **kwargs) }
98
+ end
99
+
100
+ DEFAULT_VALUE = Object.new.freeze
101
+ private_constant :DEFAULT_VALUE
102
+
103
+ ##
104
+ # Similar to +YPS.safe_load+, but load all documents given in +yaml+ and return them as a list.
105
+ #
106
+ # See also YPS.safe_load
107
+ def safe_load_stream( # rubocop:disable Metrics/ParameterLists
108
+ yaml,
109
+ permitted_classes: [], permitted_symbols: [], aliases: false,
110
+ filename: nil, fallback: DEFAULT_VALUE, symbolize_names: false,
111
+ freeze: false, strict_integer: false, value_class: Value
112
+ )
113
+ visitor = nil
114
+ results = []
115
+ Parser.parse(yaml, filename) do |node|
116
+ visitor ||=
117
+ Visitor.create(
118
+ permitted_classes, permitted_symbols, aliases,
119
+ symbolize_names, freeze, strict_integer, value_class
120
+ )
121
+ results << visitor.accept(node)
110
122
  end
123
+ return fallback if results.empty? && !fallback.equal?(DEFAULT_VALUE)
124
+
125
+ results
111
126
  end
112
127
 
113
- private
128
+ ##
129
+ # Similar to +YPS.safe_load_sream+, but Symbol is allowed to be loaded by default.
130
+ #
131
+ # See also YPS.safe_load_stream
132
+ def load_stream(yaml, permitted_classes: [Symbol], **kwargs)
133
+ safe_load_stream(yaml, permitted_classes:, **kwargs)
134
+ end
114
135
 
115
- def parse(yaml, filename)
116
- Parser
117
- .new { |node| return node }
118
- .parse(yaml, filename)
136
+ ##
137
+ # Similar to +YPS.safe_load_stream+,
138
+ # but the YAML string is read from the file specified by the +filename+ argument.
139
+ #
140
+ # See also YPS.safe_load_stream
141
+ def safe_load_stream_file(filename, **kwargs)
142
+ open_file(filename) { |f| safe_load_stream(f, filename:, **kwargs) }
143
+ end
144
+
145
+ ##
146
+ # Similar to +YPS.load_stream+,
147
+ # but the YAML string is read from the file specified by the +filename+ argument.
148
+ #
149
+ # See also YPS.load_stream
150
+ def load_stream_file(filename, **kwargs)
151
+ open_file(filename) { |f| load_stream(f, filename:, **kwargs) }
152
+ end
153
+
154
+ private
119
155
 
120
- false
156
+ def open_file(filename, &)
157
+ File.open(filename, 'r:bom|utf-8', &)
121
158
  end
122
159
  end
123
160
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taichi Ishitani
@@ -29,14 +29,17 @@ files:
29
29
  - lib/yps/parser.rb
30
30
  - lib/yps/value.rb
31
31
  - lib/yps/version.rb
32
- - lib/yps/visitors.rb
32
+ - lib/yps/visitor.rb
33
33
  homepage: https://github.com/taichi-ishitani/yps
34
34
  licenses:
35
35
  - MIT
36
36
  metadata:
37
+ bug_tracker_uri: https://github.com/taichi-ishitani/yps/issues
38
+ changelog_uri: https://github.com/taichi-ishitani/yps/releases
39
+ documentation_uri: https://taichi-ishitani.github.io/yps/
37
40
  homepage_uri: https://github.com/taichi-ishitani/yps
38
- source_code_uri: https://github.com/taichi-ishitani/yps
39
41
  rubygems_mfa_required: 'true'
42
+ source_code_uri: https://github.com/taichi-ishitani/yps
40
43
  rdoc_options:
41
44
  - "--main"
42
45
  - README.md