Class: SumSum
- Inherits:
-
Hash
- Object
- Hash
- SumSum
- Defined in:
- lib/sum_sum.rb,
lib/sum_sum/version.rb
Overview
SumSum
SumSum allows you to generate simple reports on the count of values in hashes.
Constant Summary collapse
- VERSION =
"0.0.5"
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#kind_of_children ⇒ Object
readonly
Returns the value of attribute kind_of_children.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
Instance Method Summary collapse
-
#add(hash, increase_count_by = 1) ⇒ SumSum
Add a new hash to analyze.
- #add_from_dump(data, hash = {}, on_level = 0) ⇒ Object
- #bottom? ⇒ Boolean
- #dump ⇒ Object
-
#initialize(*keys, options = {}) ⇒ SumSum
constructor
A new instance of SumSum.
- #inspect ⇒ Object
- #pretty_print(pp) ⇒ Object
- #root ⇒ Object
- #root? ⇒ Boolean
-
#share ⇒ Float
Returns share compared to parent.
- #sort! ⇒ Object
-
#total_share ⇒ Float
Returns share compared to all entries.
Constructor Details
#initialize(*keys, options = {}) ⇒ SumSum
Returns a new instance of SumSum.
11 12 13 14 15 16 |
# File 'lib/sum_sum.rb', line 11 def initialize(*args) = args[-1].is_a?(Hash) ? args.pop : {} @key, @parent, @level = [:key], [:parent], ([:level] || 0) @kind_of_children, @args, @count = args[level], args, 0 super() end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
18 19 20 |
# File 'lib/sum_sum.rb', line 18 def args @args end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
18 19 20 |
# File 'lib/sum_sum.rb', line 18 def count @count end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
18 19 20 |
# File 'lib/sum_sum.rb', line 18 def key @key end |
#kind_of_children ⇒ Object (readonly)
Returns the value of attribute kind_of_children.
18 19 20 |
# File 'lib/sum_sum.rb', line 18 def kind_of_children @kind_of_children end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
18 19 20 |
# File 'lib/sum_sum.rb', line 18 def level @level end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
18 19 20 |
# File 'lib/sum_sum.rb', line 18 def parent @parent end |
Class Method Details
.load(data) ⇒ Object
111 112 113 114 115 |
# File 'lib/sum_sum.rb', line 111 def self.load(data) new(*data[0]).tap do |sum_sum| sum_sum.add_from_dump(data[1]) end end |
Instance Method Details
#add(hash, increase_count_by = 1) ⇒ SumSum
Add a new hash to analyze.
30 31 32 33 34 35 36 37 38 |
# File 'lib/sum_sum.rb', line 30 def add(hash, increase_count_by=1) @count = @count + increase_count_by unless bottom? key = hash[kind_of_children] self[key] ||= SumSum.new(*args, :parent => self, :key => key, :level => level + 1) self[key].add(hash, increase_count_by) end self end |
#add_from_dump(data, hash = {}, on_level = 0) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/sum_sum.rb', line 117 def add_from_dump(data, hash={}, on_level=0) data.each do |k, v| hash[args[on_level]] = k v.is_a?(Hash) ? add_from_dump(v, hash, on_level + 1) : add(hash, v) end end |
#bottom? ⇒ Boolean
87 88 89 |
# File 'lib/sum_sum.rb', line 87 def bottom? !kind_of_children end |
#dump ⇒ Object
104 105 106 107 108 109 |
# File 'lib/sum_sum.rb', line 104 def dump return count if bottom? hash = {} each{ |k, v| hash[k] = v.dump } root? ? [args, hash] : hash end |
#inspect ⇒ Object
95 96 97 |
# File 'lib/sum_sum.rb', line 95 def inspect bottom? ? "#{count}" : "{#{kind_of_children}:#{count} #{super.gsub(/^\{|\}$/, "")}}" end |
#pretty_print(pp) ⇒ Object
99 100 101 102 |
# File 'lib/sum_sum.rb', line 99 def pretty_print(pp) return pp.text(" #{count}") if bottom? super end |
#root ⇒ Object
91 92 93 |
# File 'lib/sum_sum.rb', line 91 def root root? ? self : parent.root end |
#root? ⇒ Boolean
83 84 85 |
# File 'lib/sum_sum.rb', line 83 def root? !parent end |
#share ⇒ Float
Returns share compared to parent
53 54 55 |
# File 'lib/sum_sum.rb', line 53 def share root? ? 1.0 : count/parent.count.to_f end |
#sort! ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/sum_sum.rb', line 74 def sort! return self if bottom? values.each(&:sort!) to_a.sort_by{|it| it[1].count}.reverse.tap do |array| clear && array.each{|k, v| self[k] = v } end self end |
#total_share ⇒ Float
Returns share compared to all entries
70 71 72 |
# File 'lib/sum_sum.rb', line 70 def total_share count/root.count.to_f end |