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

proc{|s| s.camelize}
DASHERIZE =

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

proc{|s| s.dasherize}
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

proc{|s| s.underscore}

Instance Method Summary collapse

Instance Method Details

#array_from_xml(xml, opts = {}) ⇒ Object

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



111
112
113
# File 'lib/sequel/plugins/xml_serializer.rb', line 111

def array_from_xml(xml, opts={})
  Nokogiri::XML(xml).children.first.children.reject{|c| c.is_a?(Nokogiri::XML::Text)}.map{|c| from_xml_node(c, opts)}
end

#from_xml(xml, opts = {}) ⇒ Object

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



117
118
119
# File 'lib/sequel/plugins/xml_serializer.rb', line 117

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

#from_xml_node(parent, opts = {}) ⇒ Object

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



124
125
126
# File 'lib/sequel/plugins/xml_serializer.rb', line 124

def from_xml_node(parent, opts={})
  new.from_xml_node(parent, opts)
end

#to_xml(opts = {}) ⇒ Object

Call the dataset to_xml method.



129
130
131
# File 'lib/sequel/plugins/xml_serializer.rb', line 129

def to_xml(opts={})
  dataset.to_xml(opts)
end

#xml_builder(opts = {}) ⇒ Object

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



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/sequel/plugins/xml_serializer.rb', line 136

def xml_builder(opts={})
  if opts[:builder]
    opts[:builder]
  else
    builder_opts = if opts[:builder_opts]
      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 = {}) ⇒ Object

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



153
154
155
156
157
158
159
160
161
# File 'lib/sequel/plugins/xml_serializer.rb', line 153

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

#xml_serialize_name_proc(opts = {}) ⇒ Object

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



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/sequel/plugins/xml_serializer.rb', line 166

def xml_serialize_name_proc(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