yps 0.2.0 → 1.0.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: bcd62aefae6b26fa2093fc9c156c016a52a60d9c8d8d91898901031cfc283580
4
- data.tar.gz: 51d5d6b8ffa294355bbdf66d6d382931db6f8e8737bd014653bde7f5b1e87421
3
+ metadata.gz: 0aaa3d8a3ac39f8b0890701d45db64be1d9bbdad6d95d67bdf5a0bd4cac4c841
4
+ data.tar.gz: 9ef25972d03876b137661e62d366641c2392fd47465f6628ab2c1c99f685be65
5
5
  SHA512:
6
- metadata.gz: 9dfe215b77ff8502f76ffe904bd7bf350fd0f38c1ff6ba75ed97feba8cb2600d19830dcf4912fcbc97d7ada69d2cc365971acde95052760ebcf94fa957da8399
7
- data.tar.gz: b48ee98f7435e2fa1991b1a607833ee873a841d96b8056c74256103a32206e4e943979df9fc6d4a4365764f6fff092f9cc9ad7b51ae9036ee2ecb3977c03c1b3
6
+ metadata.gz: 64f564d9d9e5509cccf28cbcb274f8bb2bab852b5e00d87954c11564cf22b20e9b24073a11b78a7ac6fa5d3a2255830245b42ac3b30cde8005c5441d28684048
7
+ data.tar.gz: 75f09a3bf9c5eab84240c14fd0a28d2386f3c58aa31a33b2cd4add9244a6e6184e64368aa65dfb27eefb1150e2d3d46c4cec1fddd6002356f5e45caed2975cae
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  # YPS: YAML Positioning System
9
9
 
10
- YPS is a gem to parse YAML and add position information (file name, line and column) to each parsed elements.
10
+ YPS is a gem that parses YAML and adds position information (file name, line, and column) to each parsed element.
11
11
  This is useful for error reporting and debugging, allowing developers to precisely locate an issue within the original YAML file.
12
12
 
13
13
  ## Installation
@@ -26,25 +26,32 @@ gem install yps
26
26
 
27
27
  ## Usage
28
28
 
29
- You can use the methods below to load a YAML code into Ruby objects with their position information (file name, line, and column).
29
+ You can use the methods below to load YAML content into Ruby objects with position information (file name, line, and column).
30
30
 
31
- * `YPS.safe_load`/`YPS.load`/`YPS.safe_load_stram`/`YPS.load_stream`
32
- * Load the given YAML string into Ruby objects with position information.
33
- * `YPS.safe_load_file`/`YPS.load_file`/`YPS.safe_load_stram_file`/`YPS.load_stream_file`
34
- * Load the YAML code read from the given file path into Ruby objects with position information.
31
+ * Load the given YAML string into Ruby objects with position information
32
+ * `YPS.safe_load`
33
+ * `YPS.load`
34
+ * `YPS.safe_load_stream`
35
+ * `YPS.load_stream`
35
36
 
36
- For a YAML code that contains multiple documents, folowing methods load the 1st document only.
37
+ * Load the YAML read from the given file path into Ruby objects with position information
38
+ * `YPS.safe_load_file`
39
+ * `YPS.load_file`
40
+ * `YPS.safe_load_stream_file`
41
+ * `YPS.load_stream_file`
42
+
43
+ For YAML that contains multiple documents, the following methods load only the first document.
37
44
 
38
45
  * `YPS.safe_load`
39
46
  * `YPS.load`
40
47
  * `YPS.safe_load_file`
41
48
  * `YPS.load_file`
42
49
 
43
- On the other hand, following methods load all given documents and return them as a list.
50
+ In contrast, the following methods load all documents and return them as a list.
44
51
 
45
- * `YPS.safe_load_stram`
52
+ * `YPS.safe_load_stream`
46
53
  * `YPS.load_stream`
47
- * `YPS.safe_load_stram_file`
54
+ * `YPS.safe_load_stream_file`
48
55
  * `YPS.load_stream_file`
49
56
 
