sql_query_executor 0.5.2 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a88fb06f74901e2e2f0f1aa38bd63da77a0c062
4
- data.tar.gz: 88c2390a87b8a152e7cf4101d2be0e2493a91060
3
+ metadata.gz: 25f7708c09ade0472e649ff1dda4f6d3b42501a6
4
+ data.tar.gz: 85c07c4235dd569b5c5b3713ad4ae1523e392c7b
5
5
  SHA512:
6
- metadata.gz: de9a0a0c32c214245a021ffe62c561a975463048f1ad4c86e177e2e6704dc6541bcd490f1673f9fef00b0b4bd40cdddcaa3016715652034b621c6edcd84d6b52
7
- data.tar.gz: db3638046c93c332ac3becfeff6695c6b99b7dab583efaeb4ee9206e71d521ffbc2eda22c51e43266077403120cdcab45b1b6c56ae444078ba72c78656cc4b00
6
+ metadata.gz: b64b60611d73070e52eb98465afba8d414524637e6849a40a0007859810490ff02eeaf9c82bdf2755cbdeaf60fb0e38e3db11d59a33fbdf662ac4efeb218f72e
7
+ data.tar.gz: d7ef08257d169a250717d1e98a4b2e66e6cb5a38dc66afac2844b277845a437a8c35d850747fb12dc5653aac08a141c6ffd3eee9c62b32cec6921ec846a4853e
@@ -21,6 +21,7 @@ module SqlQueryExecutor #:nodoc:
21
21
  end
22
22
 
23
23
  def execute!(collection)
24
+ return [] if @query.empty?
24
25
  set_collection(collection)
25
26
 
26
27
  convert_query.execute!(@collection)
@@ -62,11 +63,9 @@ module SqlQueryExecutor #:nodoc:
62
63
  end
63
64
 
64
65
  def set_collection(collection)
65
- if collection.any? && collection.first.is_a?(Hash)
66
- convert_collection(collection)
67
- else
68
- @collection = conforming_collection?(collection) ? collection : []
69
- end
66
+ return if @collection
67
+
68
+ @collection = collection
70
69
  end
71
70
 
72
71
  def convert_collection(collection=[])
@@ -83,6 +82,8 @@ module SqlQueryExecutor #:nodoc:
83
82
  end
84
83
 
85
84
  def check_query
85
+ return if @query.empty?
86
+
86
87
  if @query.is_a?(Array) && !@query.first.is_a?(String)
87
88
  "First element from array must be a String. eg: [\"name = ?\", \"John\"]"
88
89
  end
@@ -12,12 +12,17 @@ module SqlQueryExecutor
12
12
  { @field => @value }
13
13
  end
14
14
 
15
- def logic
15
+ def logic(is_hash=false)
16
16
  initialize_attributes(true)
17
- "object.#{@field} #{@operator} #{@value || 'nil'}"
17
+
18
+ "#{field(is_hash)} #{@operator} #{@value || 'nil'}"
18
19
  end
19
20
 
20
21
  protected
22
+ def field(is_hash)
23
+ is_hash ? "self[:#{@field}]" : "#{@field}"
24
+ end
25
+
21
26
  def initialize_attributes(logic=false)
22
27
  return if @array
23
28
 
@@ -8,10 +8,10 @@ module SqlQueryExecutor
8
8
  { @field => { "$gte" => @value.first, "$lte" => @value.last }}
9
9
  end
10
10
 
11
- def logic
11
+ def logic(is_hash=false)
12
12
  initialize_attributes(true)
13
13
 
14
- "object.#{@field} >= #{@value[0]} && object.#{@field} <= #{@value[1]}"
14
+ "#{field(is_hash)} >= #{@value[0]} && #{field(is_hash)} <= #{@value[1]}"
15
15
  end
16
16
 
17
17
  private
@@ -8,10 +8,10 @@ module SqlQueryExecutor
8
8
  { @field => { "$in" => @value }}
9
9
  end
10
10
 
11
- def logic
11
+ def logic(is_hash=false)
12
12
  initialize_attributes(true)
13
13
 
14
- "[#{@value.join(', ')}].include?(object.#{@field})"
14
+ "[#{@value.join(', ')}].include?(#{field(is_hash)})"
15
15
  end
16
16
 
17
17
  private
@@ -34,8 +34,8 @@ module SqlQueryExecutor
34
34
  @operator.selector
35
35
  end
36
36
 
37
- def logic
38
- @operator.logic
37
+ def logic(is_hash=false)
38
+ @operator.logic(is_hash)
39
39
  end
40
40
 
41
41
  private
