null_object_associations 0.0.3 → 0.0.4
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 +4 -4
- data/README.md +1 -1
- data/lib/null_object_associations.rb +19 -3
- data/lib/null_object_associations/version.rb +1 -1
- data/spec/null_object_associations_spec.rb +11 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01d7b9bc03a72a2fe922cf57a4bc370d7be35dd4
|
4
|
+
data.tar.gz: 17fd46f548f0d69fce0572164cf993a256d49ac8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a20431a49ab7438d5fcf5a9d68b3e3f5cf4bd41f463e4c46f6d2315f47331aac9e7d15491ad06d97012542d7490e2da1ea7aea873b14502579845128360c1cf1
|
7
|
+
data.tar.gz: faa8b0a8b99945f7be763e25c0cef81672e3cad73f08c2edc141047f1911d17f8c7bfafd29a63c2c89e2427232172edd01e8cbed01344f85ae3be9bf56459ecf
|
data/README.md
CHANGED
@@ -25,7 +25,7 @@ class NullUser
|
|
25
25
|
include NullObjectAssociations
|
26
26
|
|
27
27
|
has_many :friends
|
28
|
-
has_many :cousins, respond_to: [:pluck, :completed]
|
28
|
+
has_many :cousins, respond_to: [:pluck, :limit, :order, :completed]
|
29
29
|
has_many :cars, respond_to: :any
|
30
30
|
has_and_belongs_to_many :accounts, respond_to: [:pluck, :completed]
|
31
31
|
has_one :house
|
@@ -1,11 +1,21 @@
|
|
1
|
-
require "null_object_associations/version"
|
2
|
-
|
3
1
|
module NullObjectAssociations
|
4
2
|
def self.included base
|
5
3
|
base.send :extend, SingletonMethods
|
6
4
|
end
|
7
5
|
|
8
6
|
module SingletonMethods
|
7
|
+
Object.const_set("NullObjectForArgument", Class.new do
|
8
|
+
def method_missing(to_be_called, *args)
|
9
|
+
if args.empty?
|
10
|
+
[]
|
11
|
+
else
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def empty?; true; end
|
17
|
+
end)
|
18
|
+
|
9
19
|
def has_many(name, actions = {})
|
10
20
|
if actions[:respond_to] == :any
|
11
21
|
associations = []
|
@@ -42,10 +52,16 @@ module NullObjectAssociations
|
|
42
52
|
|
43
53
|
def build_associations(actions)
|
44
54
|
empty_array = []
|
55
|
+
noa = NullObjectForArgument.new
|
56
|
+
methods_with_arguments = [:pluck, :limit, :order]
|
45
57
|
|
46
58
|
empty_array.tap do |empty_arr|
|
47
59
|
actions.each do |action|
|
48
|
-
empty_arr.define_singleton_method(action) do
|
60
|
+
empty_arr.define_singleton_method(action) do |*args|
|
61
|
+
if methods_with_arguments.include?(action) && !args.empty?
|
62
|
+
return noa
|
63
|
+
end
|
64
|
+
|
49
65
|
[]
|
50
66
|
end
|
51
67
|
end
|
@@ -6,7 +6,7 @@ describe NullObjectAssociations do
|
|
6
6
|
include NullObjectAssociations
|
7
7
|
|
8
8
|
has_many :foos
|
9
|
-
has_many :bars, respond_to: [:pluck, :completed]
|
9
|
+
has_many :bars, respond_to: [:pluck, :limit, :order, :completed]
|
10
10
|
has_many :blahs, respond_to: :any
|
11
11
|
has_and_belongs_to_many :zibs, respond_to: [:pluck, :completed]
|
12
12
|
has_one :baz
|
@@ -26,8 +26,16 @@ describe NullObjectAssociations do
|
|
26
26
|
null_object.bars.must_be_empty
|
27
27
|
end
|
28
28
|
|
29
|
-
it "#pluck" do
|
30
|
-
null_object.bars.pluck.must_be_empty
|
29
|
+
it "#pluck with argument" do
|
30
|
+
null_object.bars.pluck(:plan).must_be_empty
|
31
|
+
end
|
32
|
+
|
33
|
+
it "#limit with argument" do
|
34
|
+
null_object.bars.limit(10).must_be_empty
|
35
|
+
end
|
36
|
+
|
37
|
+
it "#order with argument" do
|
38
|
+
null_object.bars.order('desc').must_be_empty
|
31
39
|
end
|
32
40
|
|
33
41
|
it "#completed" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: null_object_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Karmen Blake
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|