Class: FloorManager::Floor

Inherits:
Object
  • Object
show all
Defined in:
lib/floor_manager/floor.rb

Overview

A single floor under the supervision of the manager. This is basically a context in which unique/singleton instances and templates can coexist.

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFloor

Returns a new instance of Floor.



35
36
37
# File 'lib/floor_manager/floor.rb', line 35

def initialize
  @employees = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Allows production of new employees by calling their names as methods on the floor.

With a definition of

one :dog do
end

you could call

floor.dog

and get the same as if you had called floor.build :dog



53
54
55
56
57
58
59
60
61
# File 'lib/floor_manager/floor.rb', line 53

def method_missing(sym, *args, &block)
  if args.size <= 1 && employees.has_key?(sym)
    attribute_overrides = {}
    attribute_overrides = args.first unless args.empty?
    employees[sym].build(self, attribute_overrides)
  else
    super
  end
end

Instance Attribute Details

#employeesObject (readonly)

Returns the value of attribute employees.



34
35
36
# File 'lib/floor_manager/floor.rb', line 34

def employees
  @employees
end

Class Method Details

.from_dsl(options, &block) ⇒ Object



30
31
32
# File 'lib/floor_manager/floor.rb', line 30

def self.from_dsl(options, &block)
  DSL.new(options, &block).object
end

Instance Method Details

#attrs(something, overrides = {}) ⇒ Object



69
70
71
# File 'lib/floor_manager/floor.rb', line 69

def attrs(something, overrides={})
  employees[something.to_sym].attrs(self, overrides)
end

#build(something, overrides = {}) ⇒ Object



66
67
68
# File 'lib/floor_manager/floor.rb', line 66

def build(something, overrides={})
  employees[something.to_sym].build(self, overrides)
end

#create(something, overrides = {}) ⇒ Object



63
64
65
# File 'lib/floor_manager/floor.rb', line 63

def create(something, overrides={})
  employees[something.to_sym].create(self, overrides)
end

#resetObject



73
74
75
76
77
# File 'lib/floor_manager/floor.rb', line 73

def reset
  employees.values.each do |employee|
    employee.reset
  end
end