specifind 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -85,6 +85,8 @@ module Specifind
85
85
  @comparator ? @comparator.to_params(@name) : nil
86
86
  end
87
87
 
88
+ ##
89
+ # Converts this attribute to a dynamic assertion of type of whatever value is passed to the find_by_* method
88
90
  def to_assertion
89
91
  @comparator.to_type_test @name, @type
90
92
  end
@@ -0,0 +1,108 @@
1
+ module Specifind
2
+
3
+ module Comparators
4
+ # Comparator holds the logic for each type of comparator that is use in {MethodBuilder} definition.
5
+ #
6
+ # The data are held in the class definition as [identifier (String), number of parameters (int), parameter suffixes (list of String), and sql creators (Procs)].
7
+ class Base
8
+ class_attribute :comparators
9
+ attr_accessor :pattern, :num_params, :param_suffixes, :values, :sql_proc
10
+
11
+ self.comparators = []
12
+ # list of comparator's patterns
13
+ def self.patterns
14
+ a = []
15
+ self.comparators.each{|c| a << c.pattern}
16
+ a
17
+ end
18
+
19
+ # find and return a (new) comparator by it's identifying string
20
+ def self.find(s)
21
+ self.comparators.each{|c| return c.clone if c.pattern == s}
22
+ nil
23
+ end
24
+
25
+ def self.comparators_data
26
+ [
27
+ ['_in_list', 1, %w(_list), Proc.new{|v| "in (#{v[0]})"}],
28
+ ['_less_than_equals', 1, %w(_val), Proc.new{|v| "<= #{v[0]}"}],
29
+ ['_less_than', 1, %w(_val), Proc.new{|v| "< #{v[0]}"}],
30
+ ['_greater_than_equals', 1, %w(_val), Proc.new{|v| ">= #{v[0]}"}],
31
+ ['_greater_than', 1, %w(_val), Proc.new{|v| "> #{v[0]}"}],
32
+ ['_not_equal', 1, %w(_val), Proc.new{|v| "!= #{v[0]}"}],
33
+ ['_between', 2, %w(_bound_one _bound_two), Proc.new{|v| "between #{v[0]} and #{v[1]}"}],
34
+ ['_is_not_null', 0, [], Proc.new{|v| "is not null"}],
35
+ ['_is_null', 0, [], Proc.new{|v| "is null"}],
36
+ ['_equals', 1, %w(_val), Proc.new{|v| "= #{v[0]}"}]
37
+ ]
38
+ end
39
+
40
+ # From the data listed in the Comparator class definition, Comparator.generate_comparators constructs each type of Comparators
41
+ # and adds it to Comparator.comparators
42
+ def self.generate_comparators
43
+ self.comparators_data.each{|c| c = self.new :pattern => c[0], :num_params => c[1], :param_suffixes => c[2], :sql_proc => c[3] }
44
+ end
45
+
46
+ def initialize(args)
47
+ @pattern = args[:pattern]
48
+ @num_params = args[:num_params]
49
+ @param_suffixes = @num_params > 0 ? args[:param_suffixes] : []
50
+ @sql_proc = args[:sql_proc]
51
+ @@encoding = ActiveRecord::Base.connection.instance_variable_get('@config')[:encoding]
52
+ self.class.comparators += [self]
53
+ end
54
+
55
+ # Creates the values list for this comparator so that when it's build_sql method is called,
56
+ # the sql building proc will know how to build the appropriate where clause
57
+ def set_values(property_name, attribute_hash)
58
+ @values = []
59
+ param_suffixes.each{|s| @values << attribute_hash["#{property_name}#{s}".to_sym]} if num_params > 0
60
+ end
61
+
62
+ # builds sql (through @sql_proc) for values that corresponds with this type of comparator
63
+ def to_where(name, remove_quotes = false)
64
+ #raise 'values for comparator are not defined' if !values
65
+ response = @sql_proc.call param_suffixes.map{|p| "\#{#{name}#{p}}" }
66
+ response.delete! "'" if remove_quotes
67
+ response
68
+ end
69
+
70
+ # builds signature (the parameter definitions for a method) based on the type of suffixes
71
+ # that are required for this type of Comparator. See {AttributePhrase#to_signature} for further detail
72
+ def to_signature(name)
73
+ param_suffixes.map{|p| "#{name}#{p}" }.join(',')
74
+ end
75
+
76
+ # builds hash component (the parameters for the search_by method) based on the type of suffixes
77
+ # that are required for this type of Comparator. See {AttributePhrase#to_params} for further detail
78
+ def to_params(name)
79
+ param_suffixes.map { |p| ":#{name}#{p} => #{name}#{p}" }.join(',')
80
+ end
81
+
82
+ def to_type_test(name, type)
83
+ out = ""
84
+ # assert that each variable associated with this comparator is of type passed.
85
+ # if its not a list
86
+ if @pattern != '_in_list'
87
+ param_suffixes.each do |p|
88
+ out += "#{name}#{p} = Type.assert_#{type}(#{name}#{p})\n"
89
+ end
90
+ else # is list - will only have one parameter, which is a list
91
+ out += "#{name}#{param_suffixes[0]}.each do |v|
92
+ v = Type.assert_#{type}(v)
93
+ end"
94
+ end
95
+ out
96
+ end
97
+
98
+ def to_rearrangement(name, type)
99
+ out = ''
100
+ if @pattern == '_in_list'
101
+ out += "#{name}#{param_suffixes[0]} = #{name}#{param_suffixes[0]}.map{|el| \"'\"+el+\"'\"}.join ','"
102
+ end
103
+ out
104
+ end
105
+
106
+ end
107
+ end
108
+ end
@@ -4,117 +4,15 @@ module Specifind
4
4
  # Comparator holds the logic for each type of comparator that is use in {MethodBuilder} definition.
