Module: SMG::Model

Defined in:
lib/smg/model.rb

Instance Method Summary collapse

Instance Method Details

#collect(tag, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/smg/model.rb', line 18

def collect(tag, options = {})

  options.merge!(:collection => true)
  thing = Class === options[:class] ? mapping.attach_nested(tag,options) : mapping.attach_element(tag,options)

  if (instance_methods & [thing.accessor, thing.accessor.to_s]).empty?
    class_eval(<<-EOS, __FILE__, __LINE__ + 1)
    def #{thing.accessor}(value)
      @#{thing.name} ||= []
      @#{thing.name} << value
    end
    EOS
  end

  if (instance_methods & [thing.name, thing.name.to_s]).empty?
    class_eval(<<-EOS, __FILE__, __LINE__ + 1)
    def #{thing.name}
      @#{thing.name} ||= []
    end
    EOS
  end

end

#extract(tag, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/smg/model.rb', line 4

def extract(tag, options = {})

  thing = if Class === options[:class]
    options.delete(:collection)
    mapping.attach_nested(tag,options)
  else
    mapping.attach_element(tag,options)
  end

  attr_reader thing.name if (instance_methods & [thing.name, thing.name.to_s]).empty?
  attr_writer thing.name if (instance_methods & [thing.accessor, thing.accessor.to_s]).empty?

end

#mappingObject



61
62
63
# File 'lib/smg/model.rb', line 61

def mapping
  @mapping ||= Mapping.new
end

#parse(data, context = nil) ⇒ Object



47
48
49
50
51
52
# File 'lib/smg/model.rb', line 47

def parse(data, context = nil)
  doc = SMG::Document.new(resource = new,context)
  ::Nokogiri::XML::SAX::Parser.new(doc).parse(data)
  resource.parsed!
  resource
end

#parse_file(fname, context = nil) ⇒ Object



54
55
56
57
58
59
# File 'lib/smg/model.rb', line 54

def parse_file(fname, context = nil)
  doc = SMG::Document.new(resource = new,context)
  ::Nokogiri::XML::SAX::Parser.new(doc).parse_file(fname)
  resource.parsed!
  resource
end

#root(tag) ⇒ Object



42
43
44
45
# File 'lib/smg/model.rb', line 42

def root(tag)
  mapping.use_root(tag)
  nil
end