Class: Sawmill::EntryProcessor::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sawmill/entry_processor.rb

Overview

A base class for entry processors.

Entry processors need not necessarily subclass this class, but should at least duck-type the methods.

If a class subclasses this class, and lives in the EntryProcessor namespace, then it will automatically be available in the build interface. See EntryProcessor#build.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_dsl_method(name_) ⇒ Object

Add a method to the processor building DSL. You may call this method in the DSL to create an instance of this entry processor. You must pass a method name that begins with a lower-case letter or underscore.

Processors that subclass Sawmill::EntryProcessor::Base and live in the Sawmill::EntryProcessor namespace will have their class name automatically added to the DSL. This method is primarily for other processors that do not live in that module namespace.

See Sawmill::EntryProcessor#build for more information.

Raises Sawmill::Errors::DSLMethodError if the given name is already taken.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/sawmill/entry_processor.rb', line 160

def self.add_dsl_method(name_)
  klass_ = self
  if name_.to_s !~ /^[a-z_]/
    raise ::ArgumentError, "Method name must begin with a lower-case letter or underscore"
  end
  if Builder.method_defined?(name_)
    raise Errors::DSLMethodError, "Method #{name_} already defined"
  end
  Builder.class_eval do
    define_method(name_) do |*args_|
      klass_.new(*args_)
    end
  end
end

.inherited(subclass_) ⇒ Object

:nodoc:



133
134
135
136
137
138
139
140
141
142
# File 'lib/sawmill/entry_processor.rb', line 133

def self.inherited(subclass_)  # :nodoc:
  if subclass_.name =~ /^Sawmill::EntryProcessor::([^:]+)$/
    name_ = $1
    Builder.class_eval do
      define_method(name_) do |*args_|
        subclass_.new(*args_)
      end
    end
  end
end

Instance Method Details

#attribute(entry_) ⇒ Object

Receive and process a Sawmill::Entry::Attribute.



94
95
96
# File 'lib/sawmill/entry_processor.rb', line 94

def attribute(entry_)
  true
end

#begin_record(entry_) ⇒ Object

Receive and process a Sawmill::Entry::BeginRecord.



73
74
75
# File 'lib/sawmill/entry_processor.rb', line 73

def begin_record(entry_)
  true
end

#end_record(entry_) ⇒ Object

Receive and process a Sawmill::Entry::EndRecord.



80
81
82
# File 'lib/sawmill/entry_processor.rb', line 80

def end_record(entry_)
  true
end

#finishObject

Close down the processor, perform any finishing tasks, and return any final calculated value.

After this is called, the processor should ignore any further entries.

The return value can be used to communicate a final computed value, analysis report, or other data back to the caller. It may also be nil, signalling no finish value.

Note that some processors function to multiplex other processors. In such a case, their finish value needs to be an aggregate of the values returned by their descendants. To handle these cases, we define a protocol for finish values. A finish value may be nil, an Array, or another kind of object. Nil means “no value” and thus can be ignored by a processor that aggregates other values. An Array indicates an aggregation; if finish returns an array, it is always an aggregation of actual values. Any other kind of object is to be interpreted as a single value. This means that if you want to actually return an array as a value, you must wrap it in another array, indicating “an array of one finish value, and that finish value also happens to be an array itself”.



128
129
130
# File 'lib/sawmill/entry_processor.rb', line 128

def finish
  nil
end

#message(entry_) ⇒ Object

Receive and process a Sawmill::Entry::Message.



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

def message(entry_)
  true
end

#unknown_data(entry_) ⇒ Object

Receive and process a Sawmill::Entry::UnknownData.



101
102
103
# File 'lib/sawmill/entry_processor.rb', line 101

def unknown_data(entry_)
  true
end