Class: Squeeze
- Inherits:
-
Object
- Object
- Squeeze
- Defined in:
- lib/squeeze/hash_tree.rb,
lib/squeeze.rb,
lib/squeeze/matcher.rb,
lib/squeeze/traversable.rb
Overview
Defined Under Namespace
Modules: Traversable Classes: HashTree, Matcher
Instance Attribute Summary collapse
-
#derived ⇒ Object
Returns the value of attribute derived.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#reports ⇒ Object
Returns the value of attribute reports.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Squeeze
constructor
A new instance of Squeeze.
- #process(record, fields, sig = []) ⇒ Object
- #process_field(record, field, subfields, sig) ⇒ Object
-
#reduce(records) ⇒ Object
Takes an array of hashes, keyed with Symbols.
- #resolve(name, record) ⇒ Object
Constructor Details
Instance Attribute Details
#derived ⇒ Object
Returns the value of attribute derived.
54 55 56 |
# File 'lib/squeeze.rb', line 54 def derived @derived end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
53 54 55 |
# File 'lib/squeeze.rb', line 53 def output @output end |
#reports ⇒ Object
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 |