yps 0.1.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 +7 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +77 -0
- data/lib/yps/node_extension.rb +17 -0
- data/lib/yps/parser.rb +37 -0
- data/lib/yps/value.rb +70 -0
- data/lib/yps/version.rb +6 -0
- data/lib/yps/visitors.rb +37 -0
- data/lib/yps.rb +123 -0
- metadata +63 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 43d89afcf4628612d4c00e6d902eb95bd92757e755b79297c72d73eac901ad53
|
|
4
|
+
data.tar.gz: e55453abcb53201a61704a790966ea0f7765e75e40856cf91f0365acd355a729
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 29070662eff8e6b0d3210d444e88466b6c8fed647c0ca9742d1b4ca5196a195b90740d154836d8439eec89117fbcbfd99f1a67bab6d627c18508a7add5e754d1
|
|
7
|
+
data.tar.gz: aa948cb48fac9b1b83b306c093fa8df9c573b676928a00848fd7e8c03a012fab040f2829b2e2ac584ee103ff3018be9e22c2f34645e985606a67a3731bd45bc6
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Taichi Ishitani
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
[](https://badge.fury.io/rb/yps)
|
|
2
|
+
[](https://github.com/taichi-ishitani/yps/actions/workflows/ci.yml)
|
|
3
|
+
[](https://qlty.sh/gh/taichi-ishitani/projects/yps)
|
|
4
|
+
[](https://codecov.io/gh/taichi-ishitani/yps)
|
|
5
|
+
|
|
6
|
+
[](https://ko-fi.com/A0A231E3I)
|
|
7
|
+
|
|
8
|
+
# YPS: YAML Positioning System
|
|
9
|
+
|
|
10
|
+
YPS is a gem to parse YAML and add position information (file name, line and column) to each parsed elements.
|
|
11
|
+
This is useful for error reporting and debugging, allowing developers to precisely locate an issue within the original YAML file.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
bundle add yps
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
gem install yps
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
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).
|
|
30
|
+
|
|
31
|
+
* `YPS.safe_load`/`YPS.load`
|
|
32
|
+
* Load the given YAML string into Ruby objects with position information.
|
|
33
|
+
* `YPS.safe_load_file`/`YPS.load_file`
|
|
34
|
+
* Load the YAML code read from the given file path into Ruby objects with position information.
|
|
35
|
+
|
|
36
|
+
Parsed objects, except for hash keys, have their own position information.
|
|
37
|
+
You can use the `position` method to get position information in the original YAML of the receiver object.
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
require 'yps'
|
|
41
|
+
|
|
42
|
+
yaml = YPS.load(<<~'YAML')
|
|
43
|
+
children:
|
|
44
|
+
- name: kanta
|
|
45
|
+
age: 8
|
|
46
|
+
- name: kaede
|
|
47
|
+
age: 3
|
|
48
|
+
YAML
|
|
49
|
+
|
|
50
|
+
# output
|
|
51
|
+
# name: kanta (filename: unknown line 2 column 11)
|
|
52
|
+
# age: 8 (filename: unknown line 3 column 10)
|
|
53
|
+
# name: kaede (filename: unknown line 4 column 11)
|
|
54
|
+
# age: 3 (filename: unknown line 5 column 10)
|
|
55
|
+
yaml['children'].each do |child|
|
|
56
|
+
child.each do |key, value|
|
|
57
|
+
puts "#{key}: #{value} (#{value.position})"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Contributing
|
|
63
|
+
|
|
64
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/taichi-ishitani/yps.
|
|
65
|
+
|
|
66
|
+
* [Issue Tracker](https://github.com/taichi-ishitani/yps/issues)
|
|
67
|
+
* [Pull Requesst](https://github.com/taichi-ishitani/yps/pulls)
|
|
68
|
+
* [Discussion](https://github.com/taichi-ishitani/yps/discussions)
|
|
69
|
+
|
|
70
|
+
## Copyright & License
|
|
71
|
+
|
|
72
|
+
Copyright © 2025 Taichi Ishitani.
|
|
73
|
+
YPS is licensed under the terms of the [MIT License](https://opensource.org/licenses/MIT), see [LICENSE.txt](LICENSE.txt) for further details.
|
|
74
|
+
|
|
75
|
+
## Code of Conduct
|
|
76
|
+
|
|
77
|
+
Everyone interacting in the YPS project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/taichi-ishitani/yps/blob/master/CODE_OF_CONDUCT.md).
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module YPS # :nodoc: all
|
|
4
|
+
module NodeExtension
|
|
5
|
+
refine Psych::Nodes::Node do
|
|
6
|
+
attr_accessor :filename
|
|
7
|
+
|
|
8
|
+
def mapping_key?
|
|
9
|
+
@mapping_key || false
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def mapping_key
|
|
13
|
+
@mapping_key = true
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/yps/parser.rb
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module YPS # :nodoc: all
|
|
4
|
+
using NodeExtension
|
|
5
|
+
|
|
6
|
+
class Handler < Psych::Handlers::DocumentStream
|
|
7
|
+
attr_accessor :filename
|
|
8
|
+
|
|
9
|
+
def scalar(...)
|
|
10
|
+
node = super
|
|
11
|
+
|
|
12
|
+
# The given value was added to @last.children as a mappking key inside super.
|
|
13
|
+
# Therefore, it is a mappking key if size of @last.children is odd.
|
|
14
|
+
if @last.mapping? && @last.children.size.odd?
|
|
15
|
+
node.mapping_key
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
node
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def set_start_location(node) # rubocop:disable Naming/AccessorMethodName
|
|
22
|
+
super
|
|
23
|
+
node.filename = filename
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class Parser < Psych::Parser
|
|
28
|
+
def initialize(&)
|
|
29
|
+
super(Handler.new(&))
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def parse(yaml, filename)
|
|
33
|
+
@handler.filename = filename
|
|
34
|
+
super
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
data/lib/yps/value.rb
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module YPS
|
|
4
|
+
##
|
|
5
|
+
# Position is a placeholder retaining position information of a parsed object.
|
|
6
|
+
#
|
|
7
|
+
# Fields:
|
|
8
|
+
# +filename+::
|
|
9
|
+
# File name of the original YAML file.
|
|
10
|
+
# +line+::
|
|
11
|
+
# Line number in the original YAML string where a parsed object is started.
|
|
12
|
+
# +column+::
|
|
13
|
+
# Column number in the original YAML string where a parsed object is started.
|
|
14
|
+
class Position
|
|
15
|
+
def initialize(filename, line, column) # :nodoc:
|
|
16
|
+
@filename = filename
|
|
17
|
+
@line = line
|
|
18
|
+
@column = column
|
|
19
|
+
freeze
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
##
|
|
23
|
+
# Accessor for the filename of the orignal YAML file
|
|
24
|
+
attr_reader :filename
|
|
25
|
+
|
|
26
|
+
##
|
|
27
|
+
# Accessor for the line number where the parsed object is started.
|
|
28
|
+
attr_reader :line
|
|
29
|
+
|
|
30
|
+
##
|
|
31
|
+
# Accessor for the column number where the parsed object is started.
|
|
32
|
+
attr_reader :column
|
|
33
|
+
|
|
34
|
+
##
|
|
35
|
+
# Return a string representing the position information.
|
|
36
|
+
def to_s
|
|
37
|
+
"filename: #{filename || 'unknown'} line #{line} column #{column}"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
##
|
|
41
|
+
# Equality operator.
|
|
42
|
+
# Check whether or not self and +other+ point the same position.
|
|
43
|
+
def ==(other)
|
|
44
|
+
filename == other.filename && line == other.line && column == other.column
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
##
|
|
49
|
+
# Value is a wrapper class for a parsed object and serves two main functions:
|
|
50
|
+
#
|
|
51
|
+
# 1. As a placeholder and accessor for the position information of
|
|
52
|
+
# the wrapped object (via the #position method).
|
|
53
|
+
# 2. Forwarding received method calls to the wrapped object.
|
|
54
|
+
class Value < SimpleDelegator
|
|
55
|
+
def initialize(value, position) # :nodoc:
|
|
56
|
+
super(value)
|
|
57
|
+
@position = position
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
##
|
|
61
|
+
# Accessor for the wrapped object
|
|
62
|
+
def value
|
|
63
|
+
__getobj__
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
##
|
|
67
|
+
# Accessor for the position information of the wrapped object
|
|
68
|
+
attr_reader :position
|
|
69
|
+
end
|
|
70
|
+
end
|
data/lib/yps/version.rb
ADDED
data/lib/yps/visitors.rb
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module YPS # :nodoc: all
|
|
4
|
+
module Visitors
|
|
5
|
+
using NodeExtension
|
|
6
|
+
|
|
7
|
+
module Common
|
|
8
|
+
def initialize(scanner, class_loader, value_class, symbolize_names:, freeze:)
|
|
9
|
+
super(scanner, class_loader, symbolize_names:, freeze:)
|
|
10
|
+
@value_class = value_class
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def accept(node)
|
|
14
|
+
object = super
|
|
15
|
+
create_wrapped_object(object, node)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def create_wrapped_object(object, node)
|
|
21
|
+
return object if node.document? || node.mapping_key?
|
|
22
|
+
|
|
23
|
+
pos = Position.new(node.filename, node.start_line + 1, node.start_column + 1)
|
|
24
|
+
obj = @value_class.new(object, pos)
|
|
25
|
+
@freeze && obj.freeze || obj
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class ToRuby < Psych::Visitors::ToRuby
|
|
30
|
+
include Common
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class NoAliasRuby < Psych::Visitors::NoAliasRuby
|
|
34
|
+
include Common
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
data/lib/yps.rb
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'delegate'
|
|
4
|
+
require 'psych'
|
|
5
|
+
|
|
6
|
+
require_relative 'yps/version'
|
|
7
|
+
require_relative 'yps/value'
|
|
8
|
+
require_relative 'yps/node_extension'
|
|
9
|
+
require_relative 'yps/parser'
|
|
10
|
+
require_relative 'yps/visitors'
|
|
11
|
+
|
|
12
|
+
##
|
|
13
|
+
# = YPS: YAML Positioning Sysmte
|
|
14
|
+
#
|
|
15
|
+
# YPS is a gem to parse YAML and
|
|
16
|
+
# add position information (file name, line and column) to each parsed elements.
|
|
17
|
+
# This is useful for error reporting and debugging,
|
|
18
|
+
# allowing developers to precisely locate an issue within the original YAML file.
|
|
19
|
+
module YPS
|
|
20
|
+
class << self
|
|
21
|
+
##
|
|
22
|
+
# Safely load the YAML string in +yaml+ and add position information (file name line and column)
|
|
23
|
+
# to each parsed objects except for hash keys.
|
|
24
|
+
#
|
|
25
|
+
# Parsed objects will be wrapped by YPS::Value class to add the accessor returning the position information.
|
|
26
|
+
# You can use the +value_class+ to specify your own wrapper class.
|
|
27
|
+
#
|
|
28
|
+
# Classes which are allowed to be loaded by default are same
|
|
29
|
+
# as the Psych.safe_load[https://docs.ruby-lang.org/en/master/Psych.html#method-c-safe_load] method.
|
|
30
|
+
#
|
|
31
|
+
# Arguments:
|
|
32
|
+
# +yaml+::
|
|
33
|
+
# String or IO object containing the YAML string to be parsed.
|
|
34
|
+
# +permitted_classes+::
|
|
35
|
+
# Array containing additional classes allowed to be loaded.
|
|
36
|
+
# +permitted_symbols+::
|
|
37
|
+
# Array containing Symbols allowed to be loaded. By default, any symbol can be loaded.
|
|
38
|
+
# +aliases+::
|
|
39
|
+
# Aliases can be used if set to true. By default, aliases are not allowed.
|
|
40
|
+
# +filename+::
|
|
41
|
+
# File name string which will be added to the position information of each parsed object.
|
|
42
|
+
# +fallback+::
|
|
43
|
+
# An object which will be returned when an empty YAML string is given.
|
|
44
|
+
# +symbolize_names+::
|
|
45
|
+
# All hash keys will be symbolized if set to true.
|
|
46
|
+
# +freeze+::
|
|
47
|
+
# All parsed objects will be frozen if set to true.
|
|
48
|
+
# +strict_integer+::
|
|
49
|
+
# Integer literals are not allowed to include commas ',' if set to true.
|
|
50
|
+
# Such literals will be parsed as String objects.
|
|
51
|
+
# For Ruby 3.1, this option is ignored.
|
|
52
|
+
# +value_class+::
|
|
53
|
+
# Specify a class wrapping parsed objects. By default, YPS::Value is used.
|
|
54
|
+
#
|
|
55
|
+
# See also Psych.safe_load[https://docs.ruby-lang.org/en/master/Psych.html#method-c-safe_load].
|
|
56
|
+
def safe_load( # rubocop:disable Metrics/ParameterLists
|
|
57
|
+
yaml,
|
|
58
|
+
permitted_classes: [], permitted_symbols: [], aliases: false,
|
|
59
|
+
filename: nil, fallback: nil, symbolize_names: false, freeze: false,
|
|
60
|
+
strict_integer: false, value_class: Value
|
|
61
|
+
)
|
|
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
|
|
81
|
+
|
|
82
|
+
visitor.accept(result)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
##
|
|
86
|
+
# Similar to +YPS.safe_load+, but Symbol is allowed to be loaded by default.
|
|
87
|
+
#
|
|
88
|
+
# See also Psych.load[https://docs.ruby-lang.org/en/master/Psych.html#method-c-load].
|
|
89
|
+
def load(yaml, permitted_classes: [Symbol], **kwargs)
|
|
90
|
+
safe_load(yaml, permitted_classes:, **kwargs)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
##
|
|
94
|
+
# Similar to +YPS.safe_load+, but the YAML string is read from the file specified by the +filename+ argument.
|
|
95
|
+
#
|
|
96
|
+
# See also YPS.safe_load
|
|
97
|
+
def safe_load_file(filename, **kwargs)
|
|
98
|
+
File.open(filename, 'r:bom|utf-8') do |f|
|
|
99
|
+
safe_load(f, filename:, **kwargs)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
##
|
|
104
|
+
# Similar to +YPS.load+, but the YAML string is read from the file specified by the +filename+ argument.
|
|
105
|
+
#
|
|
106
|
+
# See also YPS.load
|
|
107
|
+
def load_file(filename, **kwargs)
|
|
108
|
+
File.open(filename, 'r:bom|utf-8') do |f|
|
|
109
|
+
load(f, filename:, **kwargs)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
private
|
|
114
|
+
|
|
115
|
+
def parse(yaml, filename)
|
|
116
|
+
Parser
|
|
117
|
+
.new { |node| return node }
|
|
118
|
+
.parse(yaml, filename)
|
|
119
|
+
|
|
120
|
+
false
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: yps
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Taichi Ishitani
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: YPS is a gem to parse YAML and add position information (file name, line
|
|
13
|
+
and column) to each parsed elements. This is useful for error reporting and debugging,
|
|
14
|
+
allowing developers to precisely locate an issue within the original YAML file.
|
|
15
|
+
email:
|
|
16
|
+
- taichi730@gmail.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files:
|
|
20
|
+
- CODE_OF_CONDUCT.md
|
|
21
|
+
- LICENSE.txt
|
|
22
|
+
- README.md
|
|
23
|
+
files:
|
|
24
|
+
- CODE_OF_CONDUCT.md
|
|
25
|
+
- LICENSE.txt
|
|
26
|
+
- README.md
|
|
27
|
+
- lib/yps.rb
|
|
28
|
+
- lib/yps/node_extension.rb
|
|
29
|
+
- lib/yps/parser.rb
|
|
30
|
+
- lib/yps/value.rb
|
|
31
|
+
- lib/yps/version.rb
|
|
32
|
+
- lib/yps/visitors.rb
|
|
33
|
+
homepage: https://github.com/taichi-ishitani/yps
|
|
34
|
+
licenses:
|
|
35
|
+
- MIT
|
|
36
|
+
metadata:
|
|
37
|
+
homepage_uri: https://github.com/taichi-ishitani/yps
|
|
38
|
+
source_code_uri: https://github.com/taichi-ishitani/yps
|
|
39
|
+
rubygems_mfa_required: 'true'
|
|
40
|
+
rdoc_options:
|
|
41
|
+
- "--main"
|
|
42
|
+
- README.md
|
|
43
|
+
- "--title"
|
|
44
|
+
- 'YPS: YAML Positioning System'
|
|
45
|
+
- "--show-hash"
|
|
46
|
+
- "--line-numbers"
|
|
47
|
+
require_paths:
|
|
48
|
+
- lib
|
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '3.1'
|
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '0'
|
|
59
|
+
requirements: []
|
|
60
|
+
rubygems_version: 3.7.2
|
|
61
|
+
specification_version: 4
|
|
62
|
+
summary: 'YPS: YAML Positioning System'
|
|
63
|
+
test_files: []
|