Class: XML::Smart::Dom::NamespaceSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/xml/smart_domnamespaceset.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent, node) ⇒ NamespaceSet

Returns a new instance of NamespaceSet.



8
9
10
11
# File 'lib/xml/smart_domnamespaceset.rb', line 8

def initialize(parent,node)
  @parent = parent
  @node = node
end

Instance Method Details

#===(cls) ⇒ Object



13
# File 'lib/xml/smart_domnamespaceset.rb', line 13

def ===(cls); self.is_a? cls; end

#[](name) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/xml/smart_domnamespaceset.rb', line 20

def [](name)
  if name.nil? || name.respond_to?(:to_s)
    name = name.to_s unless name.nil?
    name = nil if name =~ /^xmlns\d*$/
    nd = @node.namespace_definitions.find{|n| n.prefix == name}
    (name.respond_to?(:to_s) || name.nil?) && nd ? Namespace.new(nd) : nil; 
  end  
end

#[]=(name, value) ⇒ Object



28
29
30
31
# File 'lib/xml/smart_domnamespaceset.rb', line 28

def []=(name,value) 
  name = nil if name == 'xmlns'
  self.add(name,value)
end

#add(name, value) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/xml/smart_domnamespaceset.rb', line 33

def add(name,value)
  if (name.respond_to?(:to_s) || name.nil?) && value.respond_to?(:to_s) 
    nnod = Nokogiri::XML::Node.new(@node.name,@node.document)
    nnew = Element.new(nnod)
    nold = Element.new(@node)
    nold.attributes.each do |attr|
      nnew.attributes[attr.qname.name] = attr.value
    end
    ns = nnod.add_namespace_definition(name.nil? ? nil : name.to_s,value.to_s)
    @node.namespace_definitions.each do |ns|
      nnod.add_namespace_definition(ns.prefix,ns.href)
    end
    nnew.add(nold.children)
    nold.replace_by(nnew)
    @node = nnod
    @parent.instance_variable_set(:@element,@node) 
    @node.document.custom_namespace_prefixes_update
    @node.document.ns_update
    Namespace.new(ns)
  end  
end

#delete_all!Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/xml/smart_domnamespaceset.rb', line 57

def delete_all!
  nnod = Nokogiri::XML::Node.new(@node.name,@node.document)
  nnew = Element.new(nnod)
  nold = Element.new(@node)
  nold.attributes.each do |attr|
    nnew.attributes[attr.qname.name] = attr.value
  end
  nnew.add(nold.children)
  nold.replace_by(nnew)
  @node = nnod
  @parent.instance_variable_set(:@element,@node)
  @node.document.custom_namespace_prefixes_update
  @node.document.ns_update
  nil
end

#delete_at(name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/xml/smart_domnamespaceset.rb', line 79

def delete_at(name)
  if (name.respond_to?(:to_s) || name.nil?)
    return false unless @node.namespace_definitions.one?{|e| e.prefix == name}

    nnod = Nokogiri::XML::Node.new(@node.name,@node.document)
    nnew = Element.new(nnod)
    nold = Element.new(@node)
    nold.attributes.each do |attr|
      nnew.attributes[attr.qname.name] = attr.value
    end
    @node.namespace_definitions.each do |ns|
      nnod.add_namespace_definition(ns.prefix,ns.href) unless ns.prefix == name
    end
    nnew.add(nold.children)
    nold.replace_by(nnew)
    @node = nnod
    @parent.instance_variable_set(:@element,@node)
    @node.document.custom_namespace_prefixes_update
    @node.document.ns_update
    true
  else
    false
  end
end

#delete_if(&block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/xml/smart_domnamespaceset.rb', line 104

def delete_if(&block)
  nnod = Nokogiri::XML::Node.new(@node.name,@node.document)
  nnew = Element.new(nnod)
  nold = Element.new(@node)
  nold.attributes.each do |attr|
    nnew.attributes[attr.qname.name] = attr.value
  end
  @node.namespace_definitions.each do |ns| 
    nnod.add_namespace_definition(ns.prefix,ns.href) unless block.call(Dom::smart_helper(ns))
  end
  nnew.add(nold.children)
  nold.replace_by(nnew)
  @node = nnod
  @parent.instance_variable_set(:@element,@node)
  @node.document.custom_namespace_prefixes_update
  @node.document.ns_update
  self
end

#each(&block) ⇒ Object



73
74
75
76
77
# File 'lib/xml/smart_domnamespaceset.rb', line 73

def each(&block)
  @node.namespace_definitions.each do |ns|
    block.call Namespace.new(ns)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


56
# File 'lib/xml/smart_domnamespaceset.rb', line 56

def empty?;      @node.namespace_definitions.empty?; end

#has_ns?(a) ⇒ Boolean Also known as: include?, ns?, member?

Returns:

  • (Boolean)


15
# File 'lib/xml/smart_domnamespaceset.rb', line 15

def has_ns?(a); @node.namespace_definitions.one?{|e| e.href == a || e.prefix == a}; end

#lengthObject



55
# File 'lib/xml/smart_domnamespaceset.rb', line 55

def length;      @node.namespace_definitions.length; end