lutaml-path 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/main.yml +27 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +10 -0
- data/.rubocop_todo.yml +25 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/Gemfile +13 -0
- data/README.adoc +153 -0
- data/Rakefile +12 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/lib/lutaml/path/element_path.rb +25 -0
- data/lib/lutaml/path/parser.rb +44 -0
- data/lib/lutaml/path/path_segment.rb +24 -0
- data/lib/lutaml/path/transformer.rb +42 -0
- data/lib/lutaml/path/version.rb +7 -0
- data/lib/lutaml/path.rb +21 -0
- data/lutaml-path.gemspec +33 -0
- data/sig/lutaml/path.rbs +6 -0
- data/spec/lutaml/path_spec.rb +88 -0
- data/spec/spec_helper.rb +15 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 35c6031869535e5000b1a97f6f19fcb33427eff85871c391a0c9923ed799f0d5
|
4
|
+
data.tar.gz: 1a2c3b642aa03c059228ccd75400d87adf4d5410c170e008fe9151ec6f31422c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1e24edbb6013b5375f0f91506b4b8c710f7a20c33c9d64ed2a6a0411f61c5a340b5a96096ae4a20dccddc5eb129a0bf0ea8f49e311fc91ee75c2c6aa011942dd
|
7
|
+
data.tar.gz: 82595300ac867fe4dead6f7638af14ffe61a642f0c0979a9e7d1f2eed1e7c2f8e459f8db4715d4bf724da44f9d6ede030bad1c56ac4f1f706a197a47475a181b
|
@@ -0,0 +1,27 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- main
|
7
|
+
|
8
|
+
pull_request:
|
9
|
+
|
10
|
+
jobs:
|
11
|
+
build:
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
name: Ruby ${{ matrix.ruby }}
|
14
|
+
strategy:
|
15
|
+
matrix:
|
16
|
+
ruby:
|
17
|
+
- '3.3.2'
|
18
|
+
|
19
|
+
steps:
|
20
|
+
- uses: actions/checkout@v4
|
21
|
+
- name: Set up Ruby
|
22
|
+
uses: ruby/setup-ruby@v1
|
23
|
+
with:
|
24
|
+
ruby-version: ${{ matrix.ruby }}
|
25
|
+
bundler-cache: true
|
26
|
+
- name: Run the default task
|
27
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2024-11-12 03:37:16 UTC using RuboCop version 1.68.0.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 2
|
10
|
+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
|
11
|
+
# AllowedMethods: refine
|
12
|
+
Metrics/BlockLength:
|
13
|
+
Max: 69
|
14
|
+
|
15
|
+
# Offense count: 5
|
16
|
+
# Configuration parameters: AllowedConstants.
|
17
|
+
Style/Documentation:
|
18
|
+
Exclude:
|
19
|
+
- 'spec/**/*'
|
20
|
+
- 'test/**/*'
|
21
|
+
- 'lib/lutaml/path.rb'
|
22
|
+
- 'lib/lutaml/path/element_path.rb'
|
23
|
+
- 'lib/lutaml/path/parser.rb'
|
24
|
+
- 'lib/lutaml/path/path_segment.rb'
|
25
|
+
- 'lib/lutaml/path/transformer.rb'
|
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/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in lutaml-model.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
gem "rspec", "~> 3.0"
|
10
|
+
gem "rubocop", "~> 1.21"
|
11
|
+
gem "rubocop-performance", require: false
|
12
|
+
gem "rubocop-rake", require: false
|
13
|
+
gem "rubocop-rspec", require: false
|
data/README.adoc
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
= LutaML Path
|
2
|
+
:source-highlighter: highlight.js
|
3
|
+
:toc: macro
|
4
|
+
|
5
|
+
image:https://github.com/lutaml/lutaml-path/workflows/build/badge.svg["Build Status", link="https://github.com/lutaml/lutaml-path/actions?workflow=build"]
|
6
|
+
image:https://img.shields.io/gem/v/lutaml-path.svg["Gem Version", link="https://rubygems.org/gems/lutaml-path"]
|
7
|
+
|
8
|
+
toc::[]
|
9
|
+
|
10
|
+
== What is LutaML Path?
|
11
|
+
|
12
|
+
LutaML Path provides a parser for path expressions that reference elements
|
13
|
+
within UML models. It implements a path notation similar to the Object
|
14
|
+
Constraint Language (OCL) to locate UML model elements across package
|
15
|
+
hierarchies.
|
16
|
+
|
17
|
+
This gem is specifically designed to work with OMG UML models and supports
|
18
|
+
referencing any UML element including packages, classes, interfaces, properties,
|
19
|
+
and operations.
|
20
|
+
|
21
|
+
== How to install
|
22
|
+
|
23
|
+
[source,ruby]
|
24
|
+
----
|
25
|
+
gem install lutaml-path
|
26
|
+
----
|
27
|
+
|
28
|
+
Or add this line to your application's Gemfile:
|
29
|
+
|
30
|
+
[source,ruby]
|
31
|
+
----
|
32
|
+
gem 'lutaml-path'
|
33
|
+
----
|
34
|
+
|
35
|
+
== Basic usage
|
36
|
+
|
37
|
+
The path syntax follows UML namespace conventions using `::` as a separator:
|
38
|
+
|
39
|
+
[source,ruby]
|
40
|
+
----
|
41
|
+
require 'lutaml/path'
|
42
|
+
|
43
|
+
# Simple element reference
|
44
|
+
path = Lutaml::Path.parse("Package::Class")
|
45
|
+
|
46
|
+
# Absolute path (starts from root namespace)
|
47
|
+
path = Lutaml::Path.parse("::Root::Package::Class")
|
48
|
+
|
49
|
+
# Path with wildcards
|
50
|
+
path = Lutaml::Path.parse("Package::*::BaseClass*")
|
51
|
+
----
|
52
|
+
|
53
|
+
== Working with patterns
|
54
|
+
|
55
|
+
The parser supports several kinds of patterns:
|
56
|
+
|
57
|
+
* `*` - matches any sequence of characters
|
58
|
+
* `?` - matches any single character
|
59
|
+
* `[abc]` - matches any character in the set
|
60
|
+
* `{pattern1,pattern2}` - matches any of the comma-separated patterns
|
61
|
+
|
62
|
+
Examples:
|
63
|
+
|
64
|
+
[source,ruby]
|
65
|
+
----
|
66
|
+
# Match any class starting with "Base"
|
67
|
+
path = Lutaml::Path.parse("Base*")
|
68
|
+
|
69
|
+
# Match specific character patterns
|
70
|
+
path = Lutaml::Path.parse("Package::[A-Z]*::Interface")
|
71
|
+
|
72
|
+
# Match multiple alternatives
|
73
|
+
path = Lutaml::Path.parse("model::{Abstract,Base}Class")
|
74
|
+
----
|
75
|
+
|
76
|
+
== How to match paths
|
77
|
+
|
78
|
+
The parsed path can be used to match against actual element paths:
|
79
|
+
|
80
|
+
[source,ruby]
|
81
|
+
----
|
82
|
+
path = Lutaml::Path.parse("model::*::BaseClass")
|
83
|
+
|
84
|
+
path.match?(["model", "core", "BaseClass"]) # => true
|
85
|
+
path.match?(["model", "BaseClass"]) # => false
|
86
|
+
path.match?(["other", "core", "BaseClass"]) # => false
|
87
|
+
----
|
88
|
+
|
89
|
+
== Understanding absolute and relative paths
|
90
|
+
|
91
|
+
* Absolute paths (starting with `::`) must match the entire element path
|
92
|
+
* Relative paths can match elements at any depth
|
93
|
+
|
94
|
+
[source,ruby]
|
95
|
+
----
|
96
|
+
absolute = Lutaml::Path.parse("::model::Class")
|
97
|
+
relative = Lutaml::Path.parse("model::Class")
|
98
|
+
|
99
|
+
absolute.match?(["model", "Class"]) # => true
|
100
|
+
absolute.match?(["root", "model", "Class"]) # => false
|
101
|
+
|
102
|
+
relative.match?(["model", "Class"]) # => true
|
103
|
+
relative.match?(["root", "model", "Class"]) # => true
|
104
|
+
----
|
105
|
+
|
106
|
+
== Path syntax reference
|
107
|
+
|
108
|
+
The path expression grammar follows these rules:
|
109
|
+
|
110
|
+
* Path segments are separated by `::`
|
111
|
+
* The separator can be escaped with a backslash: `\::`
|
112
|
+
* An absolute path starts with `::`
|
113
|
+
* Each segment can contain:
|
114
|
+
** Regular characters (including Unicode)
|
115
|
+
** Wildcards (`*`, `?`)
|
116
|
+
** Character classes (`[abc]`)
|
117
|
+
** Alternatives (`{pattern1,pattern2}`)
|
118
|
+
|
119
|
+
== Matching paths with escaped colons
|
120
|
+
|
121
|
+
When matching paths with escaped colons, the escaped sequences are treated as
|
122
|
+
part of the segment name:
|
123
|
+
|
124
|
+
[source,ruby]
|
125
|
+
----
|
126
|
+
path = Lutaml::Path.parse("model::std\\::string")
|
127
|
+
|
128
|
+
path.match?(["model", "std::string"]) # => true
|
129
|
+
path.match?(["model", "std", "string"]) # => false
|
130
|
+
----
|
131
|
+
|
132
|
+
== Examples of UML element references
|
133
|
+
|
134
|
+
[source,ruby]
|
135
|
+
----
|
136
|
+
# Reference a class in a package
|
137
|
+
"model::shapes::Rectangle"
|
138
|
+
|
139
|
+
# Reference an operation on a class
|
140
|
+
"model::shapes::Rectangle::area"
|
141
|
+
|
142
|
+
# Reference a property in a nested class
|
143
|
+
"model::university::Student::Address::street"
|
144
|
+
|
145
|
+
# Find all classes implementing an interface
|
146
|
+
"model::*::IShape"
|
147
|
+
|
148
|
+
# Match any stereotype application
|
149
|
+
"model::profiles::UMLProfile::*Stereotype"
|
150
|
+
----
|
151
|
+
|
152
|
+
These paths can be used to locate elements across UML model hierarchies, making
|
153
|
+
it easier to reference and work with model elements programmatically.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "lutaml/path"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
require "irb"
|
11
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lutaml
|
4
|
+
module Path
|
5
|
+
class ElementPath
|
6
|
+
attr_reader :segments, :absolute
|
7
|
+
|
8
|
+
def initialize(segments, absolute: false)
|
9
|
+
@segments = Array(segments)
|
10
|
+
@absolute = absolute
|
11
|
+
end
|
12
|
+
|
13
|
+
def absolute?
|
14
|
+
@absolute
|
15
|
+
end
|
16
|
+
|
17
|
+
def match?(path_segments)
|
18
|
+
return false if absolute? && path_segments.length != segments.length
|
19
|
+
return false if path_segments.length < segments.length
|
20
|
+
|
21
|
+
segments.zip(path_segments).all? { |seg, path_seg| seg.match?(path_seg) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/lutaml/path/parser.rb
|
4
|
+
require "parslet"
|
5
|
+
|
6
|
+
module Lutaml
|
7
|
+
module Path
|
8
|
+
class Parser < Parslet::Parser
|
9
|
+
rule(:space) { match('\s').repeat }
|
10
|
+
|
11
|
+
rule(:escaped_separator) { str("\\") >> str("::") }
|
12
|
+
rule(:separator) { str("::") }
|
13
|
+
|
14
|
+
# Character rules
|
15
|
+
rule(:glob_char) { match('[*?\[{]') }
|
16
|
+
rule(:regular_char) do
|
17
|
+
(separator.absent? >> str("\\").absent? >> any) |
|
18
|
+
escaped_separator
|
19
|
+
end
|
20
|
+
|
21
|
+
# Single segment can contain any regular chars or glob chars
|
22
|
+
rule(:segment_content) do
|
23
|
+
(glob_char | regular_char).repeat(1).as(:content) >>
|
24
|
+
glob_char.present?.maybe.as(:is_pattern)
|
25
|
+
end
|
26
|
+
|
27
|
+
rule(:segment) do
|
28
|
+
segment_content.as(:segment)
|
29
|
+
end
|
30
|
+
|
31
|
+
rule(:segments) do
|
32
|
+
(separator >> segment).repeat.as(:more_segments)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Full path expression - either absolute or relative
|
36
|
+
rule(:path_expr) do
|
37
|
+
((separator.as(:absolute) >> segment.as(:first_segment) >> segments) |
|
38
|
+
(segment.as(:first_segment) >> segments))
|
39
|
+
end
|
40
|
+
|
41
|
+
root(:path_expr)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lutaml
|
4
|
+
module Path
|
5
|
+
class PathSegment
|
6
|
+
attr_reader :name, :pattern
|
7
|
+
|
8
|
+
def initialize(name, is_pattern: false)
|
9
|
+
@name = name.gsub('\::', "::")
|
10
|
+
@pattern = is_pattern
|
11
|
+
end
|
12
|
+
|
13
|
+
def pattern?
|
14
|
+
@pattern
|
15
|
+
end
|
16
|
+
|
17
|
+
def match?(segment)
|
18
|
+
return File.fnmatch(name, segment) if pattern?
|
19
|
+
|
20
|
+
name == segment
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/lutaml/path/transformer.rb
|
4
|
+
require "parslet"
|
5
|
+
|
6
|
+
module Lutaml
|
7
|
+
module Path
|
8
|
+
class Transformer < Parslet::Transform
|
9
|
+
rule(content: simple(:content), is_pattern: simple(:is_pattern)) do |dict|
|
10
|
+
content = dict[:content].to_s
|
11
|
+
is_pattern = !dict[:is_pattern].nil?
|
12
|
+
PathSegment.new(content, is_pattern: is_pattern)
|
13
|
+
end
|
14
|
+
|
15
|
+
rule(segment: subtree(:segment)) { segment }
|
16
|
+
|
17
|
+
# Transform more_segments rule
|
18
|
+
rule(more_segments: sequence(:segments)) { segments }
|
19
|
+
rule(more_segments: simple(:segment)) { [segment].compact } # For single segment
|
20
|
+
rule(more_segments: []) { [] } # For empty segments
|
21
|
+
|
22
|
+
# Absolute path
|
23
|
+
rule(
|
24
|
+
absolute: simple(:_),
|
25
|
+
first_segment: simple(:first),
|
26
|
+
more_segments: sequence(:rest)
|
27
|
+
) do |dict|
|
28
|
+
segments = [dict[:first]] + Array(dict[:rest]).compact
|
29
|
+
ElementPath.new(segments, absolute: true)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Relative path
|
33
|
+
rule(
|
34
|
+
first_segment: simple(:first),
|
35
|
+
more_segments: sequence(:rest)
|
36
|
+
) do |dict|
|
37
|
+
segments = [dict[:first]] + Array(dict[:rest]).compact
|
38
|
+
ElementPath.new(segments, absolute: false)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/lutaml/path.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "parslet"
|
4
|
+
require_relative "path/version"
|
5
|
+
require_relative "path/parser"
|
6
|
+
require_relative "path/transformer"
|
7
|
+
require_relative "path/element_path"
|
8
|
+
require_relative "path/path_segment"
|
9
|
+
|
10
|
+
module Lutaml
|
11
|
+
module Path
|
12
|
+
class ParseError < StandardError; end
|
13
|
+
|
14
|
+
def self.parse(input)
|
15
|
+
tree = Parser.new.parse(input)
|
16
|
+
Transformer.new.apply(tree)
|
17
|
+
rescue Parslet::ParseFailed => e
|
18
|
+
raise ParseError, e.message
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lutaml-path.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/lutaml/path/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "lutaml-path"
|
7
|
+
spec.version = Lutaml::Path::VERSION
|
8
|
+
spec.authors = ["Ribose Inc."]
|
9
|
+
spec.email = ["open.source@ribose.com"]
|
10
|
+
|
11
|
+
spec.summary = "Element path parser supporting OCL-style paths"
|
12
|
+
spec.description = "Parser for element paths"
|
13
|
+
|
14
|
+
spec.homepage = "https://github.com/lutaml/lutaml-path"
|
15
|
+
spec.license = "BSD-2-Clause"
|
16
|
+
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0")
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the
|
23
|
+
# RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|features)/})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
|
31
|
+
spec.add_dependency "parslet"
|
32
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
33
|
+
end
|
data/sig/lutaml/path.rbs
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
RSpec.describe Lutaml::Path do
|
6
|
+
describe ".parse" do
|
7
|
+
it "parses simple paths" do
|
8
|
+
path = described_class.parse("Element")
|
9
|
+
expect(path.absolute?).to be false
|
10
|
+
expect(path.segments.length).to eq(1)
|
11
|
+
expect(path.segments.first.name).to eq("Element")
|
12
|
+
expect(path.segments.first.pattern?).to be false
|
13
|
+
end
|
14
|
+
|
15
|
+
it "parses absolute paths" do
|
16
|
+
path = described_class.parse("::Package::Element")
|
17
|
+
expect(path.absolute?).to be true
|
18
|
+
expect(path.segments.map(&:name)).to eq(%w[Package Element])
|
19
|
+
end
|
20
|
+
|
21
|
+
xit "parses paths with patterns" do
|
22
|
+
path = described_class.parse("Package::*::Base*")
|
23
|
+
expect(path.segments.map(&:pattern?)).to eq([false, true, true])
|
24
|
+
expect(path.segments.map(&:name)).to eq(["Package", "*", "Base*"])
|
25
|
+
end
|
26
|
+
|
27
|
+
it "handles escaped separators" do
|
28
|
+
path = described_class.parse("core\\::types::Element")
|
29
|
+
expect(path.segments.map(&:name)).to eq(["core::types", "Element"])
|
30
|
+
end
|
31
|
+
|
32
|
+
it "handles Unicode characters" do
|
33
|
+
path = described_class.parse("建物::窓::ガラス")
|
34
|
+
expect(path.segments.map(&:name)).to eq(%w[建物 窓 ガラス])
|
35
|
+
end
|
36
|
+
|
37
|
+
xit "handles glob patterns" do
|
38
|
+
path = described_class.parse("pkg::{a,b}*::[0-9]*")
|
39
|
+
expect(path.segments.last.pattern?).to be true
|
40
|
+
expect(path.match?(%w[pkg btest 123])).to be true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "raises error on empty segments" do
|
44
|
+
expect { described_class.parse("pkg::::element") }.to raise_error(described_class::ParseError)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "raises error on invalid syntax" do
|
48
|
+
expect { described_class.parse("") }.to raise_error(described_class::ParseError)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe Lutaml::Path::PathSegment do
|
53
|
+
it "matches exact names" do
|
54
|
+
segment = described_class.new("Element")
|
55
|
+
expect(segment.match?("Element")).to be true
|
56
|
+
expect(segment.match?("Other")).to be false
|
57
|
+
end
|
58
|
+
|
59
|
+
it "matches patterns" do
|
60
|
+
segment = described_class.new("Base*", is_pattern: true)
|
61
|
+
expect(segment.match?("BaseClass")).to be true
|
62
|
+
expect(segment.match?("Other")).to be false
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe Lutaml::Path::ElementPath do
|
67
|
+
it "matches path segments" do
|
68
|
+
path = described_class.new([
|
69
|
+
Lutaml::Path::PathSegment.new("pkg"),
|
70
|
+
Lutaml::Path::PathSegment.new("*", is_pattern: true),
|
71
|
+
Lutaml::Path::PathSegment.new("Element")
|
72
|
+
])
|
73
|
+
|
74
|
+
expect(path.match?(%w[pkg sub Element])).to be true
|
75
|
+
expect(path.match?(%w[other sub Element])).to be false
|
76
|
+
end
|
77
|
+
|
78
|
+
it "respects absolute paths" do
|
79
|
+
path = described_class.new([
|
80
|
+
Lutaml::Path::PathSegment.new("pkg"),
|
81
|
+
Lutaml::Path::PathSegment.new("Element")
|
82
|
+
], absolute: true)
|
83
|
+
|
84
|
+
expect(path.match?(%w[pkg Element])).to be true
|
85
|
+
expect(path.match?(%w[root pkg Element])).to be false
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "lutaml/path"
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
# Enable flags like --only-failures and --next-failure
|
7
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
8
|
+
|
9
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
10
|
+
config.disable_monkey_patching!
|
11
|
+
|
12
|
+
config.expect_with :rspec do |c|
|
13
|
+
c.syntax = :expect
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lutaml-path
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ribose Inc.
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-11-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: parslet
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Parser for element paths
|
28
|
+
email:
|
29
|
+
- open.source@ribose.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".github/workflows/main.yml"
|
35
|
+
- ".gitignore"
|
36
|
+
- ".rspec"
|
37
|
+
- ".rubocop.yml"
|
38
|
+
- ".rubocop_todo.yml"
|
39
|
+
- CODE_OF_CONDUCT.md
|
40
|
+
- Gemfile
|
41
|
+
- README.adoc
|
42
|
+
- Rakefile
|
43
|
+
- bin/console
|
44
|
+
- bin/setup
|
45
|
+
- lib/lutaml/path.rb
|
46
|
+
- lib/lutaml/path/element_path.rb
|
47
|
+
- lib/lutaml/path/parser.rb
|
48
|
+
- lib/lutaml/path/path_segment.rb
|
49
|
+
- lib/lutaml/path/transformer.rb
|
50
|
+
- lib/lutaml/path/version.rb
|
51
|
+
- lutaml-path.gemspec
|
52
|
+
- sig/lutaml/path.rbs
|
53
|
+
- spec/lutaml/path_spec.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
homepage: https://github.com/lutaml/lutaml-path
|
56
|
+
licenses:
|
57
|
+
- BSD-2-Clause
|
58
|
+
metadata:
|
59
|
+
rubygems_mfa_required: 'true'
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.0.0
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubygems_version: 3.5.22
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Element path parser supporting OCL-style paths
|
79
|
+
test_files: []
|