paranoid_dummy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/paranoid_dummy/dummy.rb +34 -0
- data/lib/paranoid_dummy/paranoid_dummy.rb +63 -0
- data/lib/paranoid_dummy/version.rb +3 -0
- data/lib/paranoid_dummy.rb +7 -0
- data/paranoid_dummy.gemspec +26 -0
- data/spec/lib/paranoid_dummy_spec.rb +47 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/models.rb +12 -0
- data/spec/support/schema.rb +15 -0
- metadata +112 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ParanoidDummy
|
2
|
+
|
3
|
+
class Dummy
|
4
|
+
attr_reader :default
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@default = ''
|
8
|
+
end
|
9
|
+
|
10
|
+
def responds_to method, options = {}
|
11
|
+
if options[:with].present?
|
12
|
+
instance_variable_set "@#{method}".to_sym, options[:with]
|
13
|
+
|
14
|
+
metaclass.send :redefine_method, method.to_sym do |*args|
|
15
|
+
instance_variable_get "@#{method}".to_sym
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def defaults_to value
|
21
|
+
@default = value
|
22
|
+
end
|
23
|
+
|
24
|
+
def method_missing method, *args, &block
|
25
|
+
return default unless self.respond_to? method
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
29
|
+
def metaclass
|
30
|
+
class << self; self; end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module ParanoidDummy
|
2
|
+
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def dummy; @dummy ||= Dummy.new; end
|
7
|
+
|
8
|
+
def define_paranoid_dummy options = {}, &block
|
9
|
+
if not options.is_a?(Hash) and not options.empty?
|
10
|
+
raise ArgumentError, "Hash expected, got #{options.class.name}"
|
11
|
+
end
|
12
|
+
|
13
|
+
if block_given?
|
14
|
+
dummy.instance_eval &block
|
15
|
+
|
16
|
+
if options[:for].present? and options[:for].is_a?(Array)
|
17
|
+
create_paranoid_methods_from_options options[:for]
|
18
|
+
else
|
19
|
+
create_paranoid_methods_from_associations
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def create_paranoid_methods_from_associations
|
27
|
+
paranoid_associations.each do |association|
|
28
|
+
create_paranoid_method_for association.class_name
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_paranoid_methods_from_options associations
|
33
|
+
associations.each do |association|
|
34
|
+
create_paranoid_method_for association
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def create_paranoid_method_for association
|
39
|
+
klass = self.name
|
40
|
+
method = klass.downcase.to_sym
|
41
|
+
method_with_nil = "#{method}_with_nil".to_sym
|
42
|
+
|
43
|
+
Kernel.const_get(association).class_eval do
|
44
|
+
alias_method method_with_nil, method
|
45
|
+
alias_method "origin_#{method}=".to_sym, "#{method}=".to_sym
|
46
|
+
|
47
|
+
redefine_method method do
|
48
|
+
eval "#{method_with_nil}.nil? ? #{klass}.dummy : #{method_with_nil}"
|
49
|
+
end
|
50
|
+
|
51
|
+
redefine_method "#{method}=".to_sym do |record|
|
52
|
+
send("origin_#{method}=".to_sym, record) unless record.is_a?(ParanoidDummy::Dummy)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def paranoid_associations
|
58
|
+
reflect_on_all_associations(:has_many) +
|
59
|
+
reflect_on_all_associations(:has_one)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "paranoid_dummy/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "paranoid_dummy"
|
7
|
+
s.version = ParanoidDummy::VERSION
|
8
|
+
s.authors = ["Matthias Stiller"]
|
9
|
+
s.email = ["matthias.stiller@metascape.de"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Replace nil objects with predefined dummy objects.}
|
12
|
+
s.description = %q{paranoid_dummy replaces belongs_to associations that return nil objects with predefined dummy objects.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "paranoid_dummy"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "rails", "~> 3.1.0"
|
22
|
+
|
23
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
24
|
+
s.add_development_dependency "rspec"
|
25
|
+
s.add_development_dependency "sqlite3-ruby"
|
26
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ParanoidDummy do
|
4
|
+
|
5
|
+
context '.define_paranoid_dummy' do
|
6
|
+
it 'extends class with class method dummy' do
|
7
|
+
User.should respond_to(:dummy)
|
8
|
+
User.dummy.should be_a_kind_of(ParanoidDummy::Dummy)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'associated object is nil' do
|
13
|
+
before(:each) do
|
14
|
+
@post_without_user = Post.create(subject: 'subject', body: 'body')
|
15
|
+
@dummy = @post_without_user.user
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'returns object of class ParanoidDummy::Dummy' do
|
19
|
+
@dummy.should be_a_kind_of(ParanoidDummy::Dummy)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'returns dummy object with predefined methods' do
|
23
|
+
@dummy.should respond_to(:name)
|
24
|
+
@dummy.name.should eql('deleted user')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'returns dummy object with predefined default return values' do
|
28
|
+
@dummy.foobar.should eql('foo')
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'returns dummy object that allows arbitrary method calls' do
|
32
|
+
@dummy.amazing_method('foo', :bar, [], {}).should eql('foo')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'associated object is not nil' do
|
37
|
+
before(:each) do
|
38
|
+
@user = User.create(name: 'foobar')
|
39
|
+
@post_with_user = Post.create(subject: 'subject', body: 'body', user: @user)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'returns associated object' do
|
43
|
+
@post_with_user.user.should be(@user)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
self.verbose = false
|
3
|
+
|
4
|
+
create_table :users, force: true do |t|
|
5
|
+
t.string :name
|
6
|
+
t.timestamps
|
7
|
+
end
|
8
|
+
|
9
|
+
create_table :posts, force: true do |t|
|
10
|
+
t.string :subject
|
11
|
+
t.string :body
|
12
|
+
t.integer :user_id
|
13
|
+
t.timestamps
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paranoid_dummy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthias Stiller
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-09-14 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 3.1.0
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: bundler
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.0.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: sqlite3-ruby
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id004
|
59
|
+
description: paranoid_dummy replaces belongs_to associations that return nil objects with predefined dummy objects.
|
60
|
+
email:
|
61
|
+
- matthias.stiller@metascape.de
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
extra_rdoc_files: []
|
67
|
+
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- Rakefile
|
72
|
+
- lib/paranoid_dummy.rb
|
73
|
+
- lib/paranoid_dummy/dummy.rb
|
74
|
+
- lib/paranoid_dummy/paranoid_dummy.rb
|
75
|
+
- lib/paranoid_dummy/version.rb
|
76
|
+
- paranoid_dummy.gemspec
|
77
|
+
- spec/lib/paranoid_dummy_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
- spec/support/models.rb
|
80
|
+
- spec/support/schema.rb
|
81
|
+
homepage: ""
|
82
|
+
licenses: []
|
83
|
+
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: "0"
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: "0"
|
101
|
+
requirements: []
|
102
|
+
|
103
|
+
rubyforge_project: paranoid_dummy
|
104
|
+
rubygems_version: 1.8.10
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Replace nil objects with predefined dummy objects.
|
108
|
+
test_files:
|
109
|
+
- spec/lib/paranoid_dummy_spec.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
- spec/support/models.rb
|
112
|
+
- spec/support/schema.rb
|