Module: ActiveNull
- Defined in:
- lib/active_null.rb,
lib/active_null/version.rb,
lib/active_null/null_model_builder.rb
Defined Under Namespace
Classes: NullModelBuilder
Constant Summary
collapse
- VERSION =
'0.0.5'
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.extended(klass) ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/active_null.rb', line 34
def self.extended(klass)
klass.class_eval <<-CODE
after_initialize do
self.class.null_associations
end
CODE
end
|
Instance Method Details
#find_by(*args, &block) ⇒ Object
16
17
18
|
# File 'lib/active_null.rb', line 16
def find_by(*args, &block)
super || null
end
|
#null ⇒ Object
5
6
7
|
# File 'lib/active_null.rb', line 5
def null
@null_class.get
end
|
#null_associations ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/active_null.rb', line 20
def null_associations
self.reflect_on_all_associations.each do |relation|
unless relation.collection?
class_eval <<-CODE
def #{relation.name}(*args)
result = association(:#{relation.name}).reader(*args)
return result if result || !#{relation.klass.name}.respond_to?(:null)
#{relation.klass.name}.null
end
CODE
end
end
end
|
#null_model(&block) ⇒ Object
9
10
11
12
13
14
|
# File 'lib/active_null.rb', line 9
def null_model(&block)
null_model_overrides = if block_given?
Module.new.tap { |m| m.module_eval(&block) }
end
@null_class = NullModelBuilder.new(self, null_model_overrides).build
end
|