Class: XmlDsl::XmlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/xml_dsl/xml_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, target_class, *args, &block) ⇒ XmlParser

Returns a new instance of XmlParser.

Raises:



15
16
17
18
19
20
21
22
23
# File 'lib/xml_dsl/xml_parser.rb', line 15

def initialize(owner, target_class, *args, &block)
  raise XmlDsl::Error, 'No block given for parser' unless block_given?
  @target_class = target_class
  @root_path = args
  @state_lock = Mutex.new
  self.owner = owner
  @callbacks = Hash.new { |h,k| h[k] = [] }
  self.instance_eval &block
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



13
14
15
# File 'lib/xml_dsl/xml_parser.rb', line 13

def callbacks
  @callbacks
end

#ownerObject

Returns the value of attribute owner.



13
14
15
# File 'lib/xml_dsl/xml_parser.rb', line 13

def owner
  @owner
end

#state_lockObject (readonly)

Returns the value of attribute state_lock.



13
14
15
# File 'lib/xml_dsl/xml_parser.rb', line 13

def state_lock
  @state_lock
end

#target_classObject (readonly)

Returns the value of attribute target_class.



13
14
15
# File 'lib/xml_dsl/xml_parser.rb', line 13

def target_class
  @target_class
end

Class Method Details

.create(owner, *args, &block) ⇒ Object



8
9
10
# File 'lib/xml_dsl/xml_parser.rb', line 8

def create(owner, *args, &block)
  new(owner, *args, &block)
end

Instance Method Details

#before_parse?(*args, &block) ⇒ Boolean

Validation like block receives key: symbol, string, or array of similar returns bool if true - normal, false - skip this node

Returns:

  • (Boolean)


48
49
50
# File 'lib/xml_dsl/xml_parser.rb', line 48

def before_parse?(*args, &block)
  callbacks[:before_parsers] << XmlDsl::BlockMethod.new(:before_parse?, *args, &block)
end

#error_handle(*args, &block) ⇒ Object

Error handler for automatic or manually raised XmlDsl::ParseError exceptions receives block with two args error and node, that caused the error to occur block with |exception, node|



41
42
43
# File 'lib/xml_dsl/xml_parser.rb', line 41

def error_handle(*args, &block)
  callbacks[:error_handlers] << XmlDsl::BlockMethod.new(:error_handle, *args, &block)
end

#field(*args, &block) ⇒ Object

Field for parse definition receives args: target, source = nil, getter: :text, matcher: :to_s, null: false or block with |instance, node|



34
35
36
# File 'lib/xml_dsl/xml_parser.rb', line 34

def field(*args, &block)
  callbacks[:readers] << XmlDsl::BlockMethod.new(:field, *args, &block)
end

#root_pathObject



60
61
62
63
64
# File 'lib/xml_dsl/xml_parser.rb', line 60

def root_path
  rp = @root_path.join('/')
  rp.prepend('//') if @root_path.map {|part| part.is_a? Symbol}.reduce(true, :&)
  rp
end

#setup_parser_instance(instance) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/xml_dsl/xml_parser.rb', line 52

def setup_parser_instance(instance)
  state_lock.synchronize do
    instance.instance_variable_set :@_xml_root_path, root_path
    instance.instance_variable_set :@_xml_parse_callbacks, callbacks.dup
    instance.instance_variable_set :@_xml_target_class, target_class
  end
end