scopy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: de96cef11e8765d24e4c521e7db2d8611db53165
4
+ data.tar.gz: eb9c002ee4993c66ecd377acb1ee57a0ee21aff6
5
+ SHA512:
6
+ metadata.gz: 55d4ef90a0c7d4f33223d6dddeca0c42c31d70e7d9610415c5b9ad0b8de1168bad5570fd7988609410dff44365c4319d32fbb873a63798dd4844425b11c745b3
7
+ data.tar.gz: 3487c2e5880668db3de01a17ef1a0d03a7cd8fca03327a1562b32a50055db6df97fc5a7bff01466f0f5637bed818d5ed4243fca49363e36dbd53f41c2cde93a5
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ Gemfile.lock
2
+ coverage
3
+ pkg
4
+ tmp
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in scopy.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Neighborland, Inc.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # Scopy
2
+
3
+ [![Build Status](https://api.travis-ci.org/neighborland/scopy.png)](https://travis-ci.org/neighborland/scopy)
4
+
5
+ Scopy provides common ActiveRecord utility scopes as ActiveSupport concerns.
6
+
7
+ Common scopes for the following attributes are provided:
8
+
9
+ * `created_at`
10
+ * `id`
11
+ * `name`
12
+
13
+ ## Install
14
+
15
+ Add this line to your Gemfile:
16
+
17
+ ```ruby
18
+ gem 'scopy'
19
+ ```
20
+
21
+ Scopy works with Rails 3.2 and later.
22
+
23
+ ## Usage
24
+
25
+ Include the concern modules you would like to use in your models:
26
+
27
+ ```ruby
28
+ class Dog < ActiveRecord::Base
29
+ include Scopy::CreatedAtScopes
30
+ include Scopy::IdScopes
31
+ ```
32
+
33
+ The following examples assume you have a model named `Dog`:
34
+
35
+ ##### Scopy::CreatedAtScopes
36
+
37
+ ```ruby
38
+ Dog.newest
39
+ # => most recently created dogs are first
40
+
41
+ Dog.oldest
42
+ # => most recently created dogs are last
43
+
44
+ Dog.created_since(1.month.ago)
45
+ # => only dogs created since one month ago
46
+
47
+ Dog.created_before(1.week.ago)
48
+ # => only dogs created before 1 week ago
49
+
50
+ Dog.created_between(2.weeks.ago, 1.week.ago)
51
+ # => only dogs created between 2 weeks ago & 1 week ago
52
+
53
+ Dog.created_on_day(1.day.ago)
54
+ # => dogs created yesterday
55
+
56
+ Dog.created_in_week(Date.parse("31/10/2013"))
57
+ # => dogs created in the week Sunday-Saturday around October 31, 2013
58
+
59
+ Dog.created_in_month(Date.parse("1/1/2013"))
60
+ # => dogs created in January 2013
61
+ ```
62
+
63
+ ##### Scopy::IdScopes
64
+
65
+ ```ruby
66
+ Dog.excluding_id(123)
67
+ # => dogs excluding id 123
68
+
69
+ Dog.excluding_ids( [1, 2, 3] )
70
+ # => dogs excluding ids 1, 2, and 3
71
+
72
+ Dog.excluding(dog)
73
+ # => dogs excluding dog
74
+ ```
75
+
76
+ ##### Scopy::NameScopes
77
+
78
+ ```ruby
79
+ Dog.name_like('snoop')
80
+ # => dogs with names containing 'snoop' (case insensitive)
81
+
82
+ Dog.name_like('snoop', case_sensitive: true)
83
+ # => dogs with names containing 'snoop' (case sensitive)
84
+
85
+ Dog.name_starts_with('snoop')
86
+ # => dogs with names starting with 'snoop' (case insensitive)
87
+
88
+ Dog.name_starts_with('snoop', case_sensitive: true)
89
+ # => dogs with names starting with 'snoop' (case sensitive)
90
+ ```
91
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task default: [:test]
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.test_files = %w(test/**/*.rb)
9
+ t.verbose = false
10
+ end
data/lib/scopy.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "scopy/version"
2
+ require "scopy/created_at_scopes"
3
+ require "scopy/id_scopes"
4
+ require "scopy/name_scopes"
5
+
@@ -0,0 +1,35 @@
1
+ module Scopy
2
+ module CreatedAtScopes
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ scope :newest, ->{ order("#{self.table_name}.created_at desc") }
7
+ scope :oldest, ->{ order("#{self.table_name}.created_at") }
8
+
9
+ scope :created_since, ->(since) do
10
+ where("#{self.table_name}.created_at > ?", since)
11
+ end
12
+
13
+ scope :created_before, ->(before) do
14
+ where("#{self.table_name}.created_at < ?", before)
15
+ end
16
+
17
+ scope :created_between, ->(from, to) do
18
+ created_since(from).created_before(to)
19
+ end
20
+
21
+ scope :created_on_day, ->(date) do
22
+ created_between date.beginning_of_day, date.end_of_day
23
+ end
24
+
25
+ scope :created_in_week, ->(date, day_of_week=:sunday) do
26
+ created_between date.beginning_of_week(day_of_week), date.end_of_week(day_of_week)
27
+ end
28
+
29
+ scope :created_in_month, ->(date) do
30
+ created_between date.beginning_of_month, date.end_of_month
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ module Scopy
2
+ module IdScopes
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ scope :excluding_id, ->(id){ where("#{self.table_name}.id <> ?", id) if id }
7
+ scope :excluding_ids, ->(ids){ where("#{self.table_name}.id NOT IN (?)", ids) if ids.any? }
8
+ scope :excluding, ->(object){ excluding_id(object.id) if object }
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ module Scopy
2
+ module NameScopes
3
+ extend ActiveSupport::Concern
4
+
5
+ # note: case-sensitive like operations do not work as expected in Sqlite,
6
+ # which we are using as the test database.
7
+ # see http://stackoverflow.com/questions/973541/how-to-set-sqlite3-to-be-case-insensitive-when-string-comparing
8
+
9
+ included do
10
+ scope :name_like, ->(text, options={}) do
11
+ cs = options[:case_sensitive]
12
+ where("#{_lhs_name_column(cs)} LIKE '%#{_rhs_name_value(text, cs)}%'")
13
+ end
14
+
15
+ scope :name_starts_with, ->(text, options={}) do
16
+ cs = options[:case_sensitive]
17
+ where("#{_lhs_name_column(cs)} LIKE '#{_rhs_name_value(text, cs)}%'")
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ module ClassMethods
24
+ def _lhs_name_column(case_sensitive)
25
+ case_sensitive ? "#{self.table_name}.name" : "LOWER(#{self.table_name}.name)"
26
+ end
27
+
28
+ def _rhs_name_value(text, case_sensitive)
29
+ case_sensitive ? text : text.try(:downcase)
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Scopy
2
+ VERSION = "0.0.1"
3
+ end
data/scopy.gemspec ADDED
@@ -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 'scopy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "scopy"
8
+ spec.version = Scopy::VERSION
9
+ spec.authors = ["Tee Parham"]
10
+ spec.email = ["tee@neighborland.com"]
11
+ spec.description = %q{Rails ActiveRecord model concerns for common scopes}
12
+ spec.summary = %q{Rails ActiveRecord common utility scopes for id, created_at, and name}
13
+ spec.homepage = "https://github.com/neighborland/scopy"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = []
18
+ spec.test_files = spec.files.grep(%r{^(test)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake", "~> 10.1"
23
+ spec.add_development_dependency "test-unit", "~> 2.5"
24
+ spec.add_development_dependency "mocha", "~> 0.14"
25
+ spec.add_development_dependency "shoulda-context", "~> 1.1"
26
+ spec.add_development_dependency "sqlite3", "~> 1.3"
27
+
28
+ spec.add_runtime_dependency "activerecord", ">= 3.2"
29
+ end
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+
3
+ class CreatedAtScopesTest < Test::Unit::TestCase
4
+ class Dog < ActiveRecord::Base
5
+ include Scopy::CreatedAtScopes
6
+ end
7
+
8
+ setup do
9
+ @boo = Dog.create(created_at: 1.week.ago)
10
+ @snoop = Dog.create(created_at: 1.day.ago)
11
+ end
12
+
13
+ teardown do
14
+ Dog.delete_all
15
+ end
16
+
17
+ context ".newest" do
18
+ should "sort newest first" do
19
+ assert_equal @snoop, Dog.newest.first
20
+ assert_equal @boo, Dog.newest.last
21
+ end
22
+ end
23
+
24
+ context ".oldest" do
25
+ should "sort oldest first" do
26
+ assert_equal @boo, Dog.oldest.first
27
+ assert_equal @snoop, Dog.oldest.last
28
+ end
29
+ end
30
+
31
+ context ".created_since" do
32
+ should "include since date" do
33
+ assert Dog.created_since(2.days.ago).include?(@snoop)
34
+ refute Dog.created_since(2.days.ago).include?(@boo)
35
+ end
36
+ end
37
+
38
+ context ".created_before" do
39
+ should "include before date" do
40
+ refute Dog.created_before(2.days.ago).include?(@snoop)
41
+ assert Dog.created_before(2.days.ago).include?(@boo)
42
+ end
43
+ end
44
+
45
+ context ".created_between" do
46
+ should "include between dates" do
47
+ assert Dog.created_between(2.days.ago, Time.now).include?(@snoop)
48
+ refute Dog.created_between(2.days.ago, Time.now).include?(@boo)
49
+ end
50
+ end
51
+
52
+ context ".created_on_day" do
53
+ should "include on day" do
54
+ assert Dog.created_since(2.days.ago).include?(@snoop)
55
+ refute Dog.created_since(2.days.ago).include?(@boo)
56
+ end
57
+ end
58
+
59
+ context ".created_in_week" do
60
+ should "include in week" do
61
+ lassie = Dog.create(created_at: 1.month.ago)
62
+ assert Dog.created_in_week(1.day.ago).include?(@snoop)
63
+ refute Dog.created_in_week(1.day.ago).include?(lassie)
64
+ end
65
+ end
66
+
67
+ context ".created_in_month" do
68
+ should "include in month" do
69
+ lassie = Dog.create(created_at: 2.months.ago)
70
+ assert Dog.created_in_month(1.day.ago).include?(@snoop)
71
+ refute Dog.created_in_month(1.day.ago).include?(lassie)
72
+ end
73
+ end
74
+
75
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ class IdScopesTest < Test::Unit::TestCase
4
+ class Dog < ActiveRecord::Base
5
+ include Scopy::IdScopes
6
+ end
7
+
8
+ setup do
9
+ @dog = Dog.create
10
+ end
11
+
12
+ teardown do
13
+ Dog.delete_all
14
+ end
15
+
16
+ context ".excluding_id" do
17
+ should "exclude" do
18
+ assert_empty Dog.excluding_id(@dog.id)
19
+ end
20
+
21
+ should "not exclude" do
22
+ assert_equal [@dog], Dog.excluding_id(@dog.id + 1)
23
+ end
24
+ end
25
+
26
+ context ".excluding_ids" do
27
+ should "exclude" do
28
+ assert_empty Dog.excluding_ids([@dog.id])
29
+ end
30
+
31
+ should "not exclude" do
32
+ assert_equal [@dog], Dog.excluding_ids([@dog.id + 1])
33
+ end
34
+ end
35
+
36
+ context ".excluding" do
37
+ should "exclude" do
38
+ assert_empty Dog.excluding(@dog)
39
+ end
40
+
41
+ should "not exclude" do
42
+ snoop = Dog.create
43
+ assert_equal [@dog], Dog.excluding(snoop)
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ class NameScopesTest < Test::Unit::TestCase
4
+ class Dog < ActiveRecord::Base
5
+ include Scopy::NameScopes
6
+ end
7
+
8
+ setup do
9
+ @dog = Dog.create(name: "Snoop Dogg")
10
+ end
11
+
12
+ teardown do
13
+ Dog.delete_all
14
+ end
15
+
16
+ context ".name_like" do
17
+ should "find case sensitive" do
18
+ assert Dog.name_like("Dog", case_sensitive: true).include?(@dog)
19
+ assert_empty Dog.name_like("cat", case_sensitive: true)
20
+ end
21
+
22
+ should "find case insensitive" do
23
+ assert Dog.name_like("doG").include?(@dog)
24
+ assert_empty Dog.name_like("cat")
25
+ end
26
+ end
27
+
28
+ context ".name_starts_with" do
29
+ should "find case sensitive" do
30
+ assert Dog.name_starts_with("Snoop", case_sensitive: true).include?(@dog)
31
+ assert_empty Dog.name_starts_with("Dogg", case_sensitive: true)
32
+ end
33
+
34
+ should "find case insensitive" do
35
+ assert Dog.name_starts_with("snOOp").include?(@dog)
36
+ assert_empty Dog.name_starts_with("dogg")
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,24 @@
1
+ require 'test/unit'
2
+ require 'shoulda-context'
3
+ require 'mocha/setup'
4
+ require 'active_support'
5
+ require 'active_record'
6
+ require 'sqlite3'
7
+ require 'scopy'
8
+
9
+ ActiveRecord::Base.logger = Logger.new(STDERR) if ENV["VERBOSE"]
10
+
11
+ ActiveRecord::Base.establish_connection(
12
+ adapter: "sqlite3",
13
+ database: ":memory:"
14
+ )
15
+
16
+ unless ActiveRecord::Base.connection.tables.include? 'dogs'
17
+ ActiveRecord::Schema.define do
18
+ create_table :dogs do |table|
19
+ table.column :created_at, :datetime
20
+ table.column :name, :string
21
+ end
22
+ end
23
+ end
24
+
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scopy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tee Parham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '0.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: shoulda-context
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '1.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '3.2'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '3.2'
111
+ description: Rails ActiveRecord model concerns for common scopes
112
+ email:
113
+ - tee@neighborland.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .ruby-version
120
+ - .travis.yml
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - lib/scopy.rb
126
+ - lib/scopy/created_at_scopes.rb
127
+ - lib/scopy/id_scopes.rb
128
+ - lib/scopy/name_scopes.rb
129
+ - lib/scopy/version.rb
130
+ - scopy.gemspec
131
+ - test/created_at_scopes_test.rb
132
+ - test/id_scopes_test.rb
133
+ - test/name_scopes_test.rb
134
+ - test/test_helper.rb
135
+ homepage: https://github.com/neighborland/scopy
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.1.5
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Rails ActiveRecord common utility scopes for id, created_at, and name
159
+ test_files:
160
+ - test/created_at_scopes_test.rb
161
+ - test/id_scopes_test.rb
162
+ - test/name_scopes_test.rb
163
+ - test/test_helper.rb