Module: XML::Smart
- Defined in:
- lib/xml/smart.rb,
lib/xml/smart_dom.rb,
lib/xml/smart_qname.rb,
lib/xml/smart_domtext.rb,
lib/xml/smart_domother.rb,
lib/xml/smart_domelement.rb,
lib/xml/smart_domnodeset.rb,
lib/xml/smart_domattribute.rb,
lib/xml/smart_domnamespace.rb,
lib/xml/smart_domattributeset.rb,
lib/xml/smart_domnamespaceset.rb
Defined Under Namespace
Classes: Dom, Error, QName
Constant Summary
collapse
- COPY =
0
- MOVE =
1
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.modify(name, default = nil, &block) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/xml/smart.rb', line 107
def self::modify(name,default=nil,&block)
raise Error, 'first parameter has to be a filename or filehandle' unless name.is_a?(String) || name.is_a?(IO)
raise Error, 'a block is mandatory' unless block_given?
lfname = name.is_a?(String) ? name : name.fileno.to_s
lockfile = Lockfile.new(lfname + '.lock',LOCKFILE)
begin
lockfile.lock
so = Smart::open_unprotected(name,default)
block.call(so)
so.save_as(name)
ensure
lockfile.unlock
end
nil
end
|
.open(name, default = nil) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/xml/smart.rb', line 123
def self::open(name,default=nil)
raise Error, 'first parameter has to be a filename or filehandle' unless name.is_a?(String) || name.is_a?(IO)
raise Error, 'second parameter has to be an xml string' unless default.is_a?(String) || default.nil?
lfname = name.is_a?(String) ? name : name.fileno.to_s
lockfile = Lockfile.new(lfname + '.lock',LOCKFILE)
dom = nil
begin
lockfile.lock
dom = Smart::open_unprotected(name,default)
ensure
lockfile.unlock
end
if dom && block_given?
yield dom
nil
else
dom
end
end
|
.open_unprotected(name, default = nil) ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/xml/smart.rb', line 143
def self::open_unprotected(name,default=nil)
raise Error, 'first parameter has to be a filename or filehandle' unless name.is_a?(String) || name.is_a?(IO)
raise Error, 'second parameter has to be an xml string' unless default.is_a?(String) || default.nil?
dom = begin
io = name.is_a?(String) ? ::Kernel::open(name) : name
Dom.new Nokogiri::XML::parse(io){|config| config.noblanks.noent.nsclean.strict }
rescue
if default.nil?
raise Error, "could not open #{name}"
else
Smart::string(default)
end
end
if block_given?
yield dom
nil
else
dom
end
end
|
.string(str) ⇒ Object
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/xml/smart.rb', line 164
def self::string(str)
raise Error, 'first parameter has to be stringable (:to_s)' unless str.is_a?(String)
dom = Dom.new Nokogiri::XML::parse(str.to_s){|config| config.noblanks.noent.nsclean.strict }
if block_given?
yield dom
nil
else
dom
end
end
|
Instance Method Details
#initialize(name, default = nil) ⇒ Object
105
|
# File 'lib/xml/smart.rb', line 105
def initialize(name,default=nil); open(name,default); end
|