query_methods_extend 0.0.4 → 0.0.5

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: d367aa7df76a32f4ec16a1ce7c2f3c687215bd35
4
- data.tar.gz: 44b34c1a6410a6022bff5e67e41737e075075894
3
+ metadata.gz: 458610213fb0ca54d4fc08f62d83d1aaafd07753
4
+ data.tar.gz: 73f79ee1edd5f50f9702126b70edd987c2aada66
5
5
  SHA512:
6
- metadata.gz: e9b5d15eeb893fbe8fd438d944f859c27061ca55d5a6942f328fd952d5ee8172bbe147d10711fd3cb1bf51c3132d6851e113bac7e4845e1891f1ea3b1d6703aa
7
- data.tar.gz: 20e0c7b62fc6ef090238a6f59320e986f58558f16c773c2b62238ed42f646aace78f624ecfe400ca78d800da344000a16826dfc65020bf62f58c8b92e7c3f226
6
+ metadata.gz: 7b53316ac1c3840e54730ece5d26223b41d3f3ac0e2d85040d1bfeb289f2e946f69fa7db8f197fcee8d703d569b0dba6c0b7f9c1cf8ed118916f2f71912c0184
7
+ data.tar.gz: 7d6d43fd4bd310842e3f5a5b9da7557fd2e2e7f85442288cccc2506c7631b8afd4d3efb5225b58b466f2ede4b3614462f38edd62077dd8a10e7e51be956a6951
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -1,14 +1,13 @@
1
1
  require "query_methods_extend/version"
2
- require "query_methods_extend/basic"
3
2
  require "query_methods_extend/association"
4
3
  require "query_methods_extend/or"
5
4
  require "query_methods_extend/like"
6
5
  require "query_methods_extend/operators"
7
6
  require "query_methods_extend/union"
7
+ require "query_methods_extend/activerecord"
8
8
 
9
9
  module QueryMethodsExtend extend ActiveSupport::Concern
10
10
  included do
11
- include Basic
12
11
  include Association
13
12
  include OrQuery
14
13
  include Like
@@ -16,3 +15,5 @@ module QueryMethodsExtend extend ActiveSupport::Concern
16
15
  include Union
17
16
  end
18
17
  end
18
+
19
+
@@ -0,0 +1,13 @@
1
+ module ActiveRecord
2
+ module QueryMethods
3
+ include QueryMethodsExtend::Association
4
+
5
+ def method_missing(method_name, *args, &block)
6
+ if respond_to? method_name
7
+ super
8
+ else
9
+ check_association_exist(method_name) || super
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,33 +1,27 @@
1
1
  module QueryMethodsExtend
2
- module Association
3
- @check_association_exist_flag = false
2
+ module Association extend ActiveSupport::Concern
3
+ included do
4
+ @check_association_exist_flag = false
4
5
 
5
- def method_missing(method_name, *args, &block)
6
- if respond_to? method_name
7
- super
8
- else
9
- check_association_exist(method_name) || super
10
- end
11
- end
6
+ def self.check_association_exist method_name
7
+ if @check_association_exist_flag
8
+ return false
9
+ end
12
10
 
13
- def check_association_exist method_name
14
- if @check_association_exist_flag
15
- return false
16
- end
11
+ @check_association_exist_flag = true
17
12
 
18
- @check_association_exist_flag = true
13
+ association = self.try(:reflect_on_association, method_name)
14
+ result = false
15
+ if association && association.macro == :has_many
16
+ model_reflect = association.inverse_of
17
+ model = model_reflect.active_record
18
+ query = self.select(association.primary_key_column.name)
19
+ result = model.where("#{model.table_name}.#{model_reflect.foreign_key.to_sym} IN (#{query.to_sql})")
20
+ end
19
21
 
20
- association = self.try(:reflect_on_association, method_name)
21
- result = false
22
- if association && association.macro == :has_many
23
- model_reflect = association.inverse_of
24
- model = model_reflect.active_record
25
- query = self.select(association.primary_key_column.name)
26
- result = model.where("#{model.table_name}.#{model_reflect.foreign_key.to_sym} IN (#{query.to_sql})")
22
+ @check_association_exist_flag = false
23
+ return result
27
24
  end
28
-
29
- @check_association_exist_flag = false
30
- return result
31
25
  end
32
26
  end
33
27
  end
@@ -32,7 +32,7 @@ module QueryMethodsExtend
32
32
  end
33
33
  return items
34
34
  else
35
- raise 'Agruments should be a HASH'
35
+ raise "Like method with agruments should be a HASH"
36
36
  end