5
5
  #
6
6
  # The data are held in the class definition as [identifier (String), number of parameters (int), parameter suffixes (list of String), and sql creators (Procs)].
7
- class Mysql2
8
- @@comparators = []
9
- attr_accessor :pattern, :num_params, :param_suffixes, :values, :sql_proc
10
-
11
- # Getter for comparator list
12
- def self.comparators
13
- @@comparators
14
- end
15
-
16
- # Setter for comparator list
17
- def self.comparators=(c)
18
- @@comparators = c
19
- end
20
-
21
- # list of comparator's patterns
22
- def self.patterns
23
- a = []
24
- comparators.each{|c| a << c.pattern}
25
- a
26
- end
27
-
28
- # find and return a (new) comparator by it's identifying string
29
- def self.find(s)
30
- comparators.each{|c| return c.clone if c.pattern == s}
31
- nil
32
- end
33
-
34
- # From the data listed in the Comparator class definition, Comparator.generate_comparators constructs each type of Comparators
35
- # and adds it to Comparator.comparators
36
- def self.generate_comparators
37
- self.comparators_data.each{|c| c = Mysql2.new :pattern => c[0], :num_params => c[1], :param_suffixes => c[2], :sql_proc => c[3] }
38
- end
7
+ class Mysql2 < Base
39
8
 
40
9
  def self.comparators_data
