Class: FloorManager::Employee::Template
- Inherits:
-
Object
- Object
- FloorManager::Employee::Template
- Defined in:
- lib/floor_manager/employee.rb
Overview
Base class for employees. No instances of this should be created.
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
-
#add_attribute(filter, action) ⇒ Object
Add an attribute to set.
-
#apply_attributes(instance, filter, floor, overrides = {}) ⇒ Object
Modify attribute values in
instance
, setting them to what was specified in the factory for this employee and then overriding them with what was given inoverrides
. -
#attrs(floor, overrides) ⇒ Object
Returns just the attributes that would be used.
-
#build(floor, overrides) ⇒ Object
Build this employee in memory.
- #camelcase(str) ⇒ Object
-
#create(floor, overrides) ⇒ Object
Create this employee in the database.
-
#initialize(klass_name, namespace = nil) ⇒ Template
constructor
A new instance of Template.
- #produce_instance ⇒ Object
-
#reset ⇒ Object
Reset this employee between test runs.
Constructor Details
#initialize(klass_name, namespace = nil) ⇒ Template
Returns a new instance of Template.
10 11 12 13 14 |
# File 'lib/floor_manager/employee.rb', line 10 def initialize(klass_name, namespace=nil) @klass_name = klass_name @namespace = namespace @attributes = Hash.new { |h,k| h[k] = Array.new } end |
Class Method Details
Instance Method Details
#add_attribute(filter, action) ⇒ Object
Add an attribute to set. The action should implement the AttributeAction interface. This method is mainly used by the DSL to store actions to take.
54 55 56 |
# File 'lib/floor_manager/employee.rb', line 54 def add_attribute filter, action @attributes[filter] << action end |
#apply_attributes(instance, filter, floor, overrides = {}) ⇒ Object
Modify attribute values in instance
, setting them to what was specified in the factory for this employee and then overriding them with what was given in overrides
.
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/floor_manager/employee.rb', line 70 def apply_attributes(instance, filter, floor, overrides={}) # First apply all attributes that were given in the factory definition. @attributes[filter]. each do |action| action.apply(instance, floor, self) end # Then override with what the user just gave us. overrides.each do |name, value| AttributeAction::Immediate.new(name, value).apply(instance, floor, self) end end |
#attrs(floor, overrides) ⇒ Object
Returns just the attributes that would be used.
40 41 42 |
# File 'lib/floor_manager/employee.rb', line 40 def attrs(floor, overrides) build(floor, overrides).attributes end |
#build(floor, overrides) ⇒ Object
Build this employee in memory.
18 19 20 21 |
# File 'lib/floor_manager/employee.rb', line 18 def build(floor, overrides) produce_instance.tap { |i| apply_attributes(i, :none, floor, overrides) } end |
#camelcase(str) ⇒ Object
62 63 64 |
# File 'lib/floor_manager/employee.rb', line 62 def camelcase(str) str.gsub(%r((^|_)\w)) { |match| match[-1].upcase } end |
#create(floor, overrides) ⇒ Object
Create this employee in the database.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/floor_manager/employee.rb', line 25 def create(floor, overrides) produce_instance.tap { |i| apply_attributes(i, :none, floor, overrides) i.save or fail "Could not create instance of #{@klass_name.inspect}." unless @attributes[:after_create].empty? apply_attributes(i, :after_create, floor) i.save or fail "Could not save after_create." end } end |
#produce_instance ⇒ Object
58 59 60 61 |
# File 'lib/floor_manager/employee.rb', line 58 def produce_instance name = camelcase(@klass_name.to_s) (@namespace || Object).const_get(name).new end |
#reset ⇒ Object
Reset this employee between test runs.
46 47 48 |
# File 'lib/floor_manager/employee.rb', line 46 def reset # Empty, but subclasses will override this. end |