Module: LLT::Review::Helpers::Reportable

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/llt/review/helpers/reportable.rb', line 6

def id
  @id
end

#rightObject (readonly)

Returns the value of attribute right.



6
7
8
# File 'lib/llt/review/helpers/reportable.rb', line 6

def right
  @right
end

#totalObject (readonly)

Returns the value of attribute total.



6
7
8
# File 'lib/llt/review/helpers/reportable.rb', line 6

def total
  @total
end

#uniqueObject (readonly)

Returns the value of attribute unique.



6
7
8
# File 'lib/llt/review/helpers/reportable.rb', line 6

def unique
  @unique
end

#wrongObject (readonly)

Returns the value of attribute wrong.



6
7
8
# File 'lib/llt/review/helpers/reportable.rb', line 6

def wrong
  @wrong
end

Instance Method Details

#add(element) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/llt/review/helpers/reportable.rb', line 23

def add(element)
  if el = @container[element.id]
    el.add_total(element)
    element.container.each do |_, nested_el|
      el.add(nested_el)
    end
  else
    @container[element.id] = element
  end
end

#add_total(element) ⇒ Object



34
35
36
# File 'lib/llt/review/helpers/reportable.rb', line 34

def add_total(element)
  @total += element.total
end

#add_wrong(unique = nil) ⇒ Object



38
39
40
41
# File 'lib/llt/review/helpers/reportable.rb', line 38

def add_wrong(unique = nil)
  @wrong += 1
  @unique += 1 if unique
end

#cloneObject

This could be implemented with a block as well (which holds whatever code needs to be performed on the cloned instance, but probably not a good idea as this called very often - make it as lean as possibe.



78
79
80
81
82
# File 'lib/llt/review/helpers/reportable.rb', line 78

def clone
  cloned = super
  cloned.replace_with_clone(:container)
  cloned
end

#count_rightsObject



43
44
45
46
# File 'lib/llt/review/helpers/reportable.rb', line 43

def count_rights
  @right = @total - @wrong
  each_value(&:count_rights)
end

#incrementObject



48
49
50
# File 'lib/llt/review/helpers/reportable.rb', line 48

def increment
  @total += 1
end

#init_diffObject



13
14
15
16
17
# File 'lib/llt/review/helpers/reportable.rb', line 13

def init_diff
  @wrong = 0
  @unique = 0
  each { |_, el| el.init_diff }
end

#initialize(id, total = 1) ⇒ Object



8
9
10
11
# File 'lib/llt/review/helpers/reportable.rb', line 8

def initialize(id, total = 1)
  super(id)
  @total = total
end

#percentage(category = :right) ⇒ Object



19
20
21
# File 'lib/llt/review/helpers/reportable.rb', line 19

def percentage(category = :right)
  ((send(category).to_f / @total) * 100).round(2)
end

#sortObject



60
61
62
63
64
65
66
67
# File 'lib/llt/review/helpers/reportable.rb', line 60

def sort
  Hash[
    @container.sort do |(a_id, a_r), (b_id, b_r)|
      comp = b_r.total <=> a_r.total
      comp.zero? ? a_id <=> b_id : comp
    end
  ]
end

#sort!Object



69
70
71
72
# File 'lib/llt/review/helpers/reportable.rb', line 69

def sort!
  each { |_, el| el.sort! }
  @container = sort
end

#xml_attributesObject



56
57
58
# File 'lib/llt/review/helpers/reportable.rb', line 56

def xml_attributes
  { name: @id, total: @total, right: @right, wrong: @wrong, unique: @unique }
end

#xml_tagObject



52
53
54
# File 'lib/llt/review/helpers/reportable.rb', line 52

def xml_tag
  self.class.name.scan(/::(\w+)$/)[0].first.downcase
end