Class: PolyBelongsTo::FakedCollection

Inherits:
Object
  • Object
show all
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

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

Parameters:

  • method_name (Symbol)
  • args (Array<Symbol>)

    arguments

Returns:

  • (true, false)


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

#allArray

Returns:

  • (Array)


24
25
26
# File 'lib/poly_belongs_to/faked_collection.rb', line 24

def all
  Array[@instance].compact
end

#ancestorsArray<Object>

Returns:

  • (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

Parameters:

  • args (Array<Symbol>)

    arguments

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

#countInteger

alias for size

Returns:

  • (Integer)

    1 or 0



87
88
89
# File 'lib/poly_belongs_to/faked_collection.rb', line 87

def count
  size
end

#each(*args, &block) ⇒ Enumerator

Parameters:

  • args (Array<Symbol>)

    arguments

Returns:

  • (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?

Returns:

  • (true, false)


30
31
32
# File 'lib/poly_belongs_to/faked_collection.rb', line 30

def empty?
  all.empty?
end

#firstObject?

Returns:

  • (Object, nil)


52
53
54
# File 'lib/poly_belongs_to/faked_collection.rb', line 52

def first
  @instance
end

#idInteger?

Returns:

  • (Integer, nil)


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

Parameters:

  • thing (Object)

    object class

Returns:

  • (true, false)


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

Parameters:

  • thing (Object)

    object class

Returns:

  • (true, false)


74
75
76
# File 'lib/poly_belongs_to/faked_collection.rb', line 74

def kind_of?(thing)
  ancestors.include? thing
end

#klassObject

Returns object class.

Returns:

  • (Object)

    object class



92
93
94
# File 'lib/poly_belongs_to/faked_collection.rb', line 92

def klass
  @instance.class
end

#lastObject?

Returns:

  • (Object, nil)


57
58
59
# File 'lib/poly_belongs_to/faked_collection.rb', line 57

def last
  @instance
end

#presenceself?

Returns:

  • (self, nil)


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?

Returns:

  • (true, false)


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

Parameters:

  • method_name (Symbol)

Returns:

  • (true, false)


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

#sizeInteger

Returns 1 or 0.

Returns:

  • (Integer)

    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

Returns:

  • (true, false)


47
48
49
# File 'lib/poly_belongs_to/faked_collection.rb', line 47

def valid?
  !!@instance.try(&:valid?)
end