Class: Sawtooth::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/sawtooth/builder.rb

Overview

Provides a nice and hopefully easy to use DSL to build rules and start parsing XML documents with ease.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Builder

Creates a new instance.



22
23
24
25
# File 'lib/sawtooth/builder.rb', line 22

def initialize(&block)
  @rules = Sawtooth::Rules::Set.new
  self.instance_eval(&block) if block_given?
end

Instance Attribute Details

#rulesObject (readonly)

Has access to a set of rules.



19
20
21
# File 'lib/sawtooth/builder.rb', line 19

def rules
  @rules
end

Instance Method Details

#after(&block) ⇒ Object

Called after the document has ended.



45
46
47
# File 'lib/sawtooth/builder.rb', line 45

def after(&block)
  rules.add('@document:after', Sawtooth::Rules::CallRule.new(:finish => block)) if block_given?
end

#before(&block) ⇒ Object

Called before the document starts.



40
41
42
# File 'lib/sawtooth/builder.rb', line 40

def before(&block)
  rules.add('@document:before', Sawtooth::Rules::CallRule.new(:start => block)) if block_given?
end

#delegate(delegation = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/sawtooth/builder.rb', line 92

def delegate(delegation = {})
  path = delegation.keys.find { |k| k.to_s =~ %r{/\*\*?\z} }
  cb_path = path.gsub(%r{/\*\*?\z}, '')
  to = delegation[path]
  prefix = delegation[:prefix] || path.gsub(%r{/?[^/]+/\*\*?\z}, '')

  rule = Sawtooth::Rules::DelegateRule.new(:path => path, :rules => to.respond_to?(:rules) ? to.rules : to, :prefix => prefix)
  rules.add(cb_path, rule.before_after_callbacks_rule)
  rules.add(path, rule)
end

#on(path, rule = nil, &block) ⇒ Object

Perform a rule on a block, optionally pass in a custom rule instance.



61
62
63
64
# File 'lib/sawtooth/builder.rb', line 61

def on(path, rule = nil, &block)
  rule = block.arity <= 0 ? Sawtooth::Rules::CallRule.new(&block) : Sawtooth::Rules::CallRule.new(:start => block) if block_given?
  rules.add(path, rule) if rule
end

#on_finish(path, &block) ⇒ Object Also known as: on_node

Called when the node has finished parsing, i.e. text and everything is available.



55
56
57
# File 'lib/sawtooth/builder.rb', line 55

def on_finish(path, &block)
  rules.add(path, Sawtooth::Rules::CallRule.new(:finish => block)) if block_given?
end

#on_start(path, &block) ⇒ Object



49
50
51
# File 'lib/sawtooth/builder.rb', line 49

def on_start(path, &block)
  rules.add(path, Sawtooth::Rules::CallRule.new(:start => block)) if block_given?
end

#on_text(mappings = {}, &block) ⇒ Object

Use and set a nodes text to the top object in the stack.

# Simple mapping, sets "name"
on_text 'Person/Name'

# Custom mapping
on_text 'Person/Name' => :lastname

# Data Conversion
on_text('Person/Age') { |str| str.to_i }

# Multiple Mappings
on_text 'Person/Name' => :lastname, 'Person/FirstName' => :firstname

The ‘TextRule` tries to set the value using a setter, or a hash accessor and the `document.top` object.



82
83
84
85
86
87
88
89
90
# File 'lib/sawtooth/builder.rb', line 82

def on_text(mappings = {}, &block)
  if mappings.respond_to?(:to_str)
    rules.add(mappings.to_str, Sawtooth::Rules::TextRule.new(&block))
  else
    mappings.each do |path, name|
      rules.add(path, Sawtooth::Rules::TextRule.new(name, &block))
    end
  end
end

#parse(thingy) ⇒ Object

Shortcut method to parse some input, delegates to the parser.



35
36
37
# File 'lib/sawtooth/builder.rb', line 35

def parse(thingy)
  parser.parse(thingy)
end

#parserObject

Get a parser instance with the same set of rules.



29
30
31
# File 'lib/sawtooth/builder.rb', line 29

def parser
  @parser ||= Sawtooth::Parser.new(:rules => self.rules)
end

#to_pretty_sObject

Pretty print rules.



104
# File 'lib/sawtooth/builder.rb', line 104

def to_pretty_s; rules.print_rules end