37
37
  }
38
38
  end
@@ -30,7 +30,7 @@ module QueryMethodsExtend
30
30
  end
31
31
  return items
32
32
  else
33
- raise 'Agruments should be a HASH'
33
+ raise "Operators '#{@extend_operator_string}' with agruments should be a HASH"
34
34
  end
35
35
  }
36
36
  end
@@ -39,7 +39,7 @@ module QueryMethodsExtend
39
39
  raise 'Agruments should be a HASH'
40
40
  end
41
41
  else
42
- all.set_is_query_or(true)
42
+ all.extending(OrExtend).set_is_query_or(true)
43
43
  end
44
44
  }
45
45
  end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+
3
+ module QueryMethodsExtend
4
+ describe Association do
5
+ before :all do
6
+ run_migrate
7
+ end
8
+
9
+ describe "Or methods" do
10
+ before :all do
11
+ @store1 = Store.create(address: 'Vietnam')
12
+ @store2 = Store.create(address: 'USA')
13
+ @store3 = Store.create(address: 'Japan')
14
+
15
+ @cate1 = @store1.categories.create(name: 'Php')
16
+ @cate2 = @store1.categories.create(name: 'Rails')
17
+ @cate3 = @store1.categories.create(name: 'C#')
18
+ @cate4 = @store2.categories.create(name: 'Java')
19
+ @cate5 = @store3.categories.create(name: 'Unknow')
20
+
21
+ @book1 = @cate1.books.create(name: 'Ruby on rails advance', price: 10)
22
+ @book2 = @cate1.books.create(name: 'PHP advance', price: 15)
23
+ @book3 = @cate2.books.create(name: 'Java every day', price: 20)
24
+ @book4 = @cate2.books.create(name: 'C# 24 days', price: 25)
25
+ @book5 = @cate4.books.create(name: 'Unknown', price: 30)
26
+ end
27
+
28
+ it "return 3 categories" do
29
+ result = Store.where('stores.address = ? OR stores.address = ?', 'Vietnam', 'USA').categories
30
+ expect(result).to match_array([@cate1, @cate2, @cate3, @cate4])
31
+ end
32
+
33
+ it "return 4 books" do
34
+ result = Store.where(address: 'Vietnam').categories.books
35
+ expect(result).to match_array([@book1, @book2, @book3, @book4])
36
+ end
37
+
38
+ it "return 2 books" do
39
+ result = Store.where(address: 'Vietnam').categories.books.where('books.price <= 15')
40
+ expect(result).to match_array([@book1, @book2])
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,61 @@
1
+ require "spec_helper"
2
+
3
+ module QueryMethodsExtend
4
+ describe Like do
5
+ before :all do
6
+ run_migrate
7
+ end
8
+
9
+ describe "Like methods" do
10
+ before :all do
11
+ @first = Book.create(name: 'Ruby on rails advance', price: 10)
12
+ @seconds = Book.create(name: 'PHP advance', price: 20)
13
+ end
14
+
15
+ context 'like method' do
16
+ it "return first book" do
17
+ expect(Book.like(name: 'rails')).to match_array(@first)
18
+ end
19
+ it "return seconds book" do
20
+ expect(Book.like(name: 'PHP')).to match_array(@seconds)
21
+ end
22
+ it "return first and seconds book" do
23
+ expect(Book.like(name: 'advance')).to match_array([@first, @seconds])
24
+ end
25
+ end
26
+
27
+ context 'left like method' do
28
+ it "return first book" do
29
+ expect(Book.l_like(name: 'rails advance')).to match_array(@first)
30
+ end
31
+ it "return seconds book" do
32
+ expect(Book.l_like(name: 'PHP advance')).to match_array(@seconds)
33
+ end
34
+ it "return first and seconds book" do
35
+ expect(Book.l_like(name: 'advance')).to match_array([@first, @seconds])
36
+ end
37
+ end
38
+
39
+ context 'right like method' do
40
+ it "return first book" do
41
+ expect(Book.r_like(name: 'Ruby')).to match_array(@first)
42
+ end
43
+ it "return seconds book" do
44
+ expect(Book.r_like(name: 'PHP')).to match_array(@seconds)
45
+ end
46
+ end
47
+
48
+ context 'regex like method' do
49
+ it "return first book" do
50
+ expect(Book.regex_like(name: '%rails%')).to match_array(@first)
51
+ end
52
+ it "return seconds book" do
53
+ expect(Book.regex_like(name: '%PHP%')).to match_array(@seconds)
54
+ end
55
+ it "return first and seconds book" do
56
+ expect(Book.regex_like(name: '%advance')).to match_array([@first, @seconds])
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ module QueryMethodsExtend
4
+ describe Operators do
5
+ before :all do
6
+ run_migrate
7
+ end
8
+
9
+ describe "Operators methods" do
10
+ before :all do
11
+ @first = Book.create(name: 'Ruby on rails advance', price: 10)
12
+ @seconds = Book.create(name: 'PHP advance', price: 20)
13
+ end
14
+
15
+ context 'like method' do
16
+ it "return match array is true" do
17
+ expect(Book.lt(price: 15)).to match_array(@first)
18
+ expect(Book.gt(price: 15)).to match_array(@seconds)
19
+ expect(Book.lteq(price: 10)).to match_array(@first)
20
+ expect(Book.gteq(price: 10)).to match_array([@first, @seconds])
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ require "spec_helper"
2
+
3
+ module QueryMethodsExtend
4
+ describe OrExtend do
5
+ before :all do
6
+ run_migrate
7
+ end
8
+
9
+ describe "Or methods" do
10
+ before :all do
11
+ @first = Book.create(name: 'Ruby on rails advance', price: 10)
12
+ @seconds = Book.create(name: 'PHP advance', price: 10)
13
+ @three = Book.create(name: 'C# Programmer', price: 5)
14
+ end
15
+
16
+ context 'or with agruments' do
17
+ it "return seconds and three books" do
18
+ expect(Book.or(name: 'PHP advance', price: 5)).to match_array([@seconds, @three])
19
+ end
20
+ end
21
+
22
+ context 'or no agurments' do
23
+ it "return first and seconds book" do
24
+ expect(Book.where(name: 'PHP advance').or.where(price: 5)).to match_array([@seconds, @three])
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ module QueryMethodsExtend
4
+ describe Union do
5
+ before :all do
6
+ run_migrate
7
+ end
8
+
9
+ describe "Union methods" do
10
+ before :all do
11
+ @first = Book.create(name: 'Ruby on rails advance', price: 10)
12
+ @seconds = Book.create(name: 'PHP advance', price: 10)
13
+ @three = Book.create(name: 'C# Programmer', price: 5)
14
+ end
15
+
16
+ it "return seconds and three books" do
17
+ result = Book.where(name: 'PHP advance').union(Book.where(price: 5))
18
+ expect(result).to match_array([@seconds, @three])
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module QueryMethodsExtend
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = QueryMethodsExtend::VERSION
9
9
  spec.authors = 'Alicuche'
