prism_ext 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d09f437a427a12d11ba67c28be50da7372493ad3a177938815c5b5abf065b5cf
4
+ data.tar.gz: c2f18817e9862e578b3bbda235a7040d7b8526654888ae8f200a90fac00db0c3
5
+ SHA512:
6
+ metadata.gz: 6174bbad0ec343872a8f15d0953c09f8ab642476a8ed6c68161969df4aed8c786cebd6559ba6ae2b2e9fbf0bd897c0ee2a89e55e4f7bda6c952e85b5c85c9e8b
7
+ data.tar.gz: f49dde3337f979b258633a07fa1825d286e029e474d9fd560203d2e74efd77258a76bcfd90f6fdbc1add4588c1f3e1bcd8430b8e7949bbbd23547d752a9da38e
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.1.0 (2024-02-10)
4
+
5
+ * Add some helper methods to `Prism::Node`.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in prism_ext.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ prism_ext (0.1.0)
5
+ prism
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.5.0)
11
+ prism (0.20.0)
12
+ rake (13.1.0)
13
+ rspec (3.12.0)
14
+ rspec-core (~> 3.12.0)
15
+ rspec-expectations (~> 3.12.0)
16
+ rspec-mocks (~> 3.12.0)
17
+ rspec-core (3.12.2)
18
+ rspec-support (~> 3.12.0)
19
+ rspec-expectations (3.12.3)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.12.0)
22
+ rspec-mocks (3.12.6)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-support (3.12.1)
26
+
27
+ PLATFORMS
28
+ ruby
29
+ x86_64-darwin-23
30
+
31
+ DEPENDENCIES
32
+ prism_ext!
33
+ rake (~> 13.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 2.5.3
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # PrismExt
2
+
3
+ It adds some helpers to prism node.
4
+
5
+ ```ruby
6
+ # node is a HashNode
7
+ node.foo_element # get the element node of hash foo key
8
+ node.foo_value # get the value of of the hash foo key
9
+ node.foo_source # get the source of the value node of the hash foo key
10
+ ```
11
+
12
+ ## Installation
13
+
14
+ Install the gem and add to the application's Gemfile by executing:
15
+
16
+ $ bundle add prism_ext
17
+
18
+ If bundler is not being used to manage dependencies, install the gem by executing:
19
+
20
+ $ gem install prism_ext
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'prism'
26
+ require 'prism_ext'
27
+ ```
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xinminlabs/prism_ext.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrismExt
4
+ VERSION = "0.1.0"
5
+ end
data/lib/prism_ext.rb ADDED
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "prism_ext/version"
4
+ require 'prism'
5
+
6
+ module PrismExt
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
10
+
11
+ module Prism
12
+ class Node
13
+ def source
14
+ location.instance_variable_get(:@source).source
15
+ end
16
+
17
+ def keys
18
+ if is_a?(HashNode)
19
+ elements.map(&:key)
20
+ else
21
+ raise MethodNotSupported, "keys is not supported for #{self}"
22
+ end
23
+ end
24
+
25
+ def values
26
+ if is_a?(HashNode)
27
+ elements.map(&:value)
28
+ else
29
+ raise MethodNotSupported, "values is not supported for #{self}"
30
+ end
31
+ end
32
+
33
+ def hash_element(key)
34
+ if is_a?(HashNode)
35
+ elements.find { |element_node| element_node.key.to_value == key }
36
+ else
37
+ raise MethodNotSupported, "hash_pair is not supported for #{self}"
38
+ end
39
+ end
40
+
41
+ def hash_value(key)
42
+ if is_a?(HashNode)
43
+ elements.find { |element_node| element_node.key.to_value == key }&.value
44
+ else
45
+ raise MethodNotSupported, "hash_value is not supported for #{self}"
46
+ end
47
+ end
48
+
49
+ def to_value
50
+ case self
51
+ when SymbolNode
52
+ value.to_sym
53
+ when StringNode
54
+ content
55
+ when FloatNode
56
+ value
57
+ when IntegerNode
58
+ value
59
+ when TrueNode
60
+ true
61
+ when FalseNode
62
+ false
63
+ when NilNode
64
+ nil
65
+ when ArrayNode
66
+ elements.map { |element| element.to_value }
67
+ else
68
+ self
69
+ end
70
+ end
71
+
72
+ alias :to_source :slice
73
+
74
+ # Respond key value and source for hash node
75
+ def method_missing(method_name, *args, &block)
76
+ return super unless is_a?(HashNode)
77
+
78
+ if method_name.to_s.end_with?('_element')
79
+ key = method_name.to_s[0..-9]
80
+ return elements.find { |element| element.key.to_value.to_s == key }
81
+ elsif method_name.to_s.end_with?('_value')
82
+ key = method_name.to_s[0..-7]
83
+ return elements.find { |element| element.key.to_value.to_s == key }&.value
84
+ elsif method_name.to_s.end_with?('_source')
85
+ key = method_name.to_s[0..-8]
86
+ return elements.find { |element| element.key.to_value.to_s == key }&.value&.to_source || ''
87
+ end
88
+
89
+ super
90
+ end
91
+
92
+ def respond_to_missing?(method_name, *args)
93
+ return super unless is_a?(HashNode)
94
+
95
+ if method_name.to_s.end_with?('_element')
96
+ key = method_name.to_s[0..-9]
97
+ return !!elements.find { |element| element.key.to_value.to_s == key }
98
+ elsif method_name.to_s.end_with?('_value')
99
+ key = method_name.to_s[0..-7]
100
+ return !!elements.find { |element| element.key.to_value.to_s == key }
101
+ elsif method_name.to_s.end_with?('_source')
102
+ key = method_name.to_s[0..-8]
103
+ return !!elements.find { |element| element.key.to_value.to_s == key }
104
+ end
105
+
106
+ super
107
+ end
108
+ end
109
+ end
data/sig/prism_ext.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module PrismExt
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prism_ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Huang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: prism
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: extend ruby prism
28
+ email:
29
+ - flyerhzm@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - CHANGELOG.md
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - README.md
39
+ - Rakefile
40
+ - lib/prism_ext.rb
41
+ - lib/prism_ext/version.rb
42
+ - sig/prism_ext.rbs
43
+ homepage: https://github.com/xinminlabs/prism_ext
44
+ licenses: []
45
+ metadata:
46
+ homepage_uri: https://github.com/xinminlabs/prism_ext
47
+ source_code_uri: https://github.com/xinminlabs/prism_ext
48
+ changelog_uri: https://github.com/xinminlabs/prism_ext/CHANGELOG
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.7.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.5.3
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: extend ruby prism
68
+ test_files: []