shorter 0.0.1 → 0.0.3

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: d677baf08aa64682295d46025b184a232387bdb8
4
- data.tar.gz: f988dc031d7acf6b0871424d689f323295deb602
3
+ metadata.gz: f9543680aa5c8b81831a98d445fab268224256a2
4
+ data.tar.gz: 0e49869a14dcacb1cb936a8c8d5d435920fe7caf
5
5
  SHA512:
6
- metadata.gz: 24cd640919045d17ad7238a2f9024201655a93fb8a7430685392cc69c87c775625d893f84a57cd5c40e180ec62790618fafdb847746bc72794e3001fcd662e5b
7
- data.tar.gz: 9946635e1ed42e2e7b3bb954fa187386da5e074b1f645bdfe5bdfe5e816a8c0af4ab2d62925f0d7fde32586cd512ff8d3c2283ad6bb8a66dd4aad2e3ddb7c6e9
6
+ metadata.gz: 9f45d68d93d9a43f2dd1430d931dc05f06b7b9cb7d9b01021528ef25db02b0cf4cdbd80b6ee0c475e6fc6141ef46dd5f8991267d5a62ebe704806501c82ba673
7
+ data.tar.gz: 6d5a26f85ab7a0ae8df2380ad6896f06b7d6e1a83cd04c9199895dd8ab3ff2c4f5434570c1a260ce476b2b49f3b085c60324916b010fdc44d7b219f116ea4c62
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ *.swp
3
+ pkg
4
+ rdoc
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rspec-rails', '>= 3.1'
7
+ gem 'rspec', '>= 3.1'
8
+ end
@@ -0,0 +1,88 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ shorter (0.0.3)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionpack (4.2.5)
10
+ actionview (= 4.2.5)
11
+ activesupport (= 4.2.5)
12
+ rack (~> 1.6)
13
+ rack-test (~> 0.6.2)
14
+ rails-dom-testing (~> 1.0, >= 1.0.5)
15
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
16
+ actionview (4.2.5)
17
+ activesupport (= 4.2.5)
18
+ builder (~> 3.1)
19
+ erubis (~> 2.7.0)
20
+ rails-dom-testing (~> 1.0, >= 1.0.5)
21
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
22
+ activesupport (4.2.5)
23
+ i18n (~> 0.7)
24
+ json (~> 1.7, >= 1.7.7)
25
+ minitest (~> 5.1)
26
+ thread_safe (~> 0.3, >= 0.3.4)
27
+ tzinfo (~> 1.1)
28
+ builder (3.2.2)
29
+ diff-lcs (1.2.5)
30
+ erubis (2.7.0)
31
+ i18n (0.7.0)
32
+ json (1.8.3)
33
+ loofah (2.0.3)
34
+ nokogiri (>= 1.5.9)
35
+ mini_portile (0.6.2)
36
+ minitest (5.8.3)
37
+ nokogiri (1.6.6.4)
38
+ mini_portile (~> 0.6.0)
39
+ rack (1.6.4)
40
+ rack-test (0.6.3)
41
+ rack (>= 1.0)
42
+ rails-deprecated_sanitizer (1.0.3)
43
+ activesupport (>= 4.2.0.alpha)
44
+ rails-dom-testing (1.0.7)
45
+ activesupport (>= 4.2.0.beta, < 5.0)
46
+ nokogiri (~> 1.6.0)
47
+ rails-deprecated_sanitizer (>= 1.0.1)
48
+ rails-html-sanitizer (1.0.2)
49
+ loofah (~> 2.0)
50
+ railties (4.2.5)
51
+ actionpack (= 4.2.5)
52
+ activesupport (= 4.2.5)
53
+ rake (>= 0.8.7)
54
+ thor (>= 0.18.1, < 2.0)
55
+ rake (10.4.2)
56
+ rspec (3.4.0)
57
+ rspec-core (~> 3.4.0)
58
+ rspec-expectations (~> 3.4.0)
59
+ rspec-mocks (~> 3.4.0)
60
+ rspec-core (3.4.1)
61
+ rspec-support (~> 3.4.0)
62
+ rspec-expectations (3.4.0)
63
+ diff-lcs (>= 1.2.0, < 2.0)
64
+ rspec-support (~> 3.4.0)
65
+ rspec-mocks (3.4.0)
66
+ diff-lcs (>= 1.2.0, < 2.0)
67
+ rspec-support (~> 3.4.0)
68
+ rspec-rails (3.4.0)
69
+ actionpack (>= 3.0, < 4.3)
70
+ activesupport (>= 3.0, < 4.3)
71
+ railties (>= 3.0, < 4.3)
72
+ rspec-core (~> 3.4.0)
73
+ rspec-expectations (~> 3.4.0)
74
+ rspec-mocks (~> 3.4.0)
75
+ rspec-support (~> 3.4.0)
76
+ rspec-support (3.4.1)
77
+ thor (0.19.1)
78
+ thread_safe (0.3.5)
79
+ tzinfo (1.2.2)
80
+ thread_safe (~> 0.1)
81
+
82
+ PLATFORMS
83
+ ruby
84
+
85
+ DEPENDENCIES
86
+ rspec (>= 3.1)
87
+ rspec-rails (>= 3.1)
88
+ shorter!
@@ -0,0 +1,34 @@
1
+ ## Shorter
2
+
3
+ An idea to reduce a piece of code.
4
+
5
+ ## Getting Started
6
+
7
+ gem install shorter
8
+
9
+ ## Usage
10
+
11
+ For example, if you have a User model with `users` as it's table name. You'll do as
12
+ below to fetch it from the database with `id` column.
13
+
14
+ ```
15
+ user = User.where(id: 1).first
16
+ ```
17
+
18
+ Using this gem, you can query it like this
19
+
20
+ ```
21
+ 1.users
22
+ ```
23
+
24
+ ## License
25
+
26
+ MIT License (MIT)
27
+
28
+ Copyright (c) 2015-2017 Vasu Adari
29
+
30
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
31
+
32
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
33
+
34
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,10 +1,7 @@
1
1
  require 'active_support'
