darthjee-active_ext 1.2.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: 61c0f7192c30180cba2bd3c6a85d3892ab46ea98
4
+ data.tar.gz: 6e51dd5b85691b1b13e9b68e419c1bb1f4e95ec3
5
+ SHA512:
6
+ metadata.gz: 68baae3ba89d2b91ae486248095572c457da021080e0715250aefd101f787f3dba620754f1443048eb2a3d239779c17ec0943294a96b85c8e08415a85bb5d416
7
+ data.tar.gz: e1d096d8f57e84c555849fbb36cc566534c81de6ef006f72251df6511840cb0628efe1d717d0c903718754254493715bc218023077178a741265e035491090da
@@ -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
+ darthjee-active_ext (1.2.0)
5
+ activesupport (~> 5.1.1)
6
+ darthjee-core_ext (~> 1.2.6)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.1.1)
12
+ activesupport (= 5.1.1)
13
+ activerecord (5.1.1)
14
+ activemodel (= 5.1.1)
15
+ activesupport (= 5.1.1)
16
+ arel (~> 8.0)
17
+ activesupport (5.1.1)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (~> 0.7)
20
+ minitest (~> 5.1)
21
+ tzinfo (~> 1.1)
22
+ arel (8.0.0)
23
+ concurrent-ruby (1.0.5)
24
+ darthjee-core_ext (1.2.6)
25
+ activesupport (~> 5.1.1)
26
+ diff-lcs (1.2.5)
27
+ docile (1.1.5)
28
+ i18n (0.8.4)
29
+ json (2.1.0)
30
+ minitest (5.10.2)
31
+ rake (11.3.0)
32
+ rspec (2.99.0)
33
+ rspec-core (~> 2.99.0)
34
+ rspec-expectations (~> 2.99.0)
35
+ rspec-mocks (~> 2.99.0)
36
+ rspec-core (2.99.2)
37
+ rspec-expectations (2.99.2)
38
+ diff-lcs (>= 1.1.3, < 2.0)
39
+ rspec-mocks (2.99.4)
40
+ simplecov (0.14.1)
41
+ docile (~> 1.1.0)
42
+ json (>= 1.8, < 3)
43
+ simplecov-html (~> 0.10.0)
44
+ simplecov-html (0.10.1)
45
+ sqlite3 (1.3.10)
46
+ thread_safe (0.3.6)
47
+ tzinfo (1.2.3)
48
+ thread_safe (~> 0.1)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ activerecord (~> 5.1.1)
55
+ bundler (~> 1.6)
56
+ darthjee-active_ext!
57
+ rake (~> 11.3.0)
58
+ rspec (~> 2.14)
59
+ rspec-mocks (~> 2.99.4)
60
+ simplecov (~> 0.14.1)
61
+ sqlite3
62
+
63
+ BUNDLED WITH
64
+ 1.15.1
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,94 @@
1
+ Active_Ext
2
+ ==========
3
+
4
+ [![Code Climate](https://codeclimate.com/github/darthjee/active_ext/badges/gpa.svg)](https://codeclimate.com/github/darthjee/active_ext)
5
+ [![Test Coverage](https://codeclimate.com/github/darthjee/active_ext/badges/coverage.svg)](https://codeclimate.com/github/darthjee/active_ext/coverage)
6
+ [![Issue Count](https://codeclimate.com/github/darthjee/active_ext/badges/issue_count.svg)](https://codeclimate.com/github/darthjee/active_ext)
7
+
8
+ # Usage
9
+ This project adds some new methods to the core active_record classes
10
+
11
+ To use active-ext either intall directly
12
+
13
+ ```console
14
+ gem install darthjee-active_ext
15
+ ```
16
+
17
+ or add it to Gemfile
18
+
19
+ ```
20
+ gem 'darthjee-active_ext'
21
+ ```
22
+
23
+ ```console
24
+ bundle install darthjee-active_ext
25
+ ```
26
+
27
+ # methods added
28
+
29
+ ## ActiveRecord::Relation
30
+
31
+ ```ruby
32
+ ActiveRecord::Schema.define do
33
+ self.verbose = false
34
+
35
+ create_table :documents, :force => true do |t|
36
+ t.string :status
37
+ t.boolean :active, default: false
38
+ t.timestamps null: true
39
+ end
40
+ end
41
+
42
+
43
+ class Document < ActiveRecord::Base
44
+ scope :with_error, -> { where(status: :error) }
45
+ scope :with_success, -> { where(status: :success) }
46
+ scope :active, -> { where(active: true) }
47
+ end
48
+
49
+ 2.times { Document.with_error.create }
50
+ Document.active.with_error.create
51
+ Document.active.with_success.create
52
+ ```
53
+
54
+ ### #percentage
55
+ Returns the percentage of objects of a certain scope within another scope
56
+
57
+ ```ruby
58
+ Document.percentage(:with_error)
59
+ 0.75
60
+ ```
61
+
62
+ ```ruby
63
+ Document.percentage(status: :error)
64
+ 0.75
65
+ ```
66
+
67
+ ```ruby
68
+ Document.percentage("status = 'error'")
69
+ 0.75
70
+ ```
71
+ Works also when using nested scopes
72
+
73
+ ```ruby
74
+ Document.active.percentage(:with_error)
75
+ 0.5
76
+ ```
77
+
78
+ ### #pluck_as_json
79
+ Just as pluck returns some specifc columns, pluck_as_json returns the same coluns with keys to identify
80
+
81
+ ```ruby
82
+ Document.pluck(:id, :active)
83
+ [[1, true], [2, true]]
84
+ ```
85
+
86
+ ```ruby
87
+ Document.pluck_as_json(:id, :active)
88
+ [
89
+ {id: 18, active: false},
90
+ {id: 19, active: false},
91
+ {id: 20, active: true},
92
+ {id: 21, active: true}
93
+ ]
94
+ ```
@@ -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 'darthjee/active_ext/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'darthjee-active_ext'
8
+ spec.version = Darthjee::ActiveExt::VERSION
9
+ spec.authors = ['Darthjee']
10
+ spec.email = ['darthjee@gmail.com']
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', '~> 5.1.1'
19
+ spec.add_runtime_dependency 'darthjee-core_ext', '~> 1.2.6'
20
+
21
+ spec.add_development_dependency 'activerecord', '~> 5.1.1'
22
+ spec.add_development_dependency 'sqlite3'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.6'
25
+ spec.add_development_dependency 'rake', '~> 11.3.0'
26
+ spec.add_development_dependency 'rspec', '~> 2.14'
27
+ spec.add_development_dependency 'rspec-mocks', '~> 2.99.4'
28
+ spec.add_development_dependency 'simplecov', '~> 0.14.1'
29
+ end
@@ -0,0 +1,16 @@
1
+ dependencies:
2
+ post:
3
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
4
+ - chmod +x ./cc-test-reporter
5
+
6
+
7
+ database:
8
+ override:
9
+ - echo "Skipping DB section."
10
+
11
+ test:
12
+ pre:
13
+ - bundle install
14
+ - ./cc-test-reporter before-build
15
+ override:
16
+ - bundle exec rspec; ./cc-test-reporter after-build --exit-code $?
@@ -0,0 +1,10 @@
1
+ require 'active_record'
2
+
3
+ module ActiveRecord
4
+ class Base
5
+ class << self
6
+ delegate :percentage, :pluck_as_json, to: :all
7
+ end
8
+ end
9
+ end
10
+
@@ -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 Darthjee
2
+ require 'darthjee/active_ext'
3
+ end
4
+
@@ -0,0 +1,9 @@
1
+ require 'darthjee/core_ext'
2
+ require 'active_record/relation_ext'
3
+ require 'active_record/base_ext'
4
+
5
+ module Darthjee
6
+ module ActiveExt
7
+ require 'darthjee/active_ext/version'
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Darthjee
2
+ module ActiveExt
3
+ VERSION = '1.2.0'
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::Relation do
4
+ let(:subject) { Document }
5
+
6
+ describe '.percentage' do
7
+ it_behaves_like 'a method that returns the percentage of objects found'
8
+ end
9
+
10
+ describe '.pluck_as_json' do
11
+ it_behaves_like 'a method that works as pluck but returning the keys'
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::Relation do
4
+ let(:subject) { Document.all }
5
+
6
+ describe '#percentage' do
7
+ it_behaves_like 'a method that returns the percentage of objects found'
8
+ end
9
+
10
+ describe '#pluck_as_json' do
11
+ it_behaves_like 'a method that works as pluck but returning the keys'
12
+ end
13
+ 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 'darthjee/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,5 @@
1
+ class Document < ActiveRecord::Base
2
+ scope :with_error, -> { where(status: :error) }
3
+ scope :with_success, -> { where(status: :success) }
4
+ scope :active, -> { where(active: true) }
5
+ end
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :documents, :force => true do |t|
5
+ t.string :status
6
+ t.boolean :active, default: false
7
+ t.timestamps null: true
8
+ end
9
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'a method that returns the percentage of objects found' do
4
+ let(:error_number) { 1 }
5
+ let(:success_number) { 1 }
6
+ let(:active_error_number) { 0 }
7
+ let(:active_success_number) { 0 }
8
+
9
+ before do
10
+ Document.delete_all
11
+ error_number.times { Document.create(status: :error) }
12
+ success_number.times { Document.create(status: :success) }
13
+ active_error_number.times { Document.active.create(status: :error) }
14
+ active_success_number.times { Document.active.create(status: :success) }
15
+ end
16
+
17
+ context 'when there are 50% documents with error' do
18
+ it do
19
+ expect(subject.percentage(status: :error)).to eq(0.5)
20
+ end
21
+ end
22
+
23
+ context 'when there are 25% documents with error' do
24
+ let(:success_number) { 3 }
25
+
26
+ it do
27
+ expect(subject.percentage(status: :error)).to eq(0.25)
28
+ end
29
+ end
30
+
31
+ context 'when passing a sub scope' do
32
+ let(:active_error_number) { 1 }
33
+ let(:active_success_number) { 4 }
34
+
35
+ it 'does the math inside the scope' do
36
+ expect(Document.active.percentage(:with_error)).to eq(0.2)
37
+ end
38
+ end
39
+
40
+ context 'when passing a string with SQL query' do
41
+ it 'returns the correct percentage' do
42
+ expect(subject.percentage("status ='error'")).to eq(0.5)
43
+ end
44
+ end
45
+
46
+ context 'when passing a scope name instead of query' do
47
+ it 'returns the correct percentage' do
48
+ expect(subject.percentage(:with_error)).to eq(0.5)
49
+ end
50
+ end
51
+ end
52
+
53
+ shared_examples 'a method that works as pluck but returning the keys' do
54
+ let(:json) { subject.pluck_as_json(:id, :status) }
55
+ let(:expected) do
56
+ [ { id: 1, status: 'error' }, { id: 2, status: 'success' } ]
57
+ end
58
+
59
+ before do
60
+ Document.delete_all
61
+ Document.create(id: 1, status: :error)
62
+ Document.create(id: 2, status: :success)
63
+ end
64
+
65
+ it 'returns an array of hashes' do
66
+ expect(json).to eq(expected)
67
+ end
68
+
69
+ context 'when no arguments are given' do
70
+ let(:keys) do
71
+ subject.pluck_as_json.first.keys
72
+ end
73
+ let(:expected) {%w(id status updated_at created_at active)}
74
+
75
+ it 'returns all keys' do
76
+ expect(keys).to match_array(expected)
77
+ end
78
+ end
79
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: darthjee-active_ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Darthjee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-21 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: 5.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: darthjee-core_ext
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.2.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.1.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.1.1
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: 11.3.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 11.3.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: 2.99.4
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 2.99.4
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.14.1
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.14.1
139
+ description:
140
+ email:
141
+ - darthjee@gmail.com
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
+ - circle.yml
155
+ - lib/active_record/base_ext.rb
156
+ - lib/active_record/relation_ext.rb
157
+ - lib/darthjee.rb
158
+ - lib/darthjee/active_ext.rb
159
+ - lib/darthjee/active_ext/version.rb
160
+ - spec/lib/active_record/base_spec.rb
161
+ - spec/lib/active_record/relation_spec.rb
162
+ - spec/spec_helper.rb
163
+ - spec/support/models/document.rb
164
+ - spec/support/schema.rb
165
+ - spec/support/shared_examples/active_relation.rb
166
+ homepage:
167
+ licenses: []
168
+ metadata: {}
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubyforge_project:
185
+ rubygems_version: 2.6.8
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: Active Extensions
189
+ test_files:
190
+ - spec/lib/active_record/base_spec.rb
191
+ - spec/lib/active_record/relation_spec.rb
192
+ - spec/spec_helper.rb
193
+ - spec/support/models/document.rb
194
+ - spec/support/schema.rb
195
+ - spec/support/shared_examples/active_relation.rb