forked from sky-uk/ruby-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-build-and-parse.rb
58 lines (45 loc) · 991 Bytes
/
1-build-and-parse.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
class Company
class Department
attr_reader :name,
:employees
def initialize(name, &block)
@name = name
@employees = []
instance_eval(&block)
end
def employee(&block)
@employees << Employee.new(&block)
end
end
class Employee
def initialize(&block)
instance_eval(&block)
end
%w( first_name last_name role ).each do |method|
define_method method do |*args|
attribute = "@#{method}"
if args.empty?
self.instance_variable_get(attribute)
else
self.instance_variable_set(attribute, args.first)
end
end
end
end
attr_reader :departments
def initialize(&block)
@departments = []
instance_eval(&block)
end
def department(name, &block)
departments << Department.new(name, &block)
end
end
def company(&block)
if block_given?
@company = Company.new(&block)
else
@company
end
end
require_relative 'run'