cql_ruby 0.0.12 → 0.0.13

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