41
- [
42
- ['_in_list', 1, %w(_list), Proc.new{|v| "in (#{v[0]})"}],
43
- ['_less_than_equals', 1, %w(_val), Proc.new{|v| "<= #{v[0]}"}],
44
- ['_less_than', 1, %w(_val), Proc.new{|v| "< #{v[0]}"}],
45
- ['_greater_than_equals', 1, %w(_val), Proc.new{|v| ">= #{v[0]}"}],
46
- ['_greater_than', 1, %w(_val), Proc.new{|v| "> #{v[0]}"}],
10
+ super + [
47
11
  ['_like', 1, %w(_val), Proc.new{|v| "like #{v[0]} collate #{@@encoding}_bin"}],
48
- ['_ilike', 1, %w(_val), Proc.new{|v| "like #{v[0]}"}],
49
- ['_not_equal', 1, %w(_val), Proc.new{|v| "!= #{v[0]}"}],
50
- ['_between', 2, %w(_bound_one _bound_two), Proc.new{|v| "between #{v[0]} and #{v[1]}"}],
51
- ['_is_not_null', 0, [], Proc.new{|v| "is not null"}],
52
- ['_is_null', 0, [], Proc.new{|v| "is null"}],
53
- ['_equals', 1, %w(_val), Proc.new{|v| "= #{v[0]}"}]
12
+ ['_ilike', 1, %w(_val), Proc.new{|v| "like #{v[0]}"}]
54
13
  ]
55
14
  end
56
15
 
57
- def initialize(args)
58
- @pattern = args[:pattern]
59
- @num_params = args[:num_params]
60
- @param_suffixes = @num_params > 0 ? args[:param_suffixes] : []
61
- @sql_proc = args[:sql_proc]
62
- @@encoding = ActiveRecord::Base.connection.instance_variable_get('@config')[:encoding]
63
- Mysql2.comparators << self
64
- end
65
-
66
- # Creates the values list for this comparator so that when it's build_sql method is called,
67
- # the sql building proc will know how to build the appropriate where clause
68
- def set_values(property_name, attribute_hash)
69
- @values = []
70
- param_suffixes.each{|s| @values << attribute_hash["#{property_name}#{s}".to_sym]} if num_params > 0
71
- end
72
-
73
- # builds sql (through @sql_proc) for values that corresponds with this type of comparator
74
- def to_where(name, remove_quotes = false)
75
- #raise 'values for comparator are not defined' if !values
76
- response = @sql_proc.call param_suffixes.map{|p| "\#{#{name}#{p}}" }
77
- response.delete! "'" if remove_quotes
78
- response
79
- end
80
-
81
- # builds signature (the parameter definitions for a method) based on the type of suffixes
82
- # that are required for this type of Comparator. See {AttributePhrase#to_signature} for further detail
83
- def to_signature(name)
84
- return '' if num_params == 0
85
- param_suffixes.map{|p| "#{name}#{p}" }.join(',')
86
- end
87
-
88
- # builds hash component (the parameters for the search_by method) based on the type of suffixes
89
- # that are required for this type of Comparator. See {AttributePhrase#to_params} for further detail
90
- def to_params(name)
91
- return '' if num_params == 0
92
- param_suffixes.map { |p| ":#{name}#{p} => #{name}#{p}" }.join(',')
93
- end
94
-
95
- def to_type_test(name, type)
96
- out = ""
97
- # assert that each variable associated with this comparator is of type passed.
98
- # if its not a list
99
- if @pattern != '_in_list'
100
- param_suffixes.each do |p|
101
- out += "#{name}#{p} = Type.assert_#{type}(#{name}#{p})\n"
102
- end
103
- else # is list - will only have one parameter, which is a list
104
- out += "#{name}#{param_suffixes[0]}.each do |v|
105
- v = Type.assert_#{type}(v)
106
- end"
107
- end
108
- out
109
- end
110
-
111
- def to_rearrangement(name, type)
112
- out = ''
113
- if @pattern == '_in_list'
114
- out += "#{name}#{param_suffixes[0]} = #{name}#{param_suffixes[0]}.map{|el| '\"'+el+'\"'}.join ','"
115
- end
116
- out
117
- end
118
16
  end
119
17
  end
120
18
  end
@@ -4,116 +4,15 @@ module Specifind
4
4
  # Comparator holds the logic for each type of comparator that is use in {MethodBuilder} definition.
5
5
  #
6
6
  # The data are held in the class definition as [identifier (String), number of parameters (int), parameter suffixes (list of String), and sql creators (Procs)].
7
- class PostgreSQL
8
- @@comparators = []
9
- attr_accessor :pattern, :num_params, :param_suffixes, :values, :sql_proc
10
-
11
- # Getter for comparator list
12
- def self.comparators
13
- @@comparators
14
- end
15
-
16
- # Setter for comparator list
17
- def self.comparators=(c)
18
- @@comparators = c
19
- end
20
-
21
- # list of comparator's patterns
22
- def self.patterns
23
- a = []
24
- comparators.each{|c| a << c.pattern}
25
- a
26
- end
27
-
28
- # find and return a (new) comparator by it's identifying string
29
- def self.find(s)
30
- comparators.each{|c| return c.clone if c.pattern == s}
31
- nil
32
- end
33
-
34
- # From the data listed in the Comparator class definition, Comparator.generate_comparators constructs each type of Comparators
35
- # and adds it to Comparator.comparators
36
- def self.generate_comparators
37
- self.comparators_data.each{|c| c = PostgreSQL.new :pattern => c[0], :num_params => c[1], :param_suffixes => c[2], :sql_proc => c[3] }
38
- end
39
-
7
+ class PostgreSQL < Base
8
+
40
9
  def self.comparators_data
41
- [
42
- ['_in_list', 1, %w(_list), Proc.new{|v| "in (#{v[0]})"}],
43
- ['_less_than_equals', 1, %w(_val), Proc.new{|v| "<= #{v[0]}"}],
44
- ['_less_than', 1, %w(_val), Proc.new{|v| "< #{v[0]}"}],
45
- ['_greater_than_equals', 1, %w(_val), Proc.new{|v| ">= #{v[0]}"}],
46
- ['_greater_than', 1, %w(_val), Proc.new{|v| "> #{v[0]}"}],
10
+ super + [
47
11
  ['_like', 1, %w(_val), Proc.new{|v| "like #{v[0]}"}],
48
- ['_ilike', 1, %w(_val), Proc.new{|v| "ilike #{v[0]}"}],
49
- ['_not_equal', 1, %w(_val), Proc.new{|v| "!= #{v[0]}"}],
50
- ['_between', 2, %w(_bound_one _bound_two), Proc.new{|v| "between #{v[0]} and #{v[1]}"}],
51
- ['_is_not_null', 0, [], Proc.new{|v| "is not null"}],
52
- ['_is_null', 0, [], Proc.new{|v| "is null"}],
53
- ['_equals', 1, %w(_val), Proc.new{|v| "= #{v[0]}"}]
12
+ ['_ilike', 1, %w(_val), Proc.new{|v| "ilike #{v[0]}"}]
54
13
  ]
55
14
  end
56
-
57
- def initialize(args)
58
- @pattern = args[:pattern]
59
- @num_params = args[:num_params]
60
- @param_suffixes = @num_params > 0 ? args[:param_suffixes] : []
61
- @sql_proc = args[:sql_proc]
62
- PostgreSQL.comparators << self
63
- end
64
-
65
- # Creates the values list for this comparator so that when it's build_sql method is called,
66
- # the sql building proc will know how to build the appropriate where clause
67
- def set_values(property_name, attribute_hash)
68
- @values = []
69
- param_suffixes.each{|s| @values << attribute_hash["#{property_name}#{s}".to_sym]} if num_params > 0
70
- end
71
-
72
- # builds sql (through @sql_proc) for values that corresponds with this type of comparator
73
- def to_where(name, remove_quotes = false)
74
- #raise 'values for comparator are not defined' if !values
75
- response = @sql_proc.call param_suffixes.map{|p| "\#{#{name}#{p}}" }
76
- response.delete! "'" if remove_quotes
77
- response
78
- end
79
-
80
- # builds signature (the parameter definitions for a method) based on the type of suffixes
81
- # that are required for this type of Comparator. See {AttributePhrase#to_signature} for further detail
82
- def to_signature(name)
83
- return '' if num_params == 0
84
- param_suffixes.map{|p| "#{name}#{p}" }.join(',')
85
- end
86
-
87
- # builds hash component (the parameters for the search_by method) based on the type of suffixes
88
- # that are required for this type of Comparator. See {AttributePhrase#to_params} for further detail
89
- def to_params(name)
90
- return '' if num_params == 0
91
- param_suffixes.map { |p| ":#{name}#{p} => #{name}#{p}" }.join(',')
92
- end
93
-
94
- def to_type_test(name, type)
95
- out = ""
96
- # assert that each variable associated with this comparator is of type passed.
97
- # if its not a list
98
- if @pattern != '_in_list'
99
- param_suffixes.each do |p|
100
- out += "#{name}#{p} = Type.assert_#{type}(#{name}#{p})\n"
101
- end
102
- else # is list - will only have one parameter, which is a list
103
- out += "#{name}#{param_suffixes[0]}.each do |v|
104
- v = Type.assert_#{type}(v)
105
- end"
106
- end
107
- out
108
- end
109
-
110
- def to_rearrangement(name, type)
111
- out = ''
112
- if @pattern == '_in_list'
113
- out += "#{name}#{param_suffixes[0]} = #{name}#{param_suffixes[0]}.map{|el| \"'\"+el+\"'\"}.join ','"
114
- end
115
- out
116
- end
15
+
117
16
  end
118
17
  end
119
18
  end
@@ -4,114 +4,15 @@ module Specifind
4
4
  # Comparator holds the logic for each type of comparator that is use in {MethodBuilder} definition.
5
5
  #
6
6
  # The data are held in the class definition as [identifier (String), number of parameters (int), parameter suffixes (list of String), and sql creators (Procs)].
7
- class SQLite3
8
- @@comparators = []
9
- attr_accessor :pattern, :num_params, :param_suffixes, :values, :sql_proc
10
-
11
- # Getter for comparator list
12
- def self.comparators
13
- @@comparators
14
- end
15
-
16
- # Setter for comparator list
17
- def self.comparators=(c)
18
- @@comparators = c
19
- end
20
-
21
- # list of comparator's patterns
22
- def self.patterns
23
- a = []
24
- comparators.each{|c| a << c.pattern}
25
- a
26
- end
27
-
28
- # find and return a (new) comparator by it's identifying string
29
- def self.find(s)
30
- comparators.each{|c| return c.clone if c.pattern == s}
31
- nil
32
- end
33
-
34
- # From the data listed in the Comparator class definition, Comparator.generate_comparators constructs each type of Comparators
35
- # and adds it to Comparator.comparators
36
- def self.generate_comparators
37
- self.comparators_data.each{|c| c = SQLite3.new :pattern => c[0], :num_params => c[1], :param_suffixes => c[2], :sql_proc => c[3] }
38
- end
7
+ class SQLite3 < Base
39
8
 
40
9
  def self.comparators_data
41
- [
42
- ['_in_list', 1, %w(_list), Proc.new{|v| "in (#{v[0]})"}],
43
- ['_less_than_equals', 1, %w(_val), Proc.new{|v| "<= #{v[0]}"}],
44
- ['_less_than', 1, %w(_val), Proc.new{|v| "< #{v[0]}"}],
45
- ['_greater_than_equals', 1, %w(_val), Proc.new{|v| ">= #{v[0]}"}],
46
- ['_greater_than', 1, %w(_val), Proc.new{|v| "> #{v[0]}"}],
10
+ super + [
47
11
  ['_like', 1, %w(_val), Proc.new{|v| "like #{v[0]}"}],
48
- ['_ilike', 1, %w(_val), Proc.new{|v| "like #{v[0]}"}],
49
- ['_not_equal', 1, %w(_val), Proc.new{|v| "!= #{v[0]}"}],
50
- ['_between', 2, %w(_bound_one _bound_two), Proc.new{|v| "between #{v[0]} and #{v[1]}"}],
51
- ['_is_not_null', 0, [], Proc.new{|v| "is not null"}],
52
- ['_is_null', 0, [], Proc.new{|v| "is null"}],
53
- ['_equals', 1, %w(_val), Proc.new{|v| "= #{v[0]}"}]
12
+ ['_ilike', 1, %w(_val), Proc.new{|v| "like #{v[0]}"}]
54
13
  ]
55
14
  end
56
15
 
57
- def initialize(args)
58
- @pattern = args[:pattern]
59
- @num_params = args[:num_params]
60
- @param_suffixes = @num_params > 0 ? args[:param_suffixes] : []
61
- @sql_proc = args[:sql_proc]
62
- SQLite3.comparators << self
63
- end
64
-
65
- # Creates the values list for this comparator so that when it's build_sql method is called,
66
- # the sql building proc will know how to build the appropriate where clause
67
- def set_values(property_name, attribute_hash)
68
- @values = []
69
- param_suffixes.each{|s| @values << attribute_hash["#{property_name}#{s}".to_sym]} if num_params > 0
70
- end
71
-
72
- # builds sql (through @sql_proc) for values that corresponds with this type of comparator
73
- def to_where(name, remove_quotes = false)
74
- #raise 'values for comparator are not defined' if !values
75
- response = @sql_proc.call param_suffixes.map{|p| "\#{#{name}#{p}}" }
76
- response.delete! "'" if remove_quotes
77
- response
78
- end
79
-
80
- # builds signature (the parameter definitions for a method) based on the type of suffixes
81
- # that are required for this type of Comparator. See {AttributePhrase#to_signature} for further detail
82
- def to_signature(name)
83
- param_suffixes.map{|p| "#{name}#{p}" }.join(',')
84
- end
85
-
86
- # builds hash component (the parameters for the search_by method) based on the type of suffixes
87
- # that are required for this type of Comparator. See {AttributePhrase#to_params} for further detail
88
- def to_params(name)
89
- param_suffixes.map { |p| ":#{name}#{p} => #{name}#{p}" }.join(',')
90
- end
91
-
92
- def to_type_test(name, type)
93
- out = ""
94
- # assert that each variable associated with this comparator is of type passed.
95
- # if its not a list
96
- if @pattern != '_in_list'
97
- param_suffixes.each do |p|
98
- out += "#{name}#{p} = Type.assert_#{type}(#{name}#{p})\n"
99
- end
100
- else # is list - will only have one parameter, which is a list
101
- out += "#{name}#{param_suffixes[0]}.each do |v|
102
- v = Type.assert_#{type}(v)
103
- end"
104
- end
105
- out
106
- end
107
-
108
- def to_rearrangement(name, type)
109
- out = ''
110
- if @pattern == '_in_list'
111
- out += "#{name}#{param_suffixes[0]} = #{name}#{param_suffixes[0]}.map{|el| '\"'+el+'\"'}.join ','"
112
- end
113
- out
114
- end
115
16
  end
116
17
  end
117
18
  end
@@ -6,6 +6,7 @@ module Specifind
6
6
  module Comparators
7
7
  extend ActiveSupport::Autoload
8
8
 
9
+ autoload :Base
9
10
  autoload :SQLite3
10
11
  autoload :Mysql2
11
12
  autoload :PostgreSQL
@@ -1,3 +1,3 @@
1
1
  module Specifind
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/spec/spec.sqlite3 CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: specifind
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -56,6 +56,7 @@ extensions: []
56
56
  extra_rdoc_files: []
57
57
  files:
58
58
  - lib/specifind/attribute_phrase.rb
59
+ - lib/specifind/comparators/base.rb
59
60
  - lib/specifind/comparators/mysql2.rb
60
61
  - lib/specifind/comparators/postgre_sql.rb
61
62
  - lib/specifind/comparators/sq_lite3.rb