10
10
  spec.email = 'alicuche@gmail.com'
11
- spec.summary = 'Query methods extend in rails 4: Asscociation has_many, union, or, like, operators'
11
+ spec.summary = 'Query methods extend in rails 4: Asscociation has_many, union, or, like, operators...'
12
12
  spec.description = ''
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
@@ -18,7 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "activerecord", ">= 3.0"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.7"
22
24
  spec.add_development_dependency "rake", "~> 10.0"
23
- # spec.add_development_dependency "activerecord", "~> 4.0"
25
+ spec.add_development_dependency "rspec", "~> 3.1"
26
+ spec.add_development_dependency 'sqlite3'
24
27
  end
@@ -0,0 +1,4 @@
1
+ class Book < ActiveRecord::Base
2
+ include QueryMethodsExtend
3
+ belongs_to :category
4
+ end
@@ -0,0 +1,5 @@
1
+ class Category < ActiveRecord::Base
2
+ include QueryMethodsExtend
3
+ belongs_to :store
4
+ has_many :books
5
+ end
@@ -0,0 +1,4 @@
1
+ class Store < ActiveRecord::Base
2
+ include QueryMethodsExtend
3
+ has_many :categories
4
+ end
@@ -0,0 +1,91 @@
1
+ require "active_record"
2
+ require "supports/migrate"
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
54
+ # For more details, see:
55
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58
+ # config.disable_monkey_patching!
59
+
60
+ # This setting enables warnings. It's recommended, but in some cases may
61
+ # be too noisy due to issues in dependencies.
62
+ # config.warnings = true
63
+
64
+ # Many RSpec users commonly either run the entire suite or an individual
65
+ # file, and it's useful to allow more verbose output when running an
66
+ # individual spec file.
67
+ if config.files_to_run.one?
68
+ # Use the documentation formatter for detailed output,
69
+ # unless a formatter has already been configured
70
+ # (e.g. via a command-line flag).
71
+ # config.default_formatter = 'doc'
72
+ end
73
+
74
+ # Print the 10 slowest examples and example groups at the
75
+ # end of the spec run, to help surface which specs are running
76
+ # particularly slow.
77
+ # config.profile_examples = 10
78
+
79
+ # Run specs in random order to surface order dependencies. If you find an
80
+ # order dependency and want to debug it, you can fix the order by providing
81
+ # the seed, which is printed after each run.
82
+ # --seed 1234
83
+ config.order = :random
84
+
85
+ # Seed global randomization in this process using the `--seed` CLI option.
86
+ # Setting this allows you to use `--seed` to deterministically reproduce
87
+ # test failures related to randomization by passing the same `--seed` value
88
+ # as the one that triggered the failure.
89
+ # Kernel.srand config.seed
90
+
91
+ end
@@ -0,0 +1,40 @@
1
+ require "query_methods_extend"
2
+ require "query_methods_extend/basic"
3
+ require "query_methods_extend/association"
4
+ require "query_methods_extend/like"
5
+ require "query_methods_extend/operators"
6
+ require "query_methods_extend/or"
7
+ require "query_methods_extend/union"
8
+ require 'models/book.rb'
9
+ require 'models/category.rb'
10
+ require 'models/store.rb'
11
+
12
+ def run_migrate
13
+ ActiveRecord::Base.establish_connection(
14
+ adapter: 'sqlite3',
15
+ database: ':memory:'
16
+ )
17
+
18
+ ActiveRecord::Schema.define do
19
+ create_table :books do |t|
20
+ t.string :name
21
+ t.integer :price
22
+ t.references :category
23
+
24
+ t.timestamps
25
+ end
26
+
27
+ create_table :categories do |t|
28
+ t.string :name
29
+ t.references :store
30
+
31
+ t.timestamps
32
+ end
33
+
34
+ create_table :stores do |t|
35
+ t.string :address
36
+
37
+ t.timestamps
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: query_methods_extend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alicuche
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-25 00:00:00.000000000 Z
11
+ date: 2015-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,34 @@ dependencies:
38
52
  - - ~>
