Class: Hypertemplate::Builder::Xml

Inherits:
Base
  • Object
show all
Defined in:
lib/hypertemplate/builder/xml.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

build, build_dsl, collection_helper_default_options, copy_internal_variables, #each, generic_helper, helper, member_helper_default_options, #method_missing, #ns, #write

Constructor Details

#initialize(obj, options = {}) ⇒ Xml

Returns a new instance of Xml.



11
12
13
14
15
16
17
# File 'lib/hypertemplate/builder/xml.rb', line 11

def initialize(obj, options = {})
  initialize_library
  @raw = Nokogiri::XML::Document.new
  @obj = obj
  @parent = @raw.create_element(options[:root] || "root")
  @parent.parent = @raw
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hypertemplate::Builder::Base

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



9
10
11
# File 'lib/hypertemplate/builder/xml.rb', line 9

def raw
  @raw
end

Class Method Details

.media_typesObject



5
6
7
# File 'lib/hypertemplate/builder/xml.rb', line 5

def self.media_types
  ["application/xml", "text/xml"]
end

Instance Method Details

#initialize_libraryObject



19
20
21
22
# File 'lib/hypertemplate/builder/xml.rb', line 19

def initialize_library
  return if defined?(::Nokogiri)
  require "nokogiri"
end

#insert_value(name, prefix, *args, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hypertemplate/builder/xml.rb', line 57

def insert_value(name, prefix, *args, &block)
  # Protected if empty array
  unless args.size == 1 and args.first == []
    node = create_element(name.to_s, prefix, *args)
    node.parent = @parent
    if block_given?
      @parent = node
      block.call
      @parent = node.parent
    end
  end
end


50
51
52
53
54
55
# File 'lib/hypertemplate/builder/xml.rb', line 50

def link(relationship, uri, options = {})
  options["rel"] = relationship.to_s
  options["href"] = uri
  options["type"] ||= options[:type] || "application/xml"
  insert_value("link", nil, options)
end

#members(options = {}, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hypertemplate/builder/xml.rb', line 24

def members(options = {}, &block)
  collection = options[:collection] || @obj
  raise Hypertemplate::BuilderError.new("Members method require a collection to execute") unless collection.respond_to?(:each)
  name = options[:root] || "member"
  collection.each do |element|
    member_root = @raw.create_element(name)
    member_root.parent = @parent
    @parent = member_root
    if block.arity==1
      # new dsl
      block.call(element)
    else
      # old dsl (deprecate at 1.3?)
      block.call(self, element)
    end
    @parent = member_root.parent
  end
end

#representationObject



70
71
72
# File 'lib/hypertemplate/builder/xml.rb', line 70

def representation
  @raw.to_xml
end

#values(options = {}) {|Values.new(self)| ... } ⇒ Object

Yields:



43
44
45
46
47
48
# File 'lib/hypertemplate/builder/xml.rb', line 43

def values(options = {}, &block)
  options.each do |key,value|
    apply_namespace(@parent, key.to_s, value)
  end
  yield Values.new(self)
end