Module: Plant

Defined in:
lib/setup.rb,
lib/plant.rb,
lib/query.rb,
lib/reflection.rb

Overview

begin

require 'mocha' #must put there even if mocha is not used ... else mocha override these aliasings when it's required in test file.

rescue LoadError end

Defined Under Namespace

Modules: Query, Reflection, Run

Constant Summary collapse

@@plants =
{}
@@pool =
{}
@@map =
{}
@@sequences =
{}
@@id =
0

Class Method Summary collapse

Class Method Details

.add_plant(klass, instance) ⇒ Object



25
26
27
# File 'lib/plant.rb', line 25

def self.add_plant klass, instance
  @@plants[klass] = instance
end

.allObject



33
34
35
# File 'lib/plant.rb', line 33

def self.all
  @@pool
end

.define(symbol, args = {}) {|definition| ... } ⇒ Object

Yields:

  • (definition)


15
16
17
18
19
20
21
22
23
# File 'lib/plant.rb', line 15

def self.define symbol, args={}
  klass = args[:class] || symbol.to_s.camelize.constantize
  definition = klass.new
  yield definition if block_given?
  add_plant(klass, definition)
  add_plant(symbol, definition) if args[:class]
  Plant::Reflection.reflect(klass)
  Plant::Stubber.stubs
end

.destroyObject



50
51
52
53
54
55
56
# File 'lib/plant.rb', line 50

def self.destroy
  @@pool = {}
  @@plants = {}
  @@map = {}
  @@sequences = {}
 @@id = 0
end

.next(symbol) ⇒ Object



62
63
64
# File 'lib/plant.rb', line 62

def self.next symbol
  @@sequences[symbol][:lambda].call(@@sequences[symbol][:index] += 1)
end

.plantsObject



29
30
31
# File 'lib/plant.rb', line 29

def self.plants
  @@plants
end

.pool(symbol) {|object| ... } ⇒ Object

Yields:

  • (object)


41
42
43
44
45
46
47
48
# File 'lib/plant.rb', line 41

def self.pool symbol
  definition = plants[symbol] || plants[symbol.to_s.camelize.constantize]
  object = Plant::Reflection.clone(definition,@@id += 1)
  yield  object if block_given?
  @@pool[definition.class] ||= []
  @@pool[definition.class] << object
  object
end

.reloadObject



37
38
39
# File 'lib/plant.rb', line 37

def self.reload
  load "#{RAILS_ROOT}/test/plants.rb" if Plant.plants.empty?
end

.sequence(symbol, &proc) ⇒ Object



58
59
60
# File 'lib/plant.rb', line 58

def self.sequence symbol, &proc
  @@sequences[symbol] = {:lambda => proc, :index => 0}
end

.set_foreign_keys(object, association, value) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/plant.rb', line 66

def self.set_foreign_keys object, association, value
  fk_setter = lambda {|value| value.send(Plant::Reflection.foreign_key(object, association) + '=', object.id)}
  
  case
  when Plant::Reflection.has_many_association?(object.class, association) then value.each {|v| fk_setter.call(v)}
  when value && Plant::Reflection.has_one_association?(object.class, association) then fk_setter.call(value)
  end
end