2
2
  require 'active_model'
3
3
  require 'arel'
4
-
5
4
  require 'active_record'
6
5
 
7
6
  require_relative 'shorter/active_record'
8
-
9
- module Shorter
10
- end
7
+ require_relative 'shorter/active_record/tables_and_models'
@@ -1,10 +1,8 @@
1
- require_relative 'active_record/tables_and_models'
2
-
3
1
  module Shorter
4
2
  module ActiveRecord
5
3
  def method_missing(method_name, *args, &block)
6
4
  if ::ActiveRecord::Base.connected?
7
- model = TablesAndModels.instance.model(method_name.to_s)
5
+ model = TablesAndModels.instance.model(method_name)
8
6
  model.present? ? model.where(id: self).first : super
9
7
  else
10
8
  super
@@ -20,7 +20,7 @@ module Shorter
20
20
  end
21
21
 
22
22
  def reload!
23
- unless tables_and_models.values.count == ::ActiveRecord::Base.send(:subclasses).count
23
+ unless tables_and_models.values.size == ::ActiveRecord::Base.send(:subclasses).size
24
24
  @loaded = true
25
25
  load!
26
26
  end
@@ -28,7 +28,7 @@ module Shorter
28
28
 
29
29
  def model(table_name)
30
30
  loaded || reload!
31
- tables_and_models[table_name.to_s]
31
+ tables_and_models[table_name.to_s.pluralize]
32
32
  end
33
33
  end
34
34
  end
