Module: Sequel::Plugins::XmlSerializer::ClassMethods

Defined in:
lib/sequel/plugins/xml_serializer.rb

Constant Summary collapse

CAMELIZE =

Proc that camelizes the input string, used for the :camelize option

:camelize.to_proc
DASHERIZE =

Proc that dasherizes the input string, used for the :dasherize option

:dasherize.to_proc
IDENTITY =

Proc that returns the input string as is, used if no :name_proc, :dasherize, or :camelize option is used.

proc{|s| s}
UNDERSCORE =

Proc that underscores the input string, used for the :underscore option

:underscore.to_proc

Instance Method Summary collapse

Instance Method Details

#array_from_xml(xml, opts = OPTS) ⇒ Object

Return an array of instances of this class based on the provided XML.



132
133
134
135
136
137
138
# File 'lib/sequel/plugins/xml_serializer.rb', line 132

def array_from_xml(xml, opts=OPTS)
  node = Nokogiri::XML(xml).children.first
  unless node 
    raise Error, "Malformed XML used"
  end
  node.children.reject{|c| c.is_a?(Nokogiri::XML::Text)}.map{|c| from_xml_node(c, opts)}
end

#from_xml(xml, opts = OPTS) ⇒ Object

Return an instance of this class based on the provided XML.



141
142
143
# File 'lib/sequel/plugins/xml_serializer.rb', line 141

def from_xml(xml, opts=OPTS)
  from_xml_node(Nokogiri::XML(xml).children.first, opts)
end

#from_xml_node(parent, opts = OPTS) ⇒ Object

Return an instance of this class based on the given XML node, which should be Nokogiri::XML::Node instance. This should not be used directly by user code.



148
149
150
# File 'lib/sequel/plugins/xml_serializer.rb', line 148

def from_xml_node(parent, opts=OPTS)
  new.from_xml_node(parent, opts)
end

#xml_builder(opts = OPTS) ⇒ Object

Return an appropriate Nokogiri::XML::Builder instance used to create the XML. This should not be used directly by user code.



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/sequel/plugins/xml_serializer.rb', line 155

def xml_builder(opts=OPTS)
  if opts[:builder]
    opts[:builder]
  else
    builder_opts = if opts[:builder_opts]
      Hash[opts[:builder_opts]]
    else
      {}
    end
    builder_opts[:encoding] = opts[:encoding] if opts.has_key?(:encoding)
    Nokogiri::XML::Builder.new(builder_opts)
  end
end

#xml_deserialize_name_proc(opts = OPTS) ⇒ Object

Return a proc (or any other object that responds to []), used for formatting XML tag names when serializing to XML. This should not be used directly by user code.



172
173
174
175
176
177
178
179
180
# File 'lib/sequel/plugins/xml_serializer.rb', line 172

def xml_deserialize_name_proc(opts=OPTS)
  if opts[:name_proc]
    opts[:name_proc]
  elsif opts[:underscore]
    UNDERSCORE
  else
    IDENTITY
  end
end

#xml_serialize_name_proc(opts = OPTS) ⇒ Object

Return a proc (or any other object that responds to []), used for formatting XML tag names when serializing to XML. This should not be used directly by user code.



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/sequel/plugins/xml_serializer.rb', line 185

def xml_serialize_name_proc(opts=OPTS)
  pr = if opts[:name_proc]
    opts[:name_proc]
  elsif opts[:dasherize]
    DASHERIZE
  elsif opts[:camelize]
    CAMELIZE
  else
    IDENTITY
  end
  proc{|s| "#{pr[s]}_"}
end