/
employee.rb
205 lines (180 loc) · 4.88 KB
/
employee.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
require 'active_support'
require 'blankslate'
module FloorManager::Employee
class DSL < BlankSlate
# A proxy that is the receiver of #set and #append in a construct like this:
#
# one :spy do
# relationship.set :gun
# end
#
class AssocProxy < Struct.new(:employee, :field)
def set(*create_args)
employee.add_attribute AttributeAction::AssocSet.new(field, create_args)
end
def append(*create_args)
employee.add_attribute AttributeAction::AssocAppend.new(field, create_args)
end
end
def initialize(employee, &block)
@employee = employee
instance_eval(&block)
end
def method_missing(sym, *args, &block)
if args.size == 1
# Immediate attribute
value = args.first
@employee.add_attribute AttributeAction::Immediate.new(sym, value)
elsif block
# Lazy attribute
@employee.add_attribute AttributeAction::Block.new(sym, block)
elsif args.empty?
# Maybe: #set / #append proxy?
AssocProxy.new(@employee, sym)
else
super
end
end
end
module AttributeAction
# Base class for stored actions.
class Base
def initialize(name)
@name = name
end
def set(obj, value)
if obj.kind_of?(Hash)
obj[@name] = value
else
obj.send("#{@name}=", value)
end
end
def get(obj)
if obj.kind_of?(Hash)
obj[@name]
else
obj.send(@name)
end
end
end
# Stores the action of producing another employee via a collection proxy
# stored in field.
class AssocAppend < Base
def initialize(field, create_args)
super field
@create_args = create_args
end
def apply(obj, floor)
attributes = floor.attrs(*@create_args)
get(obj).build(attributes)
end
end
# Stores the action of producing another employee via the floor and then
# storing that as a value.
class AssocSet < Base
def initialize(field, create_args)
super field
@create_args = create_args
end
def apply(obj, floor)
set(obj, floor.create(*@create_args))
end
end
# Stores the action of setting an attribute to an immediate value.
class Immediate < Base
def initialize(name, value)
super(name)
@value = value
end
def apply(obj, floor)
set(obj, @value)
end
end
# Stores the action of setting an attribute to the result of a block.
class Block < Base
def initialize(name, block)
super(name)
@block = block
end
def apply(obj, floor)
set(obj, @block.call(obj, floor))
end
end
end
# Base class for employees. No instances of this should be created.
class Base
def self.from_dsl(klass_name, &block)
new(klass_name).tap { |emp| DSL.new(emp, &block) }
end
def initialize(klass_name)
@klass_name = klass_name
@attributes = []
end
# Build this employee in memory.
#
def build(floor, overrides)
produce_instance.tap { |i| apply_attributes(i, overrides, floor) }
end
# Create this employee in the database.
#
def create(floor, overrides)
produce_instance.tap { |i|
apply_attributes(i, overrides, floor)
i.save! }
end
# Returns just the attributes that would be used.
#
def attrs(floor, overrides)
{}.tap { |h|
apply_attributes(h, overrides, floor) }
end
# Reset this employee between test runs.
#
def reset
# Empty, but subclasses will override this.
end
def add_attribute action
@attributes << action
end
protected
def produce_instance
@klass_name.to_s.
camelcase.
constantize.
new
end
def apply_attributes(instance, overrides, floor)
@attributes.each do |action|
action.apply(instance, floor)
end
overrides.each do |name, value|
AttributeAction::Immediate.new(name, value).apply(instance, floor)
end
end
end
# A unique employee that will be build/created only once in the given floor.
class Unique < Base
# REFACTOR: Redundancy
def build(floor, overrides)
return @instance if @instance
@instance = produce_instance
apply_attributes(@instance, overrides, floor)
@instance
end
# REFACTOR: Redundancy
def create(floor, overrides)
return @instance if @instance
@instance = produce_instance
apply_attributes(@instance, overrides, floor)
@instance.save!
@instance
end
def reset
@instance = nil
end
end
# A template for employees, you can call build/create many times.
class Template < Base
# Currently empty, see base class
end
end