cql_ruby 0.0.7 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,81 +1,130 @@
1
- # frozen_string_literal: true
2
-
3
- module CqlRuby
4
- class NestRule < Struct.new(:type, :name)
5
- NAME_ANY = '*'
6
- ALLOWED_TYPE = %w[class module def block].freeze
7
-
8
- class << self
9
- #
10
- # @param [String] raw_value
11
- # Format: TYPE(=NAME|=*)
12
- # Accepted types: class, module, def, block
13
- #
14
- def from(raw_value)
15
- type, name = raw_value.split('=')
16
- name ||= NAME_ANY
17
-
18
- raise "Unknown type: #{type}. Allowed: #{ALLOWED_TYPE}" unless ALLOWED_TYPE.include?(type)
19
- raise "Type #{type} cannot have a name." if %w[block].include?(type) && name != NAME_ANY
20
-
21
- new(type, name)
22
- end
23
- end
24
-
25
- def restrict_name?
26
- name != NAME_ANY
27
- end
28
- end
29
-
30
- #
31
- # Reads and provides filters.
32
- #
33
- # Accepted filters and syntax:
34
- #
35
- # Type:
36
- #
37
- # type:[name](,[name])*
38
- # example: type:def,send
39
- #
40
- class FilterReader
41
- # @attribute [Parser::AST::Node] allowed_types
42
- attr_reader :allowed_types
43
- # @attribute [Array<Cqlruby::NestRule>] nest_under
44
- attr_reader :nest_under
45
-
46
- def initialize(raw_filters)
47
- super()
48
-
49
- @allowed_types = []
50
- @nest_under = []
51
-
52
- parse_raw_filters(raw_filters)
53
- end
54
-
55
- def restrict_types?
56
- !@allowed_types.empty?
57
- end
58
-
59
- def restrict_nesting?
60
- !@nest_under.empty?
61
- end
62
-
63
- private
64
-
65
- # @param [Array<String>] raw_filters
66
- def parse_raw_filters(raw_filters)
67
- raw_filters.each do |raw_filter|
68
- name, value = raw_filter.split(':')
69
- raise "Unrecognized filter: #{raw_filter}" if name.nil? || value.nil?
70
-
71
- if %w[type t].include?(name)
72
- @allowed_types += value.split(',').map(&:to_sym)
73
- elsif %w[nest n].include?(name)
74
- @nest_under << NestRule.from(value)
75
- end
76
- end
77
-
78
- nil
79
- end
80
- end
81
- 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,53 +1,57 @@
1
- # frozen_string_literal: true
2
- module CqlRuby
3
- module PatternMatcher
4
- def self.match?(pattern, subject)
5
- subject = subject.to_s
6
- pattern = pattern.to_s
7
-
8
- if regex?(pattern)
9
- regex_match?(pattern, subject)
10
- elsif partial_string?(pattern)
11
- partial_string_match?(pattern, subject)
12
- else
13
- full_string_match?(pattern, subject)
14
- end
15
- end
16
-
17
- def self.regex?(pattern)
18
- pattern[0..1] == 'r/'
19
- end
20
- private_class_method :regex?
21
-
22
- def self.partial_string?(pattern)
23
- pattern[0] == '%'
24
- end
25
- private_class_method :partial_string?
26
-
27
- def self.regex_match?(pattern, subject)
28
- pattern = pattern[2..]
29
- delim_idx = pattern.rindex('/')
30
- mods = pattern[delim_idx + 1..].chars
31
- pattern = pattern[0..delim_idx - 1]
32
-
33
- fops = 0
34
- fops |= Regexp::IGNORECASE if mods.include?('i')
35
- fops |= Regexp::MULTILINE if mods.include?('m')
36
- fops |= Regexp::EXTENDED if mods.include?('x')
37
- fops |= Regexp::FIXEDENCODING if mods.include?('f')
38
- fops |= Regexp::NOENCODING if mods.include?('n')
39
- Regexp.new(pattern, fops).match?(subject)
40
- end
41
- private_class_method :regex_match?
42
-
43
- def self.full_string_match?(pattern, subject)
44
- pattern == subject
45
- end
46
- private_class_method :full_string_match?
47
-
48
- def self.partial_string_match?(pattern, subject)
49
- !subject.index(pattern[1..]).nil?
50
- end
51
- private_class_method :partial_string_match?
52
- end
53
- 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.7
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