@@ -0,0 +1,3 @@
1
+ module Shorter
2
+ VERSION = '0.0.3'
3
+ end
@@ -0,0 +1,21 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'shorter/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = 'shorter'
7
+ gem.version = Shorter::VERSION
8
+ gem.date = '2015-11-23'
9
+ gem.summary = "shorter for rails' methods"
10
+ gem.description = 'An idea to reduce a piece of code'
11
+ gem.authors = ["Vasu Adari"]
12
+ gem.email = 'vasuakeel@gmail.com'
13
+ gem.homepage = 'https://github.com/vasuakeel/shorter'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.require_path = 'lib'
17
+
18
+ gem.license = 'MIT'
19
+
20
+ gem.required_ruby_version = '>= 2.0.0'
21
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+ require 'shorter/active_record/tables_and_models'
3
+
4
+ module ActiveRecord
5
+ class Base
6
+ end
7
+ end
8
+
9
+ describe Shorter::ActiveRecord::TablesAndModels do
10
+ let(:instance_class) { described_class.instance }
11
+
12
+ it 'should be a singleton class' do
13
+ expect(described_class.ancestors).to include(Singleton)
14
+ end
15
+
16
+ it 'responds to :tables_and_models and :loaded' do
17
+ allow(ActiveRecord::Base).to receive(:subclasses).and_return([])
18
+
19
+ expect(instance_class).to respond_to(:tables_and_models)
20
+ expect(instance_class).to respond_to(:loaded)
21
+ end
22
+
23
+ it '#load! method loads tables and models' do
24
+ models = [double(table_name: 'users')]
25
+ allow(ActiveRecord::Base).to receive(:subclasses).and_return(models)
26
+
27
+
28
+ expect(instance_class.load!).to eql({ 'users' => models.first })
29
+ end
30
+
31
+ context '#reload!' do
32
+ it 'updates cached tables and models when count differs' do
33
+ allow(ActiveRecord::Base).to receive(:subclasses).and_return([])
34
+ expect(instance_class).to receive(:load!)
35
+
36
+ instance_class.reload!
37
+ end
38
+
39
+ it 'does nothing when count matches' do
40
+ allow(ActiveRecord::Base).to receive(:subclasses).and_return([1])
41
+ expect(instance_class).to receive(:load!).never
42
+
43
+ instance_class.reload!
44
+ end
45
+ end
46
+
47
+ context '#model' do
48
+ it 'first time invokes #load! method' do
49
+ allow(instance_class).to receive(:loaded).and_return(false)
50
+ expect_any_instance_of(String).to receive(:pluralize)
51
+ expect(instance_class).to receive(:reload!)
52
+
53
+ instance_class.model('user')
54
+ end
55
+
56
+ it 'should not invoke #load! for second time' do
57
+ allow(instance_class).to receive(:loaded).and_return(true)
58
+ expect_any_instance_of(String).to receive(:pluralize)
59
+ expect(instance_class).to receive(:reload!).never
60
+
61
+ instance_class.model('user')
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shorter::ActiveRecord do
4
+
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shorter do
4
+ it 'VERSION constant is defined' do
5
+ expect(Shorter::VERSION).to eql('0.0.3')
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Shorter' do
4
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+
4
+ RSpec.configure do |config|
5
+ config.tty = true
6
+ config.color = true
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shorter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasu Adari
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-29 00:00:00.000000000 Z
11
+ date: 2015-11-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: An idea to reduce a piece of code
14
14
  email: vasuakeel@gmail.com
@@ -16,10 +16,21 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
- - lib/shorter/active_record/tables_and_models.rb
20
- - lib/shorter/active_record.rb
19
+ - .gitignore
20
+ - Gemfile
21
+ - Gemfile.lock
22
+ - README.md
21
23
  - lib/shorter.rb
22
- homepage: http://rubygems.org/gems/shorter
24
+ - lib/shorter/active_record.rb
25
+ - lib/shorter/active_record/tables_and_models.rb
26
+ - lib/shorter/version.rb
27
+ - shorter.gemspec
28
+ - spec/shorter/active_record/tables_and_models_spec.rb
29
+ - spec/shorter/active_record_spec.rb
30
+ - spec/shorter/version_spec.rb
31
+ - spec/shorter_spec.rb
32
+ - spec/spec_helper.rb
33
+ homepage: https://github.com/vasuakeel/shorter
23
34
  licenses:
24
35
  - MIT
25
36
  metadata: {}