39
53
  - !ruby/object:Gem::Version
40
54
  version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
41
83
  description: ''
42
84
  email: alicuche@gmail.com
43
85
  executables: []
@@ -45,20 +87,31 @@ extensions: []
45
87
  extra_rdoc_files: []
46
88
  files:
47
89
  - .gitignore
90
+ - .rspec
48
91
  - Gemfile
49
92
  - LICENSE
50
93
  - LICENSE.txt
51
94
  - README.md
52
95
  - Rakefile
53
96
  - lib/query_methods_extend.rb
97
+ - lib/query_methods_extend/activerecord.rb
54
98
  - lib/query_methods_extend/association.rb
55
- - lib/query_methods_extend/basic.rb
56
99
  - lib/query_methods_extend/like.rb
57
100
  - lib/query_methods_extend/operators.rb
58
101
  - lib/query_methods_extend/or.rb
102
+ - lib/query_methods_extend/spec/association.rb
103
+ - lib/query_methods_extend/spec/like_spec.rb
104
+ - lib/query_methods_extend/spec/operators.rb
105
+ - lib/query_methods_extend/spec/or.rb
106
+ - lib/query_methods_extend/spec/union.rb
59
107
  - lib/query_methods_extend/union.rb
60
108
  - lib/query_methods_extend/version.rb
61
109
  - query_methods_extend.gemspec
110
+ - spec/models/book.rb
111
+ - spec/models/category.rb
112
+ - spec/models/store.rb
113
+ - spec/spec_helper.rb
114
+ - spec/supports/migrate.rb
62
115
  homepage: ''
63
116
  licenses:
64
117
  - MIT
@@ -83,5 +136,10 @@ rubygems_version: 2.4.5
83
136
  signing_key:
84
137
  specification_version: 4
85
138
  summary: 'Query methods extend in rails 4: Asscociation has_many, union, or, like,
86
- operators'
87
- test_files: []
139
+ operators...'
140
+ test_files:
141
+ - spec/models/book.rb
142
+ - spec/models/category.rb
143
+ - spec/models/store.rb
144
+ - spec/spec_helper.rb
145
+ - spec/supports/migrate.rb
@@ -1,9 +0,0 @@
1
- module QueryMethodsExtend
2
- module Basic extend ActiveSupport::Concern
3
- included do
4
- def self.where(opts, *rest)
5
- super(opts, *rest).all.extending(Association, OrExtend)
6
- end
7
- end
8
- end
9
- end