cql_ruby 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,97 +1,130 @@
1
- # frozen_string_literal: true
2
-
3
- # TODO: Have convenience filters for type:_ such as: isclass, ismodule, isdef ...
4
-
5
- module CqlRuby
6
- class NodeSpec < Struct.new(:type, :name)
7
- # Make this non duplicated.
8
- NAME_ANY = '*'
9
-
10
- class << self
11
- #
12
- # @param [String] raw_value
13
- # Format: TYPE(=NAME|=*)
14
- # Accepted types: class, module, def, block
15
- #
16
- def from(raw_value)
17
- type, name = raw_value.split('=')
18
- name ||= NAME_ANY
19
-
20
- raise "Type '#{type}' is not recognized. See 'cql_ruby --help' for allowed types." unless Parser::Meta::NODE_TYPES.member?(type.to_sym)
21
-
22
- new(type, name)
23
- end
24
- end
25
-
26
- def restrict_name?
27
- name != NAME_ANY
28
- end
29
- end
30
-
31
- #
32
- # Reads and provides filters.
33
- #
34
- # Accepted filters and syntax:
35
- #
36
- # Type:
37
- #
38
- # type:[name](,[name])*
39
- # example: type:def,send
40
- #
41
- class FilterReader
42
- NESTING_ALLOWED_TYPES = %w[class module def block].freeze
43
-
44
- # @attribute [Array<Symbol>] allowed_types
45
- attr_reader :allowed_types
46
- # @attribute [Array<CqlRuby::NodeSpec>] nest_under
47
- attr_reader :nest_under
48
- # @attribute [Array<CqlRuby::NodeSpec>] has_leaves
49
- attr_reader :has_leaves
50
-
51
- def initialize(raw_filters)
52
- super()
53
-
54
- @allowed_types = []
55
- @nest_under = []
56
- @has_leaves = []
57
-
58
- parse_raw_filters(raw_filters)
59
- end
60
-
61
- def restrict_types?
62
- !@allowed_types.empty?
63
- end
64
-
65
- def restrict_nesting?
66
- !@nest_under.empty?
67
- end
68
-
69
- def restrict_children?
70
- !@has_leaves.empty?
71
- end
72
-
73
- private
74
-
75
- # @param [Array<String>] raw_filters
76
- def parse_raw_filters(raw_filters)
77
- raw_filters.each do |raw_filter|
78
- name, value = raw_filter.split(':')
79
- raise "Unrecognized filter: #{raw_filter}" if name.nil? || value.nil?
80
-
81
- if %w[type t].include?(name)
82
- @allowed_types += value.split(',').map(&:to_sym)
83
- elsif %w[nest n].include?(name)
84
- spec = NodeSpec.from(value)
85
- raise "Unknown type for nesting: '#{spec.type}' from '#{raw_filter}'. Allowed: #{NESTING_ALLOWED_TYPES}" unless NESTING_ALLOWED_TYPES.include?(spec.type)
86
- raise "Type #{spec.type} cannot have a name." if %w[block].include?(spec.type) && spec.restrict_name?
87
-
88
- @nest_under << spec
89
- elsif %w[has h].include?(name)
90
- @has_leaves << NodeSpec.from(value)
91
- end
92
- end
93
-
94
- nil
95
- end
96
- end
97
- end
1
+ # frozen_string_literal: true
2
+
3
+ # TODO: Have convenience filters for type:_ such as: isclass, ismodule, isdef ...
4
+
5
+ module CqlRuby
6
+ # TODO Move this under filter reader namespace.
7
+ class HierarchyPattern
8
+ SELF_MARKER = 'X'
9
+
10
+ def self.from(raw_value)
11
+ parts = raw_value.split('-')
12
+ self_marker_idx = parts.index(SELF_MARKER)
13
+ raise "Missing self marker '#{SELF_MARKER}' in hierarchy pattern." if self_marker_idx.nil?
14
+
15
+ ancestors = parts[0...self_marker_idx]
16
+ descendants = parts[self_marker_idx + 1..]
17
+
18
+ new(ancestors, descendants)
19
+ end
20
+
21
+ attr_reader :ancestors
22
+ attr_reader :descendants
23
+
24
+ def initialize(ancestors, descendants)
25
+ @ancestors = ancestors
26
+ @descendants = descendants
27
+ end
28
+ end
29
+
30
+ class NodeSpec < Struct.new(:type, :name)
31
+ # Make this non duplicated.
32
+ NAME_ANY = '*'
33
+
34
+ class << self
35
+ #
36
+ # @param [String] raw_value
37
+ # Format: TYPE(=NAME|=*)
38
+ # Accepted types: class, module, def, block
39
+ #
40
+ def from(raw_value)
41
+ type, name = raw_value.split('=')
42
+ name ||= NAME_ANY
43
+
44
+ raise "Type '#{type}' is not recognized. See 'cql_ruby --help' for allowed types." unless Parser::Meta::NODE_TYPES.member?(type.to_sym)
45
+
46
+ new(type, name)
47
+ end
48
+ end
49
+
50
+ def restrict_name?
51
+ name != NAME_ANY
52
+ end
53
+ end
54
+
55
+ #
56
+ # Reads and provides filters.
57
+ #
58
+ # Accepted filters and syntax:
59
+ #
60
+ # Type:
61
+ #
62
+ # type:[name](,[name])*
63
+ # example: type:def,send
64
+ #
65
+ class FilterReader
66
+ NESTING_ALLOWED_TYPES = %w[class module def block].freeze
67
+
68
+ # @attribute [Array<Symbol>] allowed_types
69
+ attr_reader :allowed_types
70
+ # @attribute [Array<CqlRuby::NodeSpec>] nest_under
71
+ attr_reader :nest_under
72
+ # @attribute [Array<CqlRuby::NodeSpec>] has_leaves
73
+ attr_reader :has_leaves
74
+ # @attribute [Array<CqlRuby::HierarchyPattern>] patterns
75
+ attr_reader :patterns
76
+
77
+ def initialize(raw_filters)
78
+ super()
79
+
80
+ @allowed_types = []
81
+ @nest_under = []
82
+ @has_leaves = []
83
+ @patterns = []
84
+
85
+ parse_raw_filters(raw_filters)
86
+ end
87
+
88
+ def restrict_types?
89
+ !@allowed_types.empty?
90
+ end
91
+
92
+ def restrict_nesting?
93
+ !@nest_under.empty?
94
+ end
95
+
96
+ def restrict_children?
97
+ !@has_leaves.empty?
98
+ end
99
+
100
+ def restrict_pattern?
101
+ !@patterns.empty?
102
+ end
103
+
104
+ private
105
+
106
+ # @param [Array<String>] raw_filters
107
+ def parse_raw_filters(raw_filters)
108
+ raw_filters.each do |raw_filter|
109
+ name, value = raw_filter.split(':')
110
+ raise "Unrecognized filter: #{raw_filter}" if name.nil? || value.nil?
111
+
112
+ if %w[type t].include?(name)
113
+ @allowed_types += value.split(',').map(&:to_sym)
114
+ elsif %w[nest n].include?(name)
115
+ spec = NodeSpec.from(value)
116
+ raise "Unknown type for nesting: '#{spec.type}' from '#{raw_filter}'. Allowed: #{NESTING_ALLOWED_TYPES}" unless NESTING_ALLOWED_TYPES.include?(spec.type)
117
+ raise "Type #{spec.type} cannot have a name." if %w[block].include?(spec.type) && spec.restrict_name?
118
+
119
+ @nest_under << spec
120
+ elsif %w[has h].include?(name)
121
+ @has_leaves << NodeSpec.from(value)
122
+ elsif %w[pattern p].include?(name)
123
+ @patterns << HierarchyPattern.from(value)
124
+ end
125
+ end
126
+
127
+ nil
128
+ end
129
+ end
130
+ end
@@ -1,57 +1,57 @@
1
- # frozen_string_literal: true
2
- module CqlRuby
3
- module PatternMatcher
4
- MATCH_ANY = '*'
5
-
6
- def self.match?(pattern, subject)
7
- pattern = pattern.to_s
8
- return true if pattern == MATCH_ANY
9
-
10
- subject = subject.to_s
11
-
12
- if regex?(pattern)
13
- regex_match?(pattern, subject)
14
- elsif partial_string?(pattern)
15
- partial_string_match?(pattern, subject)
16
- else
17
- full_string_match?(pattern, subject)
18
- end
19
- end
20
-
21
- def self.regex?(pattern)
22
- pattern[0..1] == 'r/'
23
- end
24
- private_class_method :regex?
25
-
26
- def self.partial_string?(pattern)
27
- pattern[0] == '%'
28
- end
29
- private_class_method :partial_string?
30
-
31
- def self.regex_match?(pattern, subject)
32
- pattern = pattern[2..]
33
- delim_idx = pattern.rindex('/')
34
- mods = pattern[delim_idx + 1..].chars
35
- pattern = pattern[0..delim_idx - 1]
36
-
37
- fops = 0
38
- fops |= Regexp::IGNORECASE if mods.include?('i')
39
- fops |= Regexp::MULTILINE if mods.include?('m')
40
- fops |= Regexp::EXTENDED if mods.include?('x')
41
- fops |= Regexp::FIXEDENCODING if mods.include?('f')
42
- fops |= Regexp::NOENCODING if mods.include?('n')
43
- Regexp.new(pattern, fops).match?(subject)
44
- end
45
- private_class_method :regex_match?
46
-
47
- def self.full_string_match?(pattern, subject)
48
- pattern == subject
49
- end
50
- private_class_method :full_string_match?
51
-
52
- def self.partial_string_match?(pattern, subject)
53
- !subject.index(pattern[1..]).nil?
54
- end
55
- private_class_method :partial_string_match?
56
- end
57
- end
1
+ # frozen_string_literal: true
2
+ module CqlRuby
3
+ module PatternMatcher
4
+ MATCH_ANY = '*'
5
+
6
+ def self.match?(pattern, subject)
7
+ pattern = pattern.to_s
8
+ return true if pattern == MATCH_ANY
9
+
10
+ subject = subject.to_s
11
+
12
+ if regex?(pattern)
13
+ regex_match?(pattern, subject)
14
+ elsif partial_string?(pattern)
15
+ partial_string_match?(pattern, subject)
16
+ else
17
+ full_string_match?(pattern, subject)
18
+ end
19
+ end
20
+
21
+ def self.regex?(pattern)
22
+ pattern[0..1] == 'r/'
23
+ end
24
+ private_class_method :regex?
25
+
26
+ def self.partial_string?(pattern)
27
+ pattern[0] == '%'
28
+ end
29
+ private_class_method :partial_string?
30
+
31
+ def self.regex_match?(pattern, subject)
32
+ pattern = pattern[2..]
33
+ delim_idx = pattern.rindex('/')
34
+ mods = pattern[delim_idx + 1..].chars
35
+ pattern = pattern[0..delim_idx - 1]
36
+
37
+ fops = 0
38
+ fops |= Regexp::IGNORECASE if mods.include?('i')
39
+ fops |= Regexp::MULTILINE if mods.include?('m')
40
+ fops |= Regexp::EXTENDED if mods.include?('x')
41
+ fops |= Regexp::FIXEDENCODING if mods.include?('f')
42
+ fops |= Regexp::NOENCODING if mods.include?('n')
43
+ Regexp.new(pattern, fops).match?(subject)
44
+ end
45
+ private_class_method :regex_match?
46
+
47
+ def self.full_string_match?(pattern, subject)
48
+ pattern == subject
49
+ end
50
+ private_class_method :full_string_match?
51
+
52
+ def self.partial_string_match?(pattern, subject)
53
+ !subject.index(pattern[1..]).nil?
54
+ end
55
+ private_class_method :partial_string_match?
56
+ end
57
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cql_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - itarato
@@ -65,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
65
  - !ruby/object:Gem::Version
66
66
  version: '0'
67
67
  requirements: []
68
- rubygems_version: 3.0.1
68
+ rubygems_version: 3.0.3
69
69
  signing_key:
70
70
  specification_version: 4
71
71
  summary: Code Query Language for Ruby