Sha256: 808d7c8c8cbfd990e391adba756e615c228f4e89bc15619d819133da91a3b303

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

require 'xommelier/xml'
require 'xommelier/xml/element/structure'
require 'xommelier/xml/element/serialization'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/class/attribute'

module Xommelier
  module Xml
    DEFAULT_OPTIONS = {
      type: String
    }
    DEFAULT_ELEMENT_OPTIONS = DEFAULT_OPTIONS.merge(
      count: :one
    )

    class Element
      include Xommelier::Xml::Element::Structure
      include Xommelier::Xml::Element::Serialization

      attr_reader :options

      def initialize(contents = {}, options = {})
        self.options = options

        @elements = {}
        @attributes = {}
        @text = nil

        self.class.attributes.each do |name, options|
          send("#{name}=", options[:default]) if options[:default]
        end

        self.class.elements.each do |name, options|
          send("#{name}=", options[:default]) if options[:default]
        end

        case contents
        when Hash
          contents.each do |name, value|
            send("#{name}=", value)
          end
        else
          send(:text=, contents)
        end
      end

      def options=(options = {})
        @options = options
        @options.delete(:type)

        if @options.key?(:element_name)
          element_name(@options.delete(:element_name))
        end
      end

      def inspect
        %(#<#{self.class.name}:0x#{object_id.to_s(16)} #{inspect_contents}>)
      end

      private

      def inspect_contents
        [inspect_attributes, inspect_elements, inspect_text].compact.join(' ')
      end

      def inspect_attributes
        "@attributes={#{@attributes.map { |name, value| "#{name}: #{value.inspect}"}.join(', ')}}" if @attributes.any?
      end

      def inspect_elements
        "#{@elements.map { |name, value| "@#{name}=#{value.inspect}"}.join(' ')}" if @elements.any?
      end

      def inspect_text
        text.inspect if text?
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
xommelier-0.1.8 lib/xommelier/xml/element.rb
xommelier-0.1.7 lib/xommelier/xml/element.rb
xommelier-0.1.6 lib/xommelier/xml/element.rb
xommelier-0.1.5 lib/xommelier/xml/element.rb