active_null 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e79140a41af436782d71c91c0ddf040381e97da4
4
+ data.tar.gz: 07bc97f09a1852ed98dfa86b97a0234bb8352f66
5
+ SHA512:
6
+ metadata.gz: f78e90ca8b8555540005622d099edce35c10a6d0b409daeb7bc0a0b9507ffb62bebca6950ef1ee98ebcfcd781af3d7b8afc7d7c08b043e0ed13f330c602bd035
7
+ data.tar.gz: 063645859d0cb859e2cf0f71bca659c6744d4117311ea38135680e07b999d0b39cd4bf354e1139d55f1e73fa2ce960ce9d120326014fcc098ec11ddb67fc2aa9
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_null.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 alexpeachey
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,94 @@
1
+ # ActiveNull
2
+
3
+ Using Null Objects can make life a lot easier and avoid having excessive `nil?` checks.
4
+ Avdi Grimm made an excellent library called [Naught][1] which makes
5
+ working with and building Null Objects easier. Thank you Avdi for making
6
+ this possible!
7
+
8
+ This library makes ActiveRecord Models aware of Null Objects and act as you
9
+ would hope they would when using null objects in your project.
10
+
11
+ This is specially useful when combined with a decorator library like [Draper][2].
12
+ By knowing you have an object instead of the dreaded `nil` you can just
13
+ decorate it and your view won't know any different.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ gem 'active_null'
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install active_null
28
+
29
+ ## Usage
30
+
31
+ For each model you would like to be null aware, `extend ActiveNull`.
32
+
33
+ ```ruby
34
+ class Post < ActiveRecord::Base
35
+ extend ActiveNull
36
+ has_many :comments
37
+ end
38
+
39
+ class Comment < ActiveRecord::Base
40
+ extend ActiveNull
41
+ belongs_to :post
42
+ end
43
+ ```
44
+
45
+ You can now do things like this:
46
+
47
+ ```ruby
48
+ Post.null # => <null:Post>
49
+ Post.null.comments # => []
50
+ Comment.null # => <null:Comment>
51
+ Comment.null.post # => <null:Post>
52
+ Comment.new.post # => <null:Post>
53
+ Post.find_by(id: 1) # => <null:Post>
54
+ ```
55
+
56
+ ## Additional Features
57
+
58
+ You may find you want your Null Object to respond to certain methods
59
+ in a specific way. These methods might be new or they might be
60
+ overrides of the default Null Object definition.
61
+ You can define this in your model:
62
+
63
+ ```ruby
64
+ class Post < ActiveRecord::Base
65
+ extend ActiveNull
66
+
67
+ null_model do
68
+ def some_method
69
+ # special functionality
70
+ end
71
+ end
72
+ end
73
+ ```
74
+
75
+ If you have `Draper` included in your project, the Null Object versions of
76
+ your model will respond as expected to `decorate`.
77
+
78
+ You can then safely do something like:
79
+
80
+ ```ruby
81
+ @post = Post.find_by(id: 1).decorate
82
+ ```
83
+
84
+ ## Contributing
85
+
86
+ 1. Fork it ( https://github.com/Originate/active_null/fork )
87
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
88
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
89
+ 4. Push to the branch (`git push origin my-new-feature`)
90
+ 5. Create a new Pull Request
91
+
92
+
93
+ [1]: https://github.com/avdi/naught
94
+ [2]: https://github.com/drapergem/draper
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :console do
9
+ require 'irb'
10
+ require 'irb/completion'
11
+ require 'active_null' # You know what to do.
12
+ ARGV.clear
13
+ IRB.start
14
+ end
@@ -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 'active_null/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'active_null'
8
+ spec.version = ActiveNull::VERSION
9
+ spec.authors = ['alexpeachey']
10
+ spec.email = ['alex.peachey@originate.com']
11
+ spec.summary = %q{Make ActiveRecord null model aware.}
12
+ spec.description = %q{Make ActiveRecord null model aware.}
13
+ spec.homepage = 'https://github.com/Originate/active_null'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'naught', '~> 1.0'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.6'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'activerecord', '~> 4.1'
27
+ spec.add_development_dependency 'sqlite3'
28
+ spec.add_development_dependency 'draper', '~> 1.3'
29
+ end
@@ -0,0 +1,68 @@
1
+ require 'naught'
2
+
3
+ module ActiveNull
4
+ class NullModelBuilder
5
+ attr_reader :model, :overrides
6
+
7
+ def initialize(model, overrides)
8
+ @model = model
9
+ @overrides = overrides
10
+ end
11
+
12
+ def build
13
+ model = self.model
14
+ return full_name.constantize if Object.const_defined? full_name
15
+ null = Naught.build do |config|
16
+ config.impersonate model
17
+
18
+ model.reflect_on_all_associations.each do |relation|
19
+ if relation.collection?
20
+ define_method(relation.name) { [] }
21
+ else
22
+ define_method(relation.name) do
23
+ return unless relation.klass.respond_to? :null
24
+ relation.klass.null
25
+ end
26
+ end
27
+ end
28
+
29
+ model.column_defaults.each do |field, default|
30
+ define_method(field.to_sym) { default }
31
+ end
32
+
33
+ def nil?
34
+ true
35
+ end
36
+
37
+ def present?
38
+ false
39
+ end
40
+
41
+ def blank?
42
+ true
43
+ end
44
+
45
+ def to_json
46
+ '{}'
47
+ end
48
+ end
49
+ null.class_eval { include Draper::Decoratable } if Object.const_defined? 'Draper'
50
+ null.include(overrides) if overrides
51
+ set_null_model null
52
+ end
53
+
54
+ def name
55
+ base_name = model.name.split('::').last
56
+ "Null#{base_name}"
57
+ end
58
+
59
+ def full_name
60
+ return name if model.parent == Object
61
+ "#{model.parent.name}::#{name}"
62
+ end
63
+
64
+ def set_null_model(null)
65
+ model.parent.const_set name, null
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveNull
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,39 @@
1
+ require 'active_null/version'
2
+ require 'active_null/null_model_builder'
3
+
4
+ module ActiveNull
5
+ def null
6
+ NullModelBuilder.new(self, @null_model_overrides).build.get
7
+ end
8
+
9
+ def null_model(&block)
10
+ @null_model_overrides = Module.new
11
+ @null_model_overrides.module_eval(&block)
12
+ end
13
+
14
+ def find_by(*args, &block)
15
+ super || null
16
+ end
17
+
18
+ def null_associations
19
+ self.reflect_on_all_associations.each do |relation|
20
+ unless relation.collection?
21
+ class_eval <<-CODE
22
+ def #{relation.name}(*args)
23
+ result = association(:#{relation.name}).reader(*args)
24
+ return result if result || !#{relation.klass.name}.respond_to?(:null)
25
+ #{relation.klass.name}.null
26
+ end
27
+ CODE
28
+ end
29
+ end
30
+ end
31
+
32
+ def self.extended(klass)
33
+ klass.class_eval <<-CODE
34
+ after_initialize do
35
+ self.class.null_associations
36
+ end
37
+ CODE
38
+ end
39
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveNull do
4
+ describe '.null' do
5
+ specify { expect(Post.null).to be_instance_of(NullPost) }
6
+ end
7
+
8
+ describe '.null_model' do
9
+ specify { expect(Post.null.override).to eq 'I am an override.' }
10
+ end
11
+
12
+ describe 'a has many' do
13
+ specify { expect(Post.null.comments).to eq [] }
14
+ end
15
+
16
+ describe 'a belongs to' do
17
+ specify { expect(Comment.null.post).to be_instance_of(NullPost) }
18
+ specify { expect(Comment.new.post).to be_instance_of(NullPost) }
19
+ end
20
+
21
+ it 'supports draper' do
22
+ expect(Post.null.decorate).to be_decorated_with(NullPostDecorator)
23
+ end
24
+ end
data/spec/models.rb ADDED
@@ -0,0 +1,22 @@
1
+ class Post < ActiveRecord::Base
2
+ extend ActiveNull
3
+ has_many :comments
4
+ null_model do
5
+ def override
6
+ 'I am an override.'
7
+ end
8
+ end
9
+ end
10
+
11
+ class Comment < ActiveRecord::Base
12
+ extend ActiveNull
13
+ belongs_to :post
14
+ end
15
+
16
+ class NullPostDecorator < Draper::Decorator
17
+ delegate_all
18
+ end
19
+
20
+ class PostDecorator < Draper::Decorator
21
+ delegate_all
22
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,14 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :posts, force: true do |t|
5
+ t.string :title
6
+ t.timestamps
7
+ end
8
+
9
+ create_table :comments, force: true do |t|
10
+ t.string :content
11
+ t.references :post
12
+ t.timestamps
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'active_null'
3
+ require 'active_record'
4
+ require 'draper'
5
+
6
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
7
+
8
+ load File.dirname(__FILE__) + '/schema.rb'
9
+ require File.dirname(__FILE__) + '/models.rb'
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_null
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - alexpeachey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: naught
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.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: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: draper
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.3'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.3'
111
+ description: Make ActiveRecord null model aware.
112
+ email:
113
+ - alex.peachey@originate.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - active_null.gemspec
126
+ - lib/active_null.rb
127
+ - lib/active_null/null_model_builder.rb
128
+ - lib/active_null/version.rb
129
+ - spec/active_null_spec.rb
130
+ - spec/models.rb
131
+ - spec/schema.rb
132
+ - spec/spec_helper.rb
133
+ homepage: https://github.com/Originate/active_null
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.2.2
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Make ActiveRecord null model aware.
157
+ test_files:
158
+ - spec/active_null_spec.rb
159
+ - spec/models.rb
160
+ - spec/schema.rb
161
+ - spec/spec_helper.rb