active_null_object 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: 416047c91750fa6f3bd95e5548117a8c74f5433d
4
+ data.tar.gz: 2c610ebd2dba28cc7528fa2b83f5076967dd8211
5
+ SHA512:
6
+ metadata.gz: 0fa20c14aea6ee14f879ea6dc75cc3b8185425cd8566da060a26eaa7c41a5bbdf394d78e6f2ecf64d1423871d13004ca3de34473898c67bed2f654080c14ca3e
7
+ data.tar.gz: 4e7d7958c23a6842c087f135cfb05b883c9f47643663cbe309277597ece98510e66ace0803c0699ba773d2b1580106b01ef0d933f5fa55681f3b97b405ad4af1
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ Active Null Object
2
+ ==================
3
+
4
+ Instead of returning nil when an association is not found, return a [Null Object](http://martinfowler.com/eaaCatalog/specialCase.html).
5
+
6
+ Associations
7
+ ------------
8
+ ```ruby
9
+ # Automatically searches for the Null Object based on the association name (NullUser)...
10
+ belongs_to :user, null_object: true
11
+
12
+ # ...or pass explicit class name.
13
+ belongs_to :user, null_object: 'NullAdmin'
14
+ ```
15
+
16
+ Finder
17
+ ------
18
+ ```ruby
19
+ # Use a specialized finder if you want a Null Object if the record is not found.
20
+ User.find_or_null_object(1)
21
+ ```
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,31 @@
1
+ module ActiveNullObject
2
+ module Association
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class << self
7
+ alias_method_chain :belongs_to, :null_object
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def belongs_to_with_null_object(name, scope = nil, options = {})
13
+ options = scope if scope.is_a?(Hash)
14
+ null_object_name = options.delete(:null_object)
15
+ belongs_to_without_null_object(name, scope, options)
16
+ define_null_object_getter(name, null_object_name) if null_object_name
17
+ end
18
+
19
+ def define_null_object_getter(name, null_object_name)
20
+ null_object = if null_object_name.is_a?(String)
21
+ null_object_name
22
+ else
23
+ "Null#{name.to_s.classify}"
24
+ end
25
+ define_method(name) do
26
+ super() || null_object.constantize.new
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveNullObject
2
+ module Finder
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def find_or_null_object(id)
7
+ find_by(id: id) || "Null#{name}".constantize.new
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ require 'active_record'
2
+ require 'active_support'
3
+ require 'active_null_object/association'
4
+ require 'active_null_object/finder'
5
+
6
+ module ActiveNullObject
7
+ end
8
+
9
+ ActiveRecord::Base.send(:include, ActiveNullObject::Association)
10
+ ActiveRecord::Base.send(:include, ActiveNullObject::Finder)
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_null_object
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Roex
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0.beta1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0.beta1
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
+ description: ''
70
+ email: info@stefanroex.nl
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - README.md
76
+ - VERSION
77
+ - lib/active_null_object.rb
78
+ - lib/active_null_object/association.rb
79
+ - lib/active_null_object/finder.rb
80
+ homepage: http://stefanroex.nl
81
+ licenses: []
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.2.0
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Simple null objects in Active Record
103
+ test_files: []