Class: Aladtec::RangeDenormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/aladtec/range_denormalizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ RangeDenormalizer

Returns a new instance of RangeDenormalizer.


5
6
7
8
# File 'lib/aladtec/range_denormalizer.rb', line 5

def initialize(attrs = {})
  @members = attrs.fetch(:members) { raise "You must provide the members" }
  @schedules = attrs.fetch(:schedules) { raise "You must provide the schedules" }
end

Instance Attribute Details

#membersObject (readonly)

Returns the value of attribute members


3
4
5
# File 'lib/aladtec/range_denormalizer.rb', line 3

def members
  @members
end

#schedulesObject (readonly)

Returns the value of attribute schedules


3
4
5
# File 'lib/aladtec/range_denormalizer.rb', line 3

def schedules
  @schedules
end

Instance Method Details

#denormalize(ranges) ⇒ Object


10
11
12
13
14
15
16
17
# File 'lib/aladtec/range_denormalizer.rb', line 10

def denormalize(ranges)
  ranges.each do |range|
    find_member(range)
    find_schedule(range)
    find_position(range)
    range.position = positions_by_id[range.position.id] || range.position
  end
end

#find_member(range) ⇒ Object


19
20
21
22
# File 'lib/aladtec/range_denormalizer.rb', line 19

def find_member(range)
  member = members_by_id[range.member.id]
  range.member = member if member
end

#find_position(range) ⇒ Object


29
30
31
32
# File 'lib/aladtec/range_denormalizer.rb', line 29

def find_position(range)
  position = positions_by_id[range.position.id]
  range.position = position if position
end

#find_schedule(range) ⇒ Object


24
25
26
27
# File 'lib/aladtec/range_denormalizer.rb', line 24

def find_schedule(range)
  schedule = schedules_by_id[range.schedule.id]
  range.schedule = schedule if schedule
end

#members_by_idObject


34
35
36
# File 'lib/aladtec/range_denormalizer.rb', line 34

def members_by_id
  @members_by_id ||= members.reduce({}) {|h, member| h[member.id] = member; h}
end

#positions_by_idObject


46
47
48
# File 'lib/aladtec/range_denormalizer.rb', line 46

def positions_by_id
  @positions_by_id ||= {}
end

#schedules_by_idObject


38
39
40
41
42
43
44
# File 'lib/aladtec/range_denormalizer.rb', line 38

def schedules_by_id
  @schedules_by_id ||= schedules.reduce({}) do |h, schedule|
    h[schedule.id] = schedule
    schedule.positions.each {|position| positions_by_id[position.id] ||= position}
    h
  end
end