class PolyBelongsTo::FakedCollection

PolyBelongsTo::FakedCollection emulates an ActiveRecord::CollectionProxy of exactly one or zero items

Public Class Methods

new(obj = nil, child = nil) click to toggle source

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

all() click to toggle source

@return [Array]

# File lib/poly_belongs_to/faked_collection.rb, line 24
def all
  Array[@instance].compact
end
ancestors() click to toggle source

@return [Array<Object>]

# File lib/poly_belongs_to/faked_collection.rb, line 67
def ancestors
  klass.ancestors.unshift(PolyBelongsTo::FakedCollection)
end
build(*args) click to toggle source

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
count() click to toggle source

alias for size @return [Integer] 1 or 0

# File lib/poly_belongs_to/faked_collection.rb, line 87
def count
  size
end
each(*args, &block) click to toggle source

@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
empty?() click to toggle source

Boolean of empty? @return [true, false]

# File lib/poly_belongs_to/faked_collection.rb, line 30
def empty?
  all.empty?
end
first() click to toggle source

@return [Object, nil]

# File lib/poly_belongs_to/faked_collection.rb, line 52
def first
  @instance
end
id() click to toggle source

@return [Integer, nil]

# File lib/poly_belongs_to/faked_collection.rb, line 19
def id
  @instance.try(:id)
end
is_a?(thing) click to toggle source

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
kind_of?(thing) click to toggle source

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
klass() click to toggle source

@return [Object] object class

# File lib/poly_belongs_to/faked_collection.rb, line 92
def klass
  @instance.class
end
last() click to toggle source

@return [Object, nil]

# File lib/poly_belongs_to/faked_collection.rb, line 57
def last
  @instance
end
method_missing(method_name, *args, &block) click to toggle source

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]

Calls superclass method
# 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
presence() click to toggle source

@return [self, nil]

# File lib/poly_belongs_to/faked_collection.rb, line 41
def presence
  all.first ? self : nil
end
present?() click to toggle source

Boolean of not empty? @return [true, false]

# File lib/poly_belongs_to/faked_collection.rb, line 36
def present?
  !empty?
end
respond_to?(method_name) click to toggle source

Returns whether this or super responds to method @param method_name [Symbol] @return [true, false]

Calls superclass method
# File lib/poly_belongs_to/faked_collection.rb, line 113
def respond_to?(method_name)
  @instance.respond_to?(method_name) || super
end
size() click to toggle source

@return [Integer] 1 or 0

# File lib/poly_belongs_to/faked_collection.rb, line 62
def size
  all.size
end
valid?() click to toggle source

Boolean of validity @return [true, false]

# File lib/poly_belongs_to/faked_collection.rb, line 47
def valid?
  !!@instance.try(&:valid?)
end