bidu-active_ext 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b0a3c10ac6c8653000fe43a0ff515a0cac920530
4
+ data.tar.gz: d9124a57c1bec0a3fa8bb12dad7556d1f001c13f
5
+ SHA512:
6
+ metadata.gz: 85e8eb34aea7bbaf41c48e4f2aaa423d7f3a7ded0df1c8dbb956a7b680a83e96b84430d8cc8f4563e8111f6235430b4e2ad73d4e9b1331331390fc67fdfd9866
7
+ data.tar.gz: 12c2009562dd6f3072152170b059252cfc365491fec1f468b6bbe8da1bc46b245f2269acf243c6f4eabd9382aa81857402b343423ccff18a7af76c5f95dacec7
@@ -0,0 +1 @@
1
+ coverage
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in credential_builder.gemspec
4
+ gemspec
5
+
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bidu-active_ext (1.0.0)
5
+ activesupport
6
+ bidu-core_ext
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.2.3)
12
+ activesupport (= 4.2.3)
13
+ builder (~> 3.1)
14
+ activerecord (4.2.3)
15
+ activemodel (= 4.2.3)
16
+ activesupport (= 4.2.3)
17
+ arel (~> 6.0)
18
+ activesupport (4.2.3)
19
+ i18n (~> 0.7)
20
+ json (~> 1.7, >= 1.7.7)
21
+ minitest (~> 5.1)
22
+ thread_safe (~> 0.3, >= 0.3.4)
23
+ tzinfo (~> 1.1)
24
+ arel (6.0.2)
25
+ bidu-core_ext (1.0.0)
26
+ activesupport
27
+ builder (3.2.2)
28
+ diff-lcs (1.2.5)
29
+ docile (1.1.5)
30
+ i18n (0.7.0)
31
+ json (1.8.3)
32
+ minitest (5.7.0)
33
+ multi_json (1.10.1)
34
+ rake (10.3.2)
35
+ rspec (2.99.0)
36
+ rspec-core (~> 2.99.0)
37
+ rspec-expectations (~> 2.99.0)
38
+ rspec-mocks (~> 2.99.0)
39
+ rspec-core (2.99.2)
40
+ rspec-expectations (2.99.2)
41
+ diff-lcs (>= 1.1.3, < 2.0)
42
+ rspec-mocks (2.99.2)
43
+ simplecov (0.9.1)
44
+ docile (~> 1.1.0)
45
+ multi_json (~> 1.0)
46
+ simplecov-html (~> 0.8.0)
47
+ simplecov-html (0.8.0)
48
+ sqlite3 (1.3.10)
49
+ thread_safe (0.3.5)
50
+ tzinfo (1.2.2)
51
+ thread_safe (~> 0.1)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ activerecord
58
+ bidu-active_ext!
59
+ bundler (~> 1.6)
60
+ rake
61
+ rspec (~> 2.14)
62
+ rspec-mocks
63
+ simplecov
64
+ sqlite3
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Bidu.com.br
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,2 @@
1
+ Active_Ext
2
+ ==========
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
7
+ task test: :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bidu/active_ext/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'bidu-active_ext'
8
+ spec.version = Bidu::ActiveExt::VERSION
9
+ spec.authors = ['Bidu Developers']
10
+ spec.email = ['dev@bidu.com.br']
11
+ spec.summary = 'Active Extensions'
12
+
13
+ spec.files = `git ls-files -z`.split("\x0")
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_runtime_dependency 'activesupport'
19
+ spec.add_runtime_dependency 'bidu-core_ext'
20
+
21
+ spec.add_development_dependency 'activerecord'
22
+ spec.add_development_dependency 'sqlite3'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.6'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rspec', '~> 2.14'
27
+ spec.add_development_dependency 'rspec-mocks'
28
+ spec.add_development_dependency 'simplecov'
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'active_record'
2
+
3
+ module ActiveRecord
4
+ class Relation
5
+ def percentage(*filters)
6
+ return 0 if count == 0
7
+
8
+ if filters.first.is_a?(Symbol)
9
+ filtered = filters.inject(self) do |relation, scope|
10
+ relation.public_send(scope)
11
+ end
12
+ else
13
+ filtered = where(*filters)
14
+ end
15
+
16
+ filtered.count * 1.0 / count
17
+ end
18
+
19
+ def pluck_as_json(*keys)
20
+ keys.empty? ? map(&:as_json) : pluck(*keys).map { |i| i.as_hash(keys) }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ module Bidu
2
+ require 'bidu/active_ext'
3
+ end
4
+
@@ -0,0 +1,8 @@
1
+ require 'bidu/core_ext'
2
+ require 'active_record/relation_ext'
3
+
4
+ module Bidu
5
+ module ActiveExt
6
+ require 'bidu/active_ext/version'
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ module Bidu
2
+ module ActiveExt
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::Relation do
4
+ describe '#percentage' do
5
+ let(:error_number) { 1 }
6
+ let(:success_number) { 1 }
7
+
8
+ before do
9
+ Document.all.each(&:destroy)
10
+ error_number.times { Document.create(status: :error) }
11
+ success_number.times { Document.create(status: :success) }
12
+ end
13
+
14
+ context 'when there are 50% documents with error' do
15
+ it do
16
+ expect(Document.all.percentage(status: :error)).to eq(0.5)
17
+ end
18
+ end
19
+
20
+ context 'when there are 25% documents with error' do
21
+ let(:success_number) { 3 }
22
+
23
+ it do
24
+ expect(Document.all.percentage(status: :error)).to eq(0.25)
25
+ end
26
+ end
27
+
28
+ context 'when passing a sub scope' do
29
+ before do
30
+ Document.create(status: :on_going)
31
+ end
32
+
33
+ it 'does the math inside the scope' do
34
+ expect(Document.where(status: [:error, :success]).percentage(status: :error)).to eq(0.5)
35
+ end
36
+ end
37
+
38
+ context 'when passing a scope name instead of query' do
39
+ it 'does the math inside the scope' do
40
+ expect(Document.all.percentage(:with_error)).to eq(0.5)
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#pluck_as_json' do
46
+ let(:json) { Document.all.pluck_as_json(:id, :status) }
47
+ let(:expected) do
48
+ [ { id: 1, status: 'error' }, { id: 2, status: 'success' } ]
49
+ end
50
+
51
+ before do
52
+ Document.all.each(&:destroy)
53
+ Document.create(id: 1, status: :error)
54
+ Document.create(id: 2, status: :success)
55
+ end
56
+
57
+ it 'returns an array of hashes' do
58
+ expect(json).to eq(expected)
59
+ end
60
+
61
+ context 'when no arguments are given' do
62
+ let(:keys) do
63
+ Document.all.pluck_as_json.first.keys
64
+ end
65
+ let(:expected) {%w(id status updated_at created_at)}
66
+
67
+ it 'returns all keys' do
68
+ expect(keys).to match_array(expected)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,36 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.profiles.define 'gem' do
4
+ add_filter '/spec/'
5
+ end
6
+
7
+ SimpleCov.start 'gem'
8
+ require 'bidu/active_ext'
9
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
10
+
11
+ # This file was generated by the `rspec --init` command. Conventionally, all
12
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
13
+ # Require this file using `require "spec_helper"` to ensure that it is only
14
+ # loaded once.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+
18
+ # Requires supporting ruby files with custom matchers and macros, etc,
19
+ # in spec/support/ and its subdirectories.
20
+ support_files = File.expand_path("spec/support/**/*.rb")
21
+ Dir[support_files].each { |file| require file }
22
+
23
+ RSpec.configure do |config|
24
+ config.treat_symbols_as_metadata_keys_with_true_values = true
25
+ config.run_all_when_everything_filtered = true
26
+ config.filter_run :focus
27
+
28
+ # Run specs in random order to surface order dependencies. If you find an
29
+ # order dependency and want to debug it, you can fix the order by providing
30
+ # the seed, which is printed after each run.
31
+ # --seed 1234
32
+ config.order = 'random'
33
+
34
+ config.before do
35
+ end
36
+ end
@@ -0,0 +1,4 @@
1
+ class Document < ActiveRecord::Base
2
+ scope :with_error, proc { where(status: :error) }
3
+ scope :with_success, proc { where(status: :success) }
4
+ end
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :documents, :force => true do |t|
5
+ t.string :status
6
+ t.timestamps null: true
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bidu-active_ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bidu Developers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bidu-core_ext
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '2.14'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '2.14'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-mocks
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email:
141
+ - dev@bidu.com.br
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - .gitignore
147
+ - .rspec
148
+ - Gemfile
149
+ - Gemfile.lock
150
+ - LICENSE
151
+ - README.md
152
+ - Rakefile
153
+ - active_ext.gemspec
154
+ - lib/active_record/relation_ext.rb
155
+ - lib/bidu.rb
156
+ - lib/bidu/active_ext.rb
157
+ - lib/bidu/active_ext/version.rb
158
+ - spec/lib/active_record/relation_spec.rb
159
+ - spec/spec_helper.rb
160
+ - spec/support/models/document.rb
161
+ - spec/support/schema.rb
162
+ homepage:
163
+ licenses: []
164
+ metadata: {}
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - '>='
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 2.4.6
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: Active Extensions
185
+ test_files:
186
+ - spec/lib/active_record/relation_spec.rb
187
+ - spec/spec_helper.rb
188
+ - spec/support/models/document.rb
189
+ - spec/support/schema.rb