null_object_associations 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: 1c78d4d0a41767de2cca8994b72e7274ab6c53b3
4
+ data.tar.gz: a6975eb9053ca44f342d37be4535c62f64662f10
5
+ SHA512:
6
+ metadata.gz: 694cd6dcc49abbc03d374f5dfdd3ae7ff308a3c18a0565bee93cd02a274c2bdb154493fbc888c1fc2bb61da980e4f45e13af9f56900e1b1a94ee92975b5e20a5
7
+ data.tar.gz: 39234897eb8d19696384a1cc1c83b19aef1e14281a599de53cf88157675a8dbac6e6d9d6cd11222d8df6badcc158bd850d293f8a6dfb2f9fb2e2608ac7f5ea57
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Karmen Blake
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,31 @@
1
+ # NullObjectAssociations
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'null_object_associations'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install null_object_associations
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/null_object_associations/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "spec/*_spec.rb"
6
+ end
7
+
8
+ desc "Run specs"
9
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ module NullObjectAssociations
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,61 @@
1
+ require "null_object_associations/version"
2
+
3
+ module NullObjectAssociations
4
+ def self.included base
5
+ base.send :extend, SingletonMethods
6
+ end
7
+
8
+ module SingletonMethods
9
+ def has_many(name, actions = {})
10
+ if actions[:respond_to] == :any
11
+ associations = []
12
+ associations.extend ArrayMethodMissing
13
+ else
14
+ actions = coerce_actions(actions)
15
+ associations = build_associations(actions)
16
+ end
17
+
18
+
19
+ define_method(name) do
20
+ associations
21
+ end
22
+ end
23
+ alias_method :has_and_belongs_to_many, :has_many
24
+
25
+ def has_one(name)
26
+ define_method(name) { nil }
27
+ end
28
+ alias_method :belongs_to, :has_one
29
+
30
+ private
31
+
32
+ def coerce_actions(actions)
33
+ case actions[:respond_to]
34
+ when Array, nil
35
+ actions = Array(actions[:respond_to])
36
+ else
37
+ actions = *actions[:respond_to]
38
+ end
39
+
40
+ actions
41
+ end
42
+
43
+ def build_associations(actions)
44
+ empty_array = []
45
+
46
+ empty_array.tap do |a|
47
+ actions.each do |action|
48
+ empty_array.define_singleton_method(action) do
49
+ []
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ module ArrayMethodMissing
56
+ def method_missing(*args, &block)
57
+ []
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'null_object_associations/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "null_object_associations"
8
+ spec.version = NullObjectAssociations::VERSION
9
+ spec.authors = ["Karmen Blake"]
10
+ spec.email = ["dudeblake@gmail.com"]
11
+ spec.summary = %q{Handle associations in your null objects.}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
@@ -0,0 +1,81 @@
1
+ require_relative 'spec_helper'
2
+ require 'null_object_associations'
3
+
4
+ describe NullObjectAssociations do
5
+ class MyNullObject
6
+ include NullObjectAssociations
7
+
8
+ has_many :foos
9
+ has_many :bars, respond_to: [:pluck, :completed]
10
+ has_many :blahs, respond_to: :any
11
+ has_and_belongs_to_many :zibs, respond_to: [:pluck, :completed]
12
+ has_one :baz
13
+ belongs_to :wut
14
+ end
15
+
16
+ describe MyNullObject do
17
+ let(:null_object) { MyNullObject.new }
18
+
19
+ describe "has_many" do
20
+ it "#foos" do
21
+ null_object.foos.must_be_empty
22
+ end
23
+
24
+ describe "#bars" do
25
+ it "[]" do
26
+ null_object.bars.must_be_empty
27
+ end
28
+
29
+ it "#pluck" do
30
+ null_object.bars.pluck.must_be_empty
31
+ end
32
+
33
+ it "#completed" do
34
+ null_object.bars.completed.must_be_empty
35
+ end
36
+ end
37
+
38
+ describe "#blahs" do
39
+ it "[]" do
40
+ null_object.blahs.must_be_empty
41
+ end
42
+
43
+ describe "respond to anything" do
44
+ ['any', 'thing', 'will', 'work'].each do |option|
45
+ it "##{option}" do
46
+ null_object.blahs.send(option).must_be_empty
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ describe "has_and_belongs_to_many" do
54
+ describe "#zibs" do
55
+ it "[]" do
56
+ null_object.zibs.must_be_empty
57
+ end
58
+
59
+ it "#pluck" do
60
+ null_object.zibs.pluck.must_be_empty
61
+ end
62
+
63
+ it "#completed" do
64
+ null_object.zibs.completed.must_be_empty
65
+ end
66
+ end
67
+ end
68
+
69
+ describe "has_one" do
70
+ it "nil for baz" do
71
+ null_object.baz.must_be_nil
72
+ end
73
+ end
74
+
75
+ describe "belongs_to" do
76
+ it "nil for wut" do
77
+ null_object.wut.must_be_nil
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,2 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: null_object_associations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Karmen Blake
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-15 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - dudeblake@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/null_object_associations.rb
54
+ - lib/null_object_associations/version.rb
55
+ - null_object_associations.gemspec
56
+ - spec/null_object_associations_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Handle associations in your null objects.
82
+ test_files:
83
+ - spec/null_object_associations_spec.rb
84
+ - spec/spec_helper.rb