module XmlResource::Model::ClassMethods
Public Instance Methods
basename()
click to toggle source
# File lib/xml_resource/model.rb, line 49 def basename name.sub(/^.*::/, '') end
collection_from_xml(xml_or_string, default_attrs = {})
click to toggle source
# File lib/xml_resource/model.rb, line 45 def collection_from_xml(xml_or_string, default_attrs = {}) parse(xml_or_string).search(root).map { |element| from_xml(element, default_attrs) }.compact end
from_xml(xml_or_string, default_attrs = {})
click to toggle source
# File lib/xml_resource/model.rb, line 18 def from_xml(xml_or_string, default_attrs = {}) xml_or_string and xml = parse(xml_or_string) or return attrs = {} self.attributes.each do |name, options| if xpath = attribute_xpath(name) node = xml.at(xpath) value = node ? node.inner_text : nil if !value.nil? && type = attribute_type(name) value = cast_to(type, value) end attrs[name] = value end end instance = new(attrs.reject { |k, v| v.nil? }.reverse_merge(default_attrs)) self.objects.each do |name, options| if xpath = object_xpath(name) instance.public_send("#{name}=", object_class(name).from_xml(xml.at(xpath))) end end self.collections.each do |name, options| if xpath = collection_xpath(name) instance.public_send("#{name}=", collection_class(name).collection_from_xml(xml.at(xpath))) end end instance end
root()
click to toggle source
# File lib/xml_resource/model.rb, line 53 def root self.root_path || "./#{inflect(basename)}" end
root=(root)
click to toggle source
# File lib/xml_resource/model.rb, line 57 def root=(root) self.root_path = root end
Protected Instance Methods
has_attribute(name, options = {})
click to toggle source
# File lib/xml_resource/model.rb, line 69 def has_attribute(name, options = {}) self.attributes = attributes.merge(name.to_sym => options.symbolize_keys) attribute_accessor_method name end
has_attributes(*args)
click to toggle source
# File lib/xml_resource/model.rb, line 74 def has_attributes(*args) options = args.extract_options! args.each { |arg| has_attribute arg, options } end
has_collection(name, options = {})
click to toggle source
# File lib/xml_resource/model.rb, line 84 def has_collection(name, options = {}) self.collections = collections.merge(name.to_sym => options.symbolize_keys) define_method "#{name}" do instance_variable_get("@#{name}") or instance_variable_set("@#{name}", []) end define_method "#{name}=" do |assignment| instance_variable_set("@#{name}", assignment) if assignment end end
has_object(name, options = {})
click to toggle source
# File lib/xml_resource/model.rb, line 79 def has_object(name, options = {}) self.objects = objects.merge(name.to_sym => options.symbolize_keys) attr_accessor name end
Private Instance Methods
attribute_accessor_method(name)
click to toggle source
# File lib/xml_resource/model.rb, line 96 def attribute_accessor_method(name) define_method("#{name}") { self[name] } define_method("#{name}=") { |value| self[name] = value } end
attribute_type(name)
click to toggle source
# File lib/xml_resource/model.rb, line 108 def attribute_type(name) attributes[name] && attributes[name][:type] end
cast_to(type, value)
click to toggle source
# File lib/xml_resource/model.rb, line 128 def cast_to(type, value) # only called for non-nil values case type when :string then value when :integer then value.to_i when :float then value.to_f when :boolean then cast_to_boolean(value) when :decimal then BigDecimal(value, Float::DIG + 1) when :date then value.presence && Date.parse(value) when :time then value.presence && Time.parse(value) else raise XmlResource::TypeCastError, "don't know how to cast #{value.inspect} to #{type}" end end
cast_to_boolean(value)
click to toggle source
# File lib/xml_resource/model.rb, line 142 def cast_to_boolean(value) if value.is_a?(String) && value.blank? nil else TRUE_VALUES.include?(value) end end
collection_class(name)
click to toggle source
# File lib/xml_resource/model.rb, line 120 def collection_class(name) if collections[name] && class_name = collections[name][:class_name] class_name.constantize else name.to_s.singularize.camelize.constantize end end
inflect(string)
click to toggle source
# File lib/xml_resource/model.rb, line 162 def inflect(string) string = string.to_s case inflection when :lower_camelcase string.camelcase(:lower) when :upper_camelcase string.camelcase(:upper) when :dasherize string.underscore.dasherize when nil string.underscore else string.public_send(inflection) end end
object_class(name)
click to toggle source
# File lib/xml_resource/model.rb, line 112 def object_class(name) if objects[name] && class_name = objects[name][:class_name] class_name.constantize else name.to_s.camelize.constantize end end
parse(xml_or_string)
click to toggle source
# File lib/xml_resource/model.rb, line 150 def parse(xml_or_string) case xml_or_string when Nokogiri::XML::Node, Nokogiri::XML::NodeSet then xml_or_string when String xml = Nokogiri.XML(xml_or_string) xml.remove_namespaces! if remove_namespaces xml.root else raise XmlResource::ParseError, "cannot parse #{xml_or_string.inspect}" end end