class PolyBelongsTo::FakedCollection
PolyBelongsTo::FakedCollection
emulates an ActiveRecord::CollectionProxy of exactly one or zero items
Public Class Methods
Create the Faked Colllection Proxy
# 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
Public Instance Methods
@return [Array]
# File lib/poly_belongs_to/faked_collection.rb, line 24 def all Array[@instance].compact end
@return [Array<Object>]
# File lib/poly_belongs_to/faked_collection.rb, line 67 def ancestors klass.ancestors.unshift(PolyBelongsTo::FakedCollection) end
build preforms calculated build command and returns self @param args [Array<Symbol>] arguments @return [self]
# File lib/poly_belongs_to/faked_collection.rb, line 99 def build(*args) @instance = @obj.send(PolyBelongsTo::Pbt::BuildCmd[@obj, @child], *args) self end
alias for size @return [Integer] 1 or 0
# File lib/poly_belongs_to/faked_collection.rb, line 87 def count size end
@param args [Array<Symbol>] arguments @return [Enumerator]
# File lib/poly_belongs_to/faked_collection.rb, line 106 def each(*args, &block) all.each(*args, &block) end
Boolean of empty? @return [true, false]
# File lib/poly_belongs_to/faked_collection.rb, line 30 def empty? all.empty? end
@return [Object, nil]
# File lib/poly_belongs_to/faked_collection.rb, line 52 def first @instance end
@return [Integer, nil]
# File lib/poly_belongs_to/faked_collection.rb, line 19 def id @instance.try(:id) end
Boolean of kind match @param thing [Object] object class @return [true, false]
# File lib/poly_belongs_to/faked_collection.rb, line 81 def is_a?(thing) kind_of?(thing) end
Boolean of kind match @param thing [Object] object class @return [true, false]
# File lib/poly_belongs_to/faked_collection.rb, line 74 def kind_of?(thing) ancestors.include? thing end
@return [Object] object class
# File lib/poly_belongs_to/faked_collection.rb, line 92 def klass @instance.class end
@return [Object, nil]
# File lib/poly_belongs_to/faked_collection.rb, line 57 def last @instance end
Hands method and argument on to instance if it responds to method, otherwise calls super @param method_name [Symbol] @param args [Array<Symbol>] arguments @return [true, false]
# 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
@return [self, nil]
# File lib/poly_belongs_to/faked_collection.rb, line 41 def presence all.first ? self : nil end
Boolean of not empty? @return [true, false]
# File lib/poly_belongs_to/faked_collection.rb, line 36 def present? !empty? end
Returns whether this or super responds to method @param method_name [Symbol] @return [true, false]
# File lib/poly_belongs_to/faked_collection.rb, line 113 def respond_to?(method_name) @instance.respond_to?(method_name) || super end
@return [Integer] 1 or 0
# File lib/poly_belongs_to/faked_collection.rb, line 62 def size all.size end
Boolean of validity @return [true, false]
# File lib/poly_belongs_to/faked_collection.rb, line 47 def valid? !!@instance.try(&:valid?) end