Class: Squeeze

Inherits:
Object
  • Object
show all
Defined in:
lib/squeeze/hash_tree.rb,
lib/squeeze.rb,
lib/squeeze/matcher.rb,
lib/squeeze/traversable.rb

Overview

A Hash specially equipped with a default_proc that wishes into being new HashTrees when you try to get or set a non-existent key.

ht = HashTree.new
ht[:i][:am][:a][:roving] = :gambler

Defined Under Namespace

Modules: Traversable Classes: HashTree, Matcher

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Squeeze

Returns a new instance of Squeeze.



61
62
63
64
65
66
# File 'lib/squeeze.rb', line 61

def initialize(options={})
  f = options[:fields] || {}
  @fields = self.class.fields.merge(f)
  @derived = options[:derived] || {}
  @output = HashTree.new
end

Instance Attribute Details

#derivedObject

Returns the value of attribute derived.



54
55
56
# File 'lib/squeeze.rb', line 54

def derived
  @derived
end

#outputObject (readonly)

Returns the value of attribute output.



53
54
55
# File 'lib/squeeze.rb', line 53

def output
  @output
end

#reportsObject

Returns the value of attribute reports.



54
55
56
# File 'lib/squeeze.rb', line 54

def reports
  @reports
end

Class Method Details

.fields(spec = nil) ⇒ Object



56
57
58
59
# File 'lib/squeeze.rb', line 56

def self.fields(spec=nil)
  @fields ||= {}
  spec ? @fields = spec : @fields
end

Instance Method Details

#process(record, fields, sig = []) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/squeeze.rb', line 76

def process(record, fields, sig=[])
  case fields
  when Hash
    fields.each do |field, subfields|
      process_field(record, field, subfields, sig)
    end
  when Array
    fields.each do |field|
      process_field(record, field, true, sig)
    end
  end
end

#process_field(record, field, subfields, sig) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/squeeze.rb', line 89

def process_field(record, field, subfields, sig)
  return unless key = resolve(field, record)
  case subfields
  when true
    output.increment(sig + [field, key])
    output.increment(sig + [field, :_count])
  when Symbol
    output.increment(sig + [field, key, :_count])
    value = resolve(subfields, record)
    output.increment(sig + [field, key, subfields, value])
  when Array
    subfields.each do |subfield|
      process(record, {field => subfield}, sig )
    end
  when Hash
    output.increment(sig + [field, key, :_count])
    process(record, subfields, sig + [field, key])
  end
end

#reduce(records) ⇒ Object

Takes an array of hashes, keyed with Symbols



69
70
71
72
73
74
# File 'lib/squeeze.rb', line 69

def reduce(records)
  records.each do |record|
    process(record, @fields)
  end
  @output
end

#resolve(name, record) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/squeeze.rb', line 109

def resolve(name, record)
  result = if v = record[name]
    v
  elsif p = @derived[name]
    p.call(record)
  elsif record.respond_to?(name)
    record.send(name)
  else
    :_unknown
  end
end