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 =
'1.1.0'
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.extended(klass) ⇒ Object
61
62
63
64
65
66
67
|
# File 'lib/active_null.rb', line 61
def self.extended(klass)
klass.class_eval " after_initialize do\n self.class.null_associations\n end\n CODE\nend\n"
|
Instance Method Details
#find_by(*args, &block) ⇒ Object
29
30
31
|
# File 'lib/active_null.rb', line 29
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/active_null.rb', line 33
def null_associations
self.reflect_on_all_associations.each do |relation|
unless relation.collection?
klass = begin
if relation.options[:polymorphic]
polymorphic_null_defaults[relation.name]
elsif relation.klass
relation.klass.name
end
rescue
nil
end
next unless klass
class_eval " def \#{relation.name}(*args)\n result = association(:\#{relation.name}).reader(*args)\n return result if result || !\#{klass}.respond_to?(:null)\n \#{klass}.null\n end\n CODE\n end\n end\nend\n"
|
#null_class ⇒ Object
57
58
59
|
# File 'lib/active_null.rb', line 57
def null_class
@null_class ||= NullModelBuilder.new(self, @null_model_overrides).build
end
|
#null_defaults_for_polymorphic(mappings) ⇒ Object
9
10
11
|
# File 'lib/active_null.rb', line 9
def null_defaults_for_polymorphic mappings
@polymorphic_null_defaults = mappings
end
|
#null_model(method_name = nil, &block) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/active_null.rb', line 17
def null_model(method_name=nil, &block)
@null_model_overrides = if block_given?
Module.new.tap { |m| m.module_eval(&block) }
end
if method_name
singleton_class.class_eval do
define_method(method_name) { null }
end
end
end
|
#polymorphic_null_defaults ⇒ Object
13
14
15
|
# File 'lib/active_null.rb', line 13
def polymorphic_null_defaults
@polymorphic_null_defaults || {}
end
|