50
57
  Parsed objects, except for hash keys, have their own position information.
@@ -92,12 +99,45 @@ yaml.each do |list|
92
99
  end
93
100
  ```
94
101
 
102
+ ### Handling of `false` and `nil` values
103
+
104
+ By default, all objects, including `false` and `nil`, are wrapped in a wrapper class.
105
+ Note that wrapped `false` and `nil` values will not be treated as falsy.
106
+ You can use the `unwrapped_classes` option to avoid this situation.
107
+ Objects belonging to classes specified by this option are returned unwrapped but will not have access to their position information.
108
+
109
+ ```ruby
110
+ yaml = YPS.load(<<~'YAML')
111
+ - false
112
+ - null
113
+ YAML
114
+
115
+ # output
116
+ # false
117
+ # nil
118
+ puts (yaml[0] || 'foo').inspect
119
+ puts (yaml[1] || 'bar').inspect
120
+
121
+ yaml = YPS.load(<<~'YAML', unwrapped_classes: [FalseClass, NilClass])
122
+ - false
123
+ - null
124
+ YAML
125
+
126
+ # output
127
+ # "foo"
128
+ # "bar"
129
+ puts (yaml[0] || 'foo').inspect
130
+ puts (yaml[1] || 'bar').inspect
131
+ ```
132
+
133
+ For more details about these APIs, please visit [here](https://taichi-ishitani.github.io/yps/).
134
+
95
135
  ## Contributing
96
136
 
97
137
  Bug reports and pull requests are welcome on GitHub at https://github.com/taichi-ishitani/yps.
98
138
 
99
139
  * [Issue Tracker](https://github.com/taichi-ishitani/yps/issues)
100
- * [Pull Requesst](https://github.com/taichi-ishitani/yps/pulls)
140
+ * [Pull Request](https://github.com/taichi-ishitani/yps/pulls)
101
141
  * [Discussion](https://github.com/taichi-ishitani/yps/discussions)
102
142
 
103
143
  ## Copyright & License
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.2.0'
5
+ VERSION = '1.0.0'
6
6
  end
data/lib/yps/visitor.rb CHANGED
@@ -5,25 +5,37 @@ module YPS # :nodoc: all
5
5
  using NodeExtension
6
6
 
7
7
  module Common
8
- def initialize(scanner, class_loader, value_class, symbolize_names:, freeze:)
8
+ def initialize( # rubocop:disable Metrics/ParameterLists
9
+ scanner, class_loader, unwrapped_classes, value_class,
10
+ symbolize_names:, freeze:
11
+ )
9
12
  super(scanner, class_loader, symbolize_names:, freeze:)
13
+ @unwrapped_classes = unwrapped_classes
10
14
  @value_class = value_class
11
15
  end
12
16
 
13
17
  def accept(node)
14
18
  object = super
15
- create_wrapped_object(object, node)
19
+ if unwrap?(object, node)
20
+ object
21
+ else
22
+ create_wrapped_object(object, node)
23
+ end
16
24
  end
17
25
 
18
26
  private
19
27
 
20
28
  def create_wrapped_object(object, node)
21
- return object if node.document? || node.mapping_key?
22
-
23
29
  pos = Position.new(node.filename, node.start_line + 1, node.start_column + 1)
24
30
  obj = @value_class.new(object, pos)
25
31
  @freeze && obj.freeze || obj
26
32
  end
33
+
34
+ def unwrap?(object, node)
35
+ node.document? ||
36
+ node.mapping_key? ||
37
+ @unwrapped_classes.any? { |klass| object.instance_of?(klass) }
38
+ end
27
39
  end
28
40
 
29
41
  class ToRuby < Psych::Visitors::ToRuby
@@ -35,8 +47,8 @@ module YPS # :nodoc: all
35
47
  end
36
48
 
37
49
  def self.create( # rubocop:disable Metrics/ParameterLists
38
- permitted_classes, permitted_symbols, aliases,
39
- symbolize_names, freeze, strict_integer, value_class
50
+ permitted_classes, permitted_symbols, unwrapped_classes,
51
+ aliases, symbolize_names, freeze, strict_integer, value_class
40
52
  )
41
53
  class_loader = Psych::ClassLoader::Restricted.new(
42
54
  permitted_classes.map(&:to_s), permitted_symbols.map(&:to_s)
@@ -48,7 +60,7 @@ module YPS # :nodoc: all
48
60
  Psych::ScalarScanner.new(class_loader)
49
61
  end
50
62
  (aliases && ToRuby || NoAliasRuby)
51
- .new(scanner, class_loader, value_class, symbolize_names:, freeze:)
63
+ .new(scanner, class_loader, unwrapped_classes, value_class, symbolize_names:, freeze:)
52
64
  end
53
65
  end
54
66
  end
data/lib/yps.rb CHANGED
@@ -37,6 +37,9 @@ module YPS
37
37
  # Array containing additional classes allowed to be loaded.
38
38
  # +permitted_symbols+::
39
39
  # Array containing Symbols allowed to be loaded. By default, any symbol can be loaded.
40
+ # +unwrapped_classes+::
41
+ # Array containing classes whose objects are not wrapped with the wrapper class.
42
+ # By default, all objects are wrapped.
40
43
  # +aliases+::
41
44
  # Aliases can be used if set to true. By default, aliases are not allowed.
42
45
  # +filename+::
@@ -57,15 +60,15 @@ module YPS
57
60
  # See also Psych.safe_load[https://docs.ruby-lang.org/en/master/Psych.html#method-c-safe_load].
58
61
  def safe_load( # rubocop:disable Metrics/ParameterLists
59
62
  yaml,
60
- permitted_classes: [], permitted_symbols: [], aliases: false,
61
- filename: nil, fallback: nil, symbolize_names: false, freeze: false,
62
- strict_integer: false, value_class: Value
63
+ permitted_classes: [], permitted_symbols: [], unwrapped_classes: [],
64
+ aliases: false, filename: nil, fallback: nil, symbolize_names: false,
65
+ freeze: false, strict_integer: false, value_class: Value
63
66
  )
64
67
  Parser.parse(yaml, filename) do |node|
65
68
  visitor =
66
69
  Visitor.create(
67
- permitted_classes, permitted_symbols, aliases,
68
- symbolize_names, freeze, strict_integer, value_class
70
+ permitted_classes, permitted_symbols, unwrapped_classes,
71
+ aliases, symbolize_names, freeze, strict_integer, value_class
69
72
  )
70
73
  return visitor.accept(node)
71
74
  end
@@ -106,8 +109,8 @@ module YPS
106
109
  # See also YPS.safe_load
107
110
  def safe_load_stream( # rubocop:disable Metrics/ParameterLists
108
111
  yaml,
109
- permitted_classes: [], permitted_symbols: [], aliases: false,
110
- filename: nil, fallback: DEFAULT_VALUE, symbolize_names: false,
112
+ permitted_classes: [], permitted_symbols: [], unwrapped_classes: [],
113
+ aliases: false, filename: nil, fallback: DEFAULT_VALUE, symbolize_names: false,
111
114
  freeze: false, strict_integer: false, value_class: Value
112
115
  )
113
116
  visitor = nil
@@ -115,8 +118,8 @@ module YPS
115
118
  Parser.parse(yaml, filename) do |node|
116
119
  visitor ||=
117
120
  Visitor.create(
118
- permitted_classes, permitted_symbols, aliases,
119
- symbolize_names, freeze, strict_integer, value_class
121
+ permitted_classes, permitted_symbols, unwrapped_classes,
122
+ aliases, symbolize_names, freeze, strict_integer, value_class
120
123
  )
121
124
  results << visitor.accept(node)
122
125
  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.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taichi Ishitani
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  requirements: []
63
- rubygems_version: 3.7.2
63
+ rubygems_version: 4.0.3
64
64
  specification_version: 4
65
65
  summary: 'YPS: YAML Positioning System'
66
66
  test_files: []