Class: SumSum

Inherits:
Hash
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*keys, options = {}) ⇒ SumSum

Returns a new instance of SumSum.

Examples:

Create a SumSum to analyze hashes with attributes :gender, :age and :name

SumSum.new(:gender, :age, :name)

Parameters:

  • *args (Symbol, String)

    the keys to anaylze on hashes

  • options (Hash) (defaults to: {})

    are only used internaly



11
12
13
14
15
16
# File 'lib/sum_sum.rb', line 11

def initialize(*args)
  options = args[-1].is_a?(Hash) ? args.pop : {}
  @key, @parent, @level = options[:key], options[:parent], (options[:level] || 0)
  @kind_of_children, @args, @count = args[level], args, 0
  super()
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



18
19
20
# File 'lib/sum_sum.rb', line 18

def args
  @args
end

#countObject (readonly)

Returns the value of attribute count.



18
19
20
# File 'lib/sum_sum.rb', line 18

def count
  @count
end

#keyObject (readonly)

Returns the value of attribute key.



18
19
20
# File 'lib/sum_sum.rb', line 18

def key
  @key
end

#kind_of_childrenObject (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

#levelObject (readonly)

Returns the value of attribute level.



18
19
20
# File 'lib/sum_sum.rb', line 18

def level
  @level
end

#parentObject (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.

Examples:

Add some data

sum_sum.add(:gender => "W", :age => 23, :name => "Nina")
sum_sum.add(:gender => "M", :age => 77, :name => "Carl")
sum_sum.add(:gender => "W", :age => 33, :name => "Nora")

Parameters:

  • hash (Hash, #[])

    the data to add to the SumSum instance

  • increase_count_by (Integer) (defaults to: 1)

    amount to add to count

Returns:



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

Returns:

  • (Boolean)


87
88
89
# File 'lib/sum_sum.rb', line 87

def bottom?
  !kind_of_children
end

#dumpObject



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

#inspectObject



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

#rootObject



91
92
93
# File 'lib/sum_sum.rb', line 91

def root
  root? ? self : parent.root
end

#root?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/sum_sum.rb', line 83

def root?
  !parent
end

#shareFloat

Returns share compared to parent

Examples:

Get share of all (returns alway 1.0)

sum_sum.share
=> 1.0

Get share of all women compared to all entries (two out of three)

sum_sum["W"].share
=> 0.75

Get share of all women with age 23 compared to all women entries (one out of two)

sum_sum["W"][23].share
=> 0.5

Returns:

  • (Float)

    Returns the share between 0.0 and 1.0



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_shareFloat

Returns share compared to all entries

Examples:

Get share of all (returns alway 1.0)

sum_sum.total_share
=> 1.0

Get share of all women compared to all entries (two out of three)

sum_sum["W"].total_share
=> 0.75

Get share of all women with age 23 compared to all entries (one out of three)

sum_sum["W"][23].total_share
=> 0.3333333

Returns:

  • (Float)

    Returns the share between 0.0 and 1.0



70
71
72
# File 'lib/sum_sum.rb', line 70

def total_share
  count/root.count.to_f
end