lazy_model 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +38 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +58 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lazy_model.gemspec +74 -0
- data/lib/lazy_model/lazy_boolean.rb +36 -0
- data/lib/lazy_model/lazy_model.rb +31 -0
- data/lib/lazy_model/lazy_model_support.rb +13 -0
- data/lib/lazy_model/lazy_string.rb +121 -0
- data/lib/lazy_model/lazy_text.rb +1 -0
- data/lib/lazy_model.rb +21 -0
- data/test/helper.rb +24 -0
- data/test/lazy_model.sqlite3 +0 -0
- data/test/support/data.rb +4 -0
- data/test/support/models.rb +8 -0
- data/test/support/schema.rb +9 -0
- data/test/test_lazy_model.rb +113 -0
- metadata +136 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
gem "activerecord", "~> 3.0"
|
7
|
+
gem "activesupport", "~> 3.0"
|
8
|
+
|
9
|
+
# Add dependencies to develop your gem here.
|
10
|
+
# Include everything needed to run rake, tests, features, etc.
|
11
|
+
group :development do
|
12
|
+
gem "shoulda", ">= 0"
|
13
|
+
gem "bundler", "~> 1.0.0"
|
14
|
+
gem "jeweler", "~> 1.6.4"
|
15
|
+
gem 'sqlite3-ruby'
|
16
|
+
end
|
17
|
+
|
18
|
+
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.10)
|
5
|
+
activesupport (= 3.0.10)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.5.0)
|
8
|
+
activerecord (3.0.10)
|
9
|
+
activemodel (= 3.0.10)
|
10
|
+
activesupport (= 3.0.10)
|
11
|
+
arel (~> 2.0.10)
|
12
|
+
tzinfo (~> 0.3.23)
|
13
|
+
activesupport (3.0.10)
|
14
|
+
arel (2.0.10)
|
15
|
+
builder (2.1.2)
|
16
|
+
git (1.2.5)
|
17
|
+
i18n (0.5.0)
|
18
|
+
jeweler (1.6.4)
|
19
|
+
bundler (~> 1.0)
|
20
|
+
git (>= 1.2.5)
|
21
|
+
rake
|
22
|
+
rake (0.9.2.2)
|
23
|
+
shoulda (2.11.3)
|
24
|
+
sqlite3 (1.3.4)
|
25
|
+
sqlite3-ruby (1.3.3)
|
26
|
+
sqlite3 (>= 1.3.3)
|
27
|
+
tzinfo (0.3.29)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
activerecord (~> 3.0)
|
34
|
+
activesupport (~> 3.0)
|
35
|
+
bundler (~> 1.0.0)
|
36
|
+
jeweler (~> 1.6.4)
|
37
|
+
shoulda
|
38
|
+
sqlite3-ruby
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 AcademicWorks, inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
= lazy_model
|
2
|
+
|
3
|
+
More documentation to come. Check the tests to see all the permutations.
|
4
|
+
|
5
|
+
If you have boolean attributes on a model or a text/string attribute and a set list of values (think enumerable), then you can get several finders & instance methods for free. For instance:
|
6
|
+
|
7
|
+
|
8
|
+
== Examples
|
9
|
+
|
10
|
+
class Post < ActiveRecord::Base
|
11
|
+
|
12
|
+
CHOICES = ["one", "two", "three"]
|
13
|
+
|
14
|
+
lazy_model :archived #boolean
|
15
|
+
lazy_model :choice, Post::CHOICES, {:prime => ["one", "three"]} #string
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
Post.prime.archived #composable query where choice is in "one", "two" and archived is true
|
20
|
+
|
21
|
+
Post.one.nil_archived #composable query where choice is equal to "one" and archived is nil
|
22
|
+
|
23
|
+
Post.not_two.not_archived # composable query where choice is not equal to two and archived is false
|
24
|
+
|
25
|
+
Post.choice # composable query of all posts where choice is not nill
|
26
|
+
|
27
|
+
post = Post.new
|
28
|
+
|
29
|
+
post.one? #true if post.choice == "one"
|
30
|
+
post.prime? #true if ["one", "three"].include?(post.choice)
|
31
|
+
|
32
|
+
|
33
|
+
== Restrictions
|
34
|
+
|
35
|
+
This is a ActiveRecord 3.x party.
|
36
|
+
|
37
|
+
|
38
|
+
== Notes
|
39
|
+
|
40
|
+
The next simple addition is to add date support.
|
41
|
+
|
42
|
+
Even cooler would be to add some support for intellegent association methods. I'd love to get some feedback on this. What would that api look like?
|
43
|
+
|
44
|
+
== Contributing to lazy_model
|
45
|
+
|
46
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
47
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
48
|
+
* Fork the project
|
49
|
+
* Start a feature/bugfix branch
|
50
|
+
* Commit and push until you are happy with your contribution
|
51
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
52
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
53
|
+
|
54
|
+
== Copyright
|
55
|
+
|
56
|
+
Copyright (c) 2011 AcademicWorks, inc.. See LICENSE.txt for
|
57
|
+
further details.
|
58
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "lazy_model"
|
18
|
+
gem.homepage = "http://github.com/AcademicWorks/lazy_model"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{write common active_record methods in shorthand}
|
21
|
+
gem.description = %Q{write common active_record methods in shorthand}
|
22
|
+
gem.email = "ascruggs@academicworks.com"
|
23
|
+
gem.authors = ["Aaron Scruggs"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => :test
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "lazy_model #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lazy_model.gemspec
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "lazy_model"
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Aaron Scruggs"]
|
12
|
+
s.date = "2011-12-08"
|
13
|
+
s.description = "write common active_record methods in shorthand"
|
14
|
+
s.email = "ascruggs@academicworks.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lazy_model.gemspec",
|
28
|
+
"lib/lazy_model.rb",
|
29
|
+
"lib/lazy_model/lazy_boolean.rb",
|
30
|
+
"lib/lazy_model/lazy_model.rb",
|
31
|
+
"lib/lazy_model/lazy_model_support.rb",
|
32
|
+
"lib/lazy_model/lazy_string.rb",
|
33
|
+
"lib/lazy_model/lazy_text.rb",
|
34
|
+
"test/helper.rb",
|
35
|
+
"test/lazy_model.sqlite3",
|
36
|
+
"test/support/data.rb",
|
37
|
+
"test/support/models.rb",
|
38
|
+
"test/support/schema.rb",
|
39
|
+
"test/test_lazy_model.rb"
|
40
|
+
]
|
41
|
+
s.homepage = "http://github.com/AcademicWorks/lazy_model"
|
42
|
+
s.licenses = ["MIT"]
|
43
|
+
s.require_paths = ["lib"]
|
44
|
+
s.rubygems_version = "1.8.10"
|
45
|
+
s.summary = "write common active_record methods in shorthand"
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_runtime_dependency(%q<activerecord>, ["~> 3.0"])
|
52
|
+
s.add_runtime_dependency(%q<activesupport>, ["~> 3.0"])
|
53
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
54
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
55
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
56
|
+
s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<activerecord>, ["~> 3.0"])
|
59
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0"])
|
60
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
61
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
62
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
63
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
64
|
+
end
|
65
|
+
else
|
66
|
+
s.add_dependency(%q<activerecord>, ["~> 3.0"])
|
67
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0"])
|
68
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
69
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
70
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
71
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module LazyModel
|
2
|
+
|
3
|
+
class LazyBoolean
|
4
|
+
|
5
|
+
include LazyModelSupport
|
6
|
+
|
7
|
+
def define_methods
|
8
|
+
define_class_methods
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def define_class_methods
|
14
|
+
model.class_eval <<-LZY
|
15
|
+
class << self
|
16
|
+
|
17
|
+
def #{attribute}
|
18
|
+
where(self.arel_table[:#{attribute}].eq(true))
|
19
|
+
end
|
20
|
+
|
21
|
+
def not_#{attribute}
|
22
|
+
where(self.arel_table[:#{attribute}].eq(false))
|
23
|
+
end
|
24
|
+
|
25
|
+
def nil_#{attribute}
|
26
|
+
where(self.arel_table[:#{attribute}].eq(nil))
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
LZY
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module LazyModel
|
2
|
+
|
3
|
+
class LazyModel
|
4
|
+
|
5
|
+
attr_accessor :model, :attribute, :enumerables, :custom_finders, :column
|
6
|
+
|
7
|
+
def initialize(model, attribute, enumerables = nil, custom_finders = {})
|
8
|
+
self.model = model
|
9
|
+
self.attribute = attribute
|
10
|
+
self.enumerables = Array(enumerables)
|
11
|
+
self.custom_finders = custom_finders
|
12
|
+
end
|
13
|
+
|
14
|
+
def define_methods
|
15
|
+
#make sure the column exists
|
16
|
+
unless self.column = model.columns_hash[attribute.to_s]
|
17
|
+
raise "\'#{attribute}\' is not an attribute for \'#{model.to_s}\' model"
|
18
|
+
end
|
19
|
+
|
20
|
+
#pass to lazy class id supported
|
21
|
+
begin
|
22
|
+
klass = "LazyModel::Lazy#{column.type.to_s.camelize}".constantize
|
23
|
+
klass.new(self).define_methods
|
24
|
+
rescue NameError
|
25
|
+
raise " attribute type \'#{column.type}\'' on \'#{attribute}\' is not supported "
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module LazyModelSupport
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
attr_accessor :lazy_model
|
6
|
+
delegate :model, :attribute, :enumerables, :custom_finders, :column, :to => :lazy_model
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(lazy_model)
|
10
|
+
self.lazy_model = lazy_model
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module LazyModel
|
2
|
+
|
3
|
+
class LazyString
|
4
|
+
|
5
|
+
include LazyModelSupport
|
6
|
+
|
7
|
+
def define_methods
|
8
|
+
define_instance_methods
|
9
|
+
define_class_methods
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
##### INSTANCE METHODS ##########
|
15
|
+
|
16
|
+
def define_instance_methods
|
17
|
+
define_instance_enumerables
|
18
|
+
define_instance_custom
|
19
|
+
end
|
20
|
+
|
21
|
+
def define_instance_enumerables
|
22
|
+
enumerables.each do |enumerable|
|
23
|
+
model.class_eval <<-LZY
|
24
|
+
def #{enumerable}?
|
25
|
+
#{attribute} == "#{enumerable}"
|
26
|
+
end
|
27
|
+
LZY
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def define_instance_custom
|
32
|
+
custom_finders.each do |custom_finder, values|
|
33
|
+
model.class_eval <<-LZY
|
34
|
+
def #{custom_finder}?
|
35
|
+
#{values}.include?(#{attribute})
|
36
|
+
end
|
37
|
+
LZY
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
##### CLASS METHODS ##########
|
43
|
+
|
44
|
+
def define_class_methods
|
45
|
+
define_core_class_finder_methods
|
46
|
+
define_enumerables_class_finder_methods
|
47
|
+
define_custom_class_finder_methods
|
48
|
+
end
|
49
|
+
|
50
|
+
def define_core_class_finder_methods
|
51
|
+
model.class_eval <<-LZY
|
52
|
+
class << self
|
53
|
+
|
54
|
+
def #{attribute}(value = nil)
|
55
|
+
table = self.arel_table[:#{attribute}]
|
56
|
+
|
57
|
+
filter = case value.class.to_s
|
58
|
+
when 'Array' then table.in(value)
|
59
|
+
when 'NilClass' then table.not_eq(value)
|
60
|
+
when 'String', 'Symbol' then table.eq(value)
|
61
|
+
else raise "\'" + value + "\' with class \'"+value.class.to_s+ "\' is not valid comparitor"
|
62
|
+
end
|
63
|
+
|
64
|
+
where(filter)
|
65
|
+
end
|
66
|
+
|
67
|
+
def not_#{attribute}(value = nil)
|
68
|
+
table = self.arel_table[:#{attribute}]
|
69
|
+
|
70
|
+
filter = case value.class.to_s
|
71
|
+
when 'Array' then table.not_in(value)
|
72
|
+
when 'NilClass' then table.eq(value)
|
73
|
+
when 'String', 'Symbol' then table.not_eq(value)
|
74
|
+
else raise "\'" + value + "\' with class \'"+value.class.to_s+ "\' is not valid comparitor"
|
75
|
+
end
|
76
|
+
|
77
|
+
where(filter)
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
LZY
|
82
|
+
end
|
83
|
+
|
84
|
+
def define_enumerables_class_finder_methods
|
85
|
+
enumerables.each do |enumerable|
|
86
|
+
model.class_eval <<-LZY
|
87
|
+
class << self
|
88
|
+
|
89
|
+
def #{enumerable}
|
90
|
+
#{attribute}("#{enumerable}")
|
91
|
+
end
|
92
|
+
|
93
|
+
def not_#{enumerable}
|
94
|
+
not_#{attribute}("#{enumerable}")
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
LZY
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def define_custom_class_finder_methods
|
103
|
+
custom_finders.each do |custom_finder, values|
|
104
|
+
model.class_eval <<-LZY
|
105
|
+
class << self
|
106
|
+
def #{custom_finder}
|
107
|
+
#{attribute}(#{values})
|
108
|
+
end
|
109
|
+
|
110
|
+
def not_#{custom_finder}
|
111
|
+
not_#{attribute}(#{values})
|
112
|
+
end
|
113
|
+
end
|
114
|
+
LZY
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
LazyModel::LazyText = LazyModel::LazyString
|
data/lib/lazy_model.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_support/core_ext/module/delegation'
|
4
|
+
|
5
|
+
require File.dirname(__FILE__) + '/lazy_model/lazy_model.rb'
|
6
|
+
require File.dirname(__FILE__) + '/lazy_model/lazy_model_support.rb'
|
7
|
+
|
8
|
+
require File.dirname(__FILE__) + '/lazy_model/lazy_boolean.rb'
|
9
|
+
require File.dirname(__FILE__) + '/lazy_model/lazy_string.rb'
|
10
|
+
require File.dirname(__FILE__) + '/lazy_model/lazy_text.rb'
|
11
|
+
|
12
|
+
|
13
|
+
module LazyModel
|
14
|
+
|
15
|
+
def lazy_model(attribute, enumerables = nil, custom_finders = {})
|
16
|
+
LazyModel.new(self, attribute, enumerables, custom_finders).define_methods
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
ActiveRecord::Base.extend LazyModel
|
data/test/helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'lazy_model'
|
16
|
+
|
17
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
|
18
|
+
:database => File.dirname(__FILE__) + "/lazy_model.sqlite3")
|
19
|
+
load File.dirname(__FILE__) + '/support/schema.rb'
|
20
|
+
load File.dirname(__FILE__) + '/support/models.rb'
|
21
|
+
load File.dirname(__FILE__) + '/support/data.rb'
|
22
|
+
|
23
|
+
class Test::Unit::TestCase
|
24
|
+
end
|
Binary file
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestLazyModel < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "LazyBoolean" do
|
6
|
+
|
7
|
+
should "provide truthy boolean finder" do
|
8
|
+
assert Post.respond_to?(:archived)
|
9
|
+
end
|
10
|
+
|
11
|
+
should "provide falsey boolean finder" do
|
12
|
+
assert Post.respond_to?(:not_archived)
|
13
|
+
end
|
14
|
+
|
15
|
+
should "provide nil boolean finder" do
|
16
|
+
assert Post.respond_to?(:nil_archived)
|
17
|
+
end
|
18
|
+
|
19
|
+
should "return only truthy values for truthy finder" do
|
20
|
+
assert_equal Post.archived.count, 1
|
21
|
+
end
|
22
|
+
|
23
|
+
should "return only falsey values for falsey finder" do
|
24
|
+
assert_equal Post.not_archived.count, 1
|
25
|
+
end
|
26
|
+
|
27
|
+
should "return only nil values for nil finder" do
|
28
|
+
assert_equal Post.nil_archived.count, 2
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
context "LazyString" do
|
34
|
+
|
35
|
+
context "Instance Methods" do
|
36
|
+
|
37
|
+
should "provide attribute options as question methods" do
|
38
|
+
Post::CHOICES.each do |choice|
|
39
|
+
assert Post.new.respond_to?("#{choice}?")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
should "be true if attribute is set to corresponding value" do
|
44
|
+
assert Post.new(:choice => "one").one?
|
45
|
+
end
|
46
|
+
|
47
|
+
should "be false if attribute is not set to corresponding value" do
|
48
|
+
refute Post.new(:choice => "two").one?
|
49
|
+
end
|
50
|
+
|
51
|
+
should "provide custom attribute options as question methods" do
|
52
|
+
assert Post.new.respond_to?("prime?")
|
53
|
+
end
|
54
|
+
|
55
|
+
should "be true if custom attribute is set to corresponding value" do
|
56
|
+
assert Post.new(:choice => "one").prime?
|
57
|
+
end
|
58
|
+
|
59
|
+
should "be false if custom attribute is not set to corresponding value" do
|
60
|
+
refute Post.new(:choice => "two").prime?
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
context "Class Methods" do
|
66
|
+
|
67
|
+
should "have core presence finder that accepts nil" do
|
68
|
+
assert Post.respond_to?(:choice)
|
69
|
+
end
|
70
|
+
|
71
|
+
should "have core presence finder that accepts value" do
|
72
|
+
assert Post.respond_to?(:choice, "one")
|
73
|
+
end
|
74
|
+
|
75
|
+
should "have core absence finder that accepts nil" do
|
76
|
+
assert Post.respond_to?(:not_choice)
|
77
|
+
end
|
78
|
+
|
79
|
+
should "have core absence finder that accepts value" do
|
80
|
+
assert Post.respond_to?(:not_choice, "one")
|
81
|
+
end
|
82
|
+
|
83
|
+
should "return correct records for finder w/ nil for core presence finder" do
|
84
|
+
assert Post.choice.all.select {|p| !p.choice}.empty?
|
85
|
+
end
|
86
|
+
|
87
|
+
should "return correct records for finder w/ nil for core absence finder" do
|
88
|
+
assert Post.not_choice.all.select {|p| p.choice}.empty?
|
89
|
+
end
|
90
|
+
|
91
|
+
should "return only records of enumerable type for enumerable finder" do
|
92
|
+
assert Post.one.all.select {|p| p.choice != "one" }.empty?
|
93
|
+
end
|
94
|
+
|
95
|
+
should "not return records of enumerable type for enumerable NOT finder" do
|
96
|
+
assert Post.not_one.all.select {|p| p.choice == "one" }.empty?
|
97
|
+
end
|
98
|
+
|
99
|
+
should "return only records with custom atributes for finder" do
|
100
|
+
assert Post.prime.all.select {|p| !["one", "three"].include?(p.choice) }.empty?
|
101
|
+
end
|
102
|
+
|
103
|
+
should "return not records with custom atributes for NOT finder" do
|
104
|
+
assert Post.not_prime.all.select {|p| ["one", "three"].include?(p.choice) }.empty?
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazy_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Aaron Scruggs
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &70241510566480 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70241510566480
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
requirement: &70241510564900 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70241510564900
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: shoulda
|
38
|
+
requirement: &70241510563900 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70241510563900
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: &70241510579240 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.0.0
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70241510579240
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: jeweler
|
60
|
+
requirement: &70241510577040 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 1.6.4
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70241510577040
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3-ruby
|
71
|
+
requirement: &70241510574020 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70241510574020
|
80
|
+
description: write common active_record methods in shorthand
|
81
|
+
email: ascruggs@academicworks.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files:
|
85
|
+
- LICENSE.txt
|
86
|
+
- README.rdoc
|
87
|
+
files:
|
88
|
+
- .document
|
89
|
+
- Gemfile
|
90
|
+
- Gemfile.lock
|
91
|
+
- LICENSE.txt
|
92
|
+
- README.rdoc
|
93
|
+
- Rakefile
|
94
|
+
- VERSION
|
95
|
+
- lazy_model.gemspec
|
96
|
+
- lib/lazy_model.rb
|
97
|
+
- lib/lazy_model/lazy_boolean.rb
|
98
|
+
- lib/lazy_model/lazy_model.rb
|
99
|
+
- lib/lazy_model/lazy_model_support.rb
|
100
|
+
- lib/lazy_model/lazy_string.rb
|
101
|
+
- lib/lazy_model/lazy_text.rb
|
102
|
+
- test/helper.rb
|
103
|
+
- test/lazy_model.sqlite3
|
104
|
+
- test/support/data.rb
|
105
|
+
- test/support/models.rb
|
106
|
+
- test/support/schema.rb
|
107
|
+
- test/test_lazy_model.rb
|
108
|
+
homepage: http://github.com/AcademicWorks/lazy_model
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
hash: -3438707571940229878
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
none: false
|
126
|
+
requirements:
|
127
|
+
- - ! '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 1.8.10
|
133
|
+
signing_key:
|
134
|
+
specification_version: 3
|
135
|
+
summary: write common active_record methods in shorthand
|
136
|
+
test_files: []
|