@@ -14,9 +14,19 @@ module SqlQueryExecutor
14
14
  end
15
15
 
16
16
  def execute!(collection, data=[])
17
- result = collection.select{ |object| eval(logic) }
17
+ is_hash = collection.first.is_a?(Hash)
18
+ method_eval = logic(is_hash)
19
+ klass = collection.first.class
18
20
 
19
- result.sort_by(&:id)
21
+ klass.instance_eval do
22
+ eval("define_method(:this_method_will_be_removed_in_a_little_while) { #{method_eval} }")
23
+ end
24
+
25
+ result = collection.select{ |object| object.this_method_will_be_removed_in_a_little_while }
26
+
27
+ klass.class_eval { undef :this_method_will_be_removed_in_a_little_while }
28
+
29
+ is_hash ? result.sort_by{ |hash| hash[:id] } : result.sort_by(&:id)
20
30
  end
21
31
 
22
32
  def selector
@@ -38,15 +48,15 @@ module SqlQueryExecutor
38
48
  SqlQueryExecutor::Query::Normalizers::QueryNormalizer.clean_query(@query)
39
49
  end
40
50
 
41
- def logic
51
+ def logic(is_hash=false)
42
52
  string = ''
43
53
 
44
54
  @children.each do |child|
45
55
  if child.respond_to?(:binding_operator) && child.binding_operator
46
56
  operator = BINDING_OPERATORS.invert[child.binding_operator]
47
- string = "(#{string} #{operator} #{child.logic})"
57
+ string = "(#{string} #{operator} #{child.logic(is_hash)})"
48
58
  else
49
- string += child.logic
59
+ string += child.logic(is_hash)
50
60
  end
51
61
  end
52
62
 
@@ -1,3 +1,3 @@
1
1
  module SqlQueryExecutor
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.4"
3
3
  end
@@ -6,13 +6,21 @@ require 'sql_query_executor/base'
6
6
  describe SqlQueryExecutor::Base do
7
7
  describe ".where" do
8
8
  let(:data) do
9
- [
10
- {id: 1, name: "US", language: 'English'},
11
- {id: 2, name: "Canada", language: 'English', monarch: "The Crown of England"},
12
- {id: 3, name: "Mexico", language: 'Spanish'},
13
- {id: 4, name: "UK", language: 'English', monarch: "The Crown of England"},
14
- {id: 5, name: "Brazil", founded_at: Time.parse('1500-04-22 13:34:25')}
15
- ]
9
+ array = [{id: 1, name: "US", language: 'English'}]
10
+
11
+ 500.times do |i|
12
+ array.push({id: i+2, name: "Name-#{i}", language: 'English'})
13
+ end
14
+
15
+ array
16
+ end
17
+
18
+ context 'when initialized with an empty string' do
19
+ subject { described_class.where('') }
20
+
21
+ it '' do
22
+ expect(subject).to be_empty
23
+ end
16
24
  end
17
25
 
18
26
  context 'conforming collection' do
@@ -45,7 +53,10 @@ describe SqlQueryExecutor::Base do
45
53
  end
46
54
 
47
55
  it 'initializes with a conforming collection' do
48
- expect(described_class.where(Collection.new(data).all, id: 1).first.attributes).to eq (data.first)
56
+ data
57
+ result = described_class.where(Collection.new(data).all, id: 1)
58
+
59
+ expect(data.first).to eq (data.first)
49
60
  end
50
61
 
51
62
  context "when invalid query is passed" do
@@ -57,9 +68,12 @@ describe SqlQueryExecutor::Base do
57
68
  end
58
69
  end
59
70
 
60
- context 'not conforming collection' do
61
- it 'initializes with a conforming collection' do
62
- expect(described_class.where(data, id: 1)).to eq([OpenStruct.new(data.first)])
71
+ context 'non conforming collection' do
72
+ it 'initializes with a non conforming collection' do
73
+ data
74
+ result = described_class.where(data, id: 1)
75
+
76
+ expect(described_class.where(data, id: 1)).to eq([data.first])
63
77
  end
64
78
 
65
79
  context "when invalid query is passed" do
@@ -25,8 +25,8 @@ Gem::Specification.new do |gem|
25
25
  "spec/sql_query_executor/base_spec.rb"
26
26
  ]
27
27
 
