Class: PolyBelongsTo::FakedCollection
- Inherits:
-
Object
- Object
- PolyBelongsTo::FakedCollection
- Defined in:
- lib/poly_belongs_to/faked_collection.rb
Overview
PolyBelongsTo::FakedCollection emulates an ActiveRecord::CollectionProxy of exactly one or zero items
Instance Method Summary collapse
- #all ⇒ Array
- #ancestors ⇒ Array<Object>
-
#build(*args) ⇒ self
build preforms calculated build command and returns self.
-
#count ⇒ Integer
alias for size.
- #each(*args, &block) ⇒ Enumerator
-
#empty? ⇒ true, false
Boolean of empty?.
- #first ⇒ Object?
- #id ⇒ Integer?
-
#initialize(obj = nil, child = nil) ⇒ FakedCollection
constructor
Create the Faked Colllection Proxy.
-
#is_a?(thing) ⇒ true, false
Boolean of kind match.
-
#kind_of?(thing) ⇒ true, false
Boolean of kind match.
-
#klass ⇒ Object
Object class.
- #last ⇒ Object?
-
#method_missing(method_name, *args, &block) ⇒ true, false
Hands method and argument on to instance if it responds to method, otherwise calls super.
- #presence ⇒ self?
-
#present? ⇒ true, false
Boolean of not empty?.
-
#respond_to?(method_name) ⇒ true, false
Returns whether this or super responds to method.
-
#size ⇒ Integer
1 or 0.
-
#valid? ⇒ true, false
Boolean of validity.
Constructor Details
#initialize(obj = nil, child = nil) ⇒ FakedCollection
Create the Faked Colllection Proxy
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 6 def initialize(obj = nil, child = nil) @obj = obj @child = child if obj.nil? || child.nil? @instance = nil else raise "Not a has_one rleationship for FakedCollection" unless PolyBelongsTo::Pbt::IsSingular[obj, child] @instance = @obj.send(PolyBelongsTo::Pbt::CollectionProxy[@obj, @child]) end self end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ true, false
Hands method and argument on to instance if it responds to method, otherwise calls super
121 122 123 124 125 126 127 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 121 def method_missing(method_name, *args, &block) if @instance.respond_to?(method_name) @instance.send method_name, *args, &block else super end end |
Instance Method Details
#all ⇒ Array
24 25 26 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 24 def all Array[@instance].compact end |
#ancestors ⇒ Array<Object>
67 68 69 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 67 def ancestors klass.ancestors.unshift(PolyBelongsTo::FakedCollection) end |
#build(*args) ⇒ self
build preforms calculated build command and returns self
99 100 101 102 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 99 def build(*args) @instance = @obj.send(PolyBelongsTo::Pbt::BuildCmd[@obj, @child], *args) self end |
#count ⇒ Integer
alias for size
87 88 89 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 87 def count size end |
#each(*args, &block) ⇒ Enumerator
106 107 108 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 106 def each(*args, &block) all.each(*args, &block) end |
#empty? ⇒ true, false
Boolean of empty?
30 31 32 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 30 def empty? all.empty? end |
#first ⇒ Object?
52 53 54 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 52 def first @instance end |
#id ⇒ Integer?
19 20 21 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 19 def id @instance.try(:id) end |
#is_a?(thing) ⇒ true, false
Boolean of kind match
81 82 83 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 81 def is_a?(thing) kind_of?(thing) end |
#kind_of?(thing) ⇒ true, false
Boolean of kind match
74 75 76 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 74 def kind_of?(thing) ancestors.include? thing end |
#klass ⇒ Object
Returns object class.
92 93 94 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 92 def klass @instance.class end |
#last ⇒ Object?
57 58 59 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 57 def last @instance end |
#presence ⇒ self?
41 42 43 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 41 def presence all.first ? self : nil end |
#present? ⇒ true, false
Boolean of not empty?
36 37 38 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 36 def present? !empty? end |
#respond_to?(method_name) ⇒ true, false
Returns whether this or super responds to method
113 114 115 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 113 def respond_to?(method_name) @instance.respond_to?(method_name) || super end |
#size ⇒ Integer
Returns 1 or 0.
62 63 64 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 62 def size all.size end |
#valid? ⇒ true, false
Boolean of validity
47 48 49 |
# File 'lib/poly_belongs_to/faked_collection.rb', line 47 def valid? !!@instance.try(&:valid?) end |