Class: SAXMachine::SAXHandler

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/sax-machine/sax_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ SAXHandler

Returns a new instance of SAXHandler.



7
8
9
10
# File 'lib/sax-machine/sax_handler.rb', line 7

def initialize(object)
  @object = object
  @parsed_configs = {}
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/sax-machine/sax_handler.rb', line 5

def object
  @object
end

Instance Method Details

#cdata_block(string) ⇒ Object



20
21
22
# File 'lib/sax-machine/sax_handler.rb', line 20

def cdata_block(string)
  characters(string)
end

#characaters_captured?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/sax-machine/sax_handler.rb', line 60

def characaters_captured?
  !@value.nil? && !@value.empty?
end

#characters(string) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/sax-machine/sax_handler.rb', line 12

def characters(string)
  if parsing_collection?
    @collection_handler.characters(string)
  elsif @element_config
    @value << string
  end
end

#end_element(name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sax-machine/sax_handler.rb', line 44

def end_element(name)
  if parsing_collection? && @collection_config.name == name
    @object.send(@collection_config.accessor) << @collection_handler.object
    reset_current_collection
    
  elsif parsing_collection?
    @collection_handler.end_element(name)
    
  elsif characaters_captured? && !parsed_config?
    mark_as_parsed
    @object.send(@element_config.setter, @value)
  end
  
  reset_current_tag
end

#mark_as_parsedObject



79
80
81
82
83
# File 'lib/sax-machine/sax_handler.rb', line 79

def mark_as_parsed
  # TODO: make this code not suck like this
  @parsed_configs[@element_config] = true unless (@element_config.respond_to?(:collection?) && @element_config.collection?) || 
    (@element_config.class == Array && @element_config.first.collection?)
end

#parse_element_attributeObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/sax-machine/sax_handler.rb', line 68

def parse_element_attribute
  unless parsed_config?
    mark_as_parsed
    @element_config.each do |config|
      @object.send(config.setter, config.value_from_attrs(@attrs))
    end
  end
  
  @element_config = nil
end

#parsed_config?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/sax-machine/sax_handler.rb', line 85

def parsed_config?
  @parsed_configs[@element_config]
end

#parsing_collection?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/sax-machine/sax_handler.rb', line 64

def parsing_collection?
  !@collection_handler.nil?
end

#reset_current_collectionObject



89
90
91
92
# File 'lib/sax-machine/sax_handler.rb', line 89

def reset_current_collection
  @collection_handler = nil
  @collection_config  = nil
end

#reset_current_tagObject



94
95
96
97
98
99
# File 'lib/sax-machine/sax_handler.rb', line 94

def reset_current_tag
  @name   = nil
  @attrs  = nil
  @value  = nil
  @element_config = nil
end

#sax_configObject



101
102
103
# File 'lib/sax-machine/sax_handler.rb', line 101

def sax_config
  @object.class.sax_config
end

#start_element(name, attrs = []) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sax-machine/sax_handler.rb', line 24

def start_element(name, attrs = [])
  @name   = name
  @attrs  = attrs
  
  if parsing_collection?
    @collection_handler.start_element(@name, @attrs)
    
  elsif @collection_config = sax_config.collection_config(@name)
    @collection_handler = @collection_config.handler
    @collection_handler.start_element(@name, @attrs)
    
  elsif @element_config = sax_config.element_config_for_attribute(@name, @attrs)
    parse_element_attribute
    
  else
    @value = ""
    @element_config = sax_config.element_config_for_tag(@name, @attrs)
  end
end