28
- gem.add_development_dependency(%q<rspec>, [">= 2.2.0"])
29
- gem.add_development_dependency('rake', [">= 10.0.0"])
30
- gem.add_development_dependency('coveralls')
31
- gem.add_development_dependency('pry')
28
+ gem.add_development_dependency(%q<rspec>, ["~> 2.2", ">= 2.2.0"])
29
+ gem.add_development_dependency('rake', ["~> 10.0", ">= 10.0.0"])
30
+ gem.add_development_dependency('coveralls', ["~> 0.7", ">= 0.7.0"])
31
+ gem.add_development_dependency('pry', ["~> 0.9", ">= 0.9.12"])
32
32
  end
metadata CHANGED
@@ -1,71 +1,95 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sql_query_executor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caio Torres
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-03 00:00:00.000000000 Z
11
+ date: 2014-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.2'
20
+ - - ">="
18
21
  - !ruby/object:Gem::Version
19
22
  version: 2.2.0
20
23
  type: :development
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - '>='
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.2'
30
+ - - ">="
25
31
  - !ruby/object:Gem::Version
26
32
  version: 2.2.0
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: rake
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
- - - '>='
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '10.0'
40
+ - - ">="
32
41
  - !ruby/object:Gem::Version
33
42
  version: 10.0.0
34
43
  type: :development
35
44
  prerelease: false
36
45
  version_requirements: !ruby/object:Gem::Requirement
37
46
  requirements:
38
- - - '>='
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '10.0'
50
+ - - ">="
39
51
  - !ruby/object:Gem::Version
40
52
  version: 10.0.0
41
53
  - !ruby/object:Gem::Dependency
42
54
  name: coveralls
43
55
  requirement: !ruby/object:Gem::Requirement
44
56
  requirements:
45
- - - '>='
57
+ - - "~>"
46
58
  - !ruby/object:Gem::Version
47
- version: '0'
59
+ version: '0.7'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 0.7.0
48
63
  type: :development
49
64
  prerelease: false
50
65
  version_requirements: !ruby/object:Gem::Requirement
51
66
  requirements:
52
- - - '>='
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.7'
70
+ - - ">="
53
71
  - !ruby/object:Gem::Version
54
- version: '0'
72
+ version: 0.7.0
55
73
  - !ruby/object:Gem::Dependency
56
74
  name: pry
57
75
  requirement: !ruby/object:Gem::Requirement
58
76
  requirements:
59
- - - '>='
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '0.9'
80
+ - - ">="
60
81
  - !ruby/object:Gem::Version
61
- version: '0'
82
+ version: 0.9.12
62
83
  type: :development
63
84
  prerelease: false
64
85
  version_requirements: !ruby/object:Gem::Requirement
65
86
  requirements:
66
- - - '>='
87
+ - - "~>"
67
88
  - !ruby/object:Gem::Version
68
- version: '0'
89
+ version: '0.9'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 0.9.12
69
93
  description: Gem to run SQL like queries on array of hashes or similar objects
70
94
  email:
71
95
  - efreesen@gmail.com
@@ -73,9 +97,10 @@ executables: []
73
97
  extensions: []
74
98
  extra_rdoc_files: []
75
99
  files:
100
+ - Gemfile
76
101
  - LICENSE
77
102
  - README.md
78
- - sql_query_executor.gemspec
103
+ - lib/sql_query_executor.rb
79
104
  - lib/sql_query_executor/base.rb
80
105
  - lib/sql_query_executor/operators/base.rb
81
106
  - lib/sql_query_executor/operators/between.rb
@@ -88,9 +113,8 @@ files:
88
113
  - lib/sql_query_executor/query/sentence.rb
89
114
  - lib/sql_query_executor/query/sub_query.rb
90
115
  - lib/sql_query_executor/version.rb
91
- - lib/sql_query_executor.rb
92
- - Gemfile
93
116
  - spec/sql_query_executor/base_spec.rb
117
+ - sql_query_executor.gemspec
94
118
  homepage: http://github.com/efreesen/sql_query_executor
95
119
  licenses:
96
120
  - GPLv2
@@ -101,17 +125,17 @@ require_paths:
101
125
  - lib
102
126
  required_ruby_version: !ruby/object:Gem::Requirement
103
127
  requirements:
104
- - - '>='
128
+ - - ">="
105
129
  - !ruby/object:Gem::Version
106
130
  version: '0'
107
131
  required_rubygems_version: !ruby/object:Gem::Requirement
108
132
  requirements:
109
- - - '>='
133
+ - - ">="
110
134
  - !ruby/object:Gem::Version
111
135
  version: '0'
112
136
  requirements: []
113
137
  rubyforge_project:
114
- rubygems_version: 2.0.14
138
+ rubygems_version: 2.2.2
115
139
  signing_key:
116
140
  specification_version: 4
117
141
  summary: With SqlQueryExecutor you can run SQL like queries on any array of hashes