module Xommelier::Xml::Element::Structure::ClassMethods

Public Instance Methods

any(&block) click to toggle source
# File lib/xommelier/xml/element/structure.rb, line 113
def any(&block)
  with_options(count: :any) { |any| any.instance_eval(&block) }
end
attribute(name, options = {}) click to toggle source

Defines containing attribute

# File lib/xommelier/xml/element/structure.rb, line 102
def attribute(name, options = {})
  options[:ns] ||= xmlns
  attributes[name] = Attribute.new(name, options)
  define_attribute_accessors(name)
end
element(name, options = {}) click to toggle source

Defines containing element @example

element :author, type: Xommelier::Atom::Person
# File lib/xommelier/xml/element/structure.rb, line 60
def element(name, options = {})
  # Set name, type and element name by reference if provided
  if name.is_a?(Hash)
    options = name
    name = nil
  end

  # Set type and element name from reference
  if options.key?(:ref)
    raise "#{options[:ref]} is not subclass of Xommelier::Element" unless referenceable?(options[:ref])

    options[:type] = options.delete(:ref)

    # Set element name from provided complex type
    options[:as]   ||= options[:type].element_name

    # Set attribute name from element name
    name           ||= options[:as].underscore.to_sym
  end

  # Try to define element name from
  options[:as] ||= name.to_s.camelize(:lower)

  # Set namespace from element type or wrapper xmlns
  options[:ns] ||= if referenceable?(options[:type])
                     options[:type].xmlns
                   else
                     xmlns
                   end

  if options[:fixed]
    options[:default] = options[:fixed]
    options[:count] = :one
  end

  element        = Element.new(name, options)
  elements[name] = element

  define_element_accessors(element)
end
inherited(child) click to toggle source

@param [Xommelier::Xml::Element] child

# File lib/xommelier/xml/element/structure.rb, line 52
def inherited(child)
  child.elements   = elements.dup
  child.attributes = attributes.dup
end
many(&block) click to toggle source
# File lib/xommelier/xml/element/structure.rb, line 117
def many(&block)
  with_options(count: :many) { |many| many.instance_eval(&block) }
end
may(&block) click to toggle source
# File lib/xommelier/xml/element/structure.rb, line 121
def may(&block)
  with_options(count: :may) { |may| may.instance_eval(&block) }
end
text(*) click to toggle source

Defines containing text

# File lib/xommelier/xml/element/structure.rb, line 109
def text(*)
  define_text_accessors
end

Protected Instance Methods

referenceable?(type) click to toggle source
# File lib/xommelier/xml/element/structure.rb, line 186
def referenceable?(type)
  type.is_a?(Class) && type < Xommelier::Xml::Element
end
rw_accessor(name, &block) click to toggle source

Defines read-write accessor for name and provides alias for write-only version

# File lib/xommelier/xml/element/structure.rb, line 181
def rw_accessor(name, &block)
  define_method(name, &block)
  alias_method "#{name}=", name
end

Private Instance Methods

define_attribute_accessors(name) click to toggle source
# File lib/xommelier/xml/element/structure.rb, line 163
def define_attribute_accessors(name)
  rw_accessor(name) do |*args|
    write_attribute(name, args[0]) if args.length == 1
    read_attribute(name)
  end
end
define_element_accessors(element) click to toggle source
# File lib/xommelier/xml/element/structure.rb, line 127
def define_element_accessors(element)
  name = element.name
  if element.multiple?
    # Define plural accessors
    plural = element.plural

    rw_accessor(plural) do |*args|
      args.flatten.each_with_index do |object, index|
        write_element(name, object, index)
      end unless args.empty?

      @elements[name] ||= []
    end

    # Define singular accessors for first element
    unless element.numbers_equal?
      rw_accessor(name) do |*args|
        send(plural, [args[0]]) if args.length == 1
        send(plural)[0]
      end
    end
  elsif element.fixed?
    # Define singular pseudo-accessors
    rw_accessor(name) do |*args|
      write_element(name, element.fixed) if args.length == 1
      element.fixed
    end
  else
    # Define singular accessors
    rw_accessor(name) do |*args|
      write_element(name, args[0]) if args.length == 1
      read_element(name)
    end
  end
end
define_text_accessors() click to toggle source
# File lib/xommelier/xml/element/structure.rb, line 170
def define_text_accessors
  rw_accessor(:text) do |*args|
    write_text(args[0]) if args.length == 1
    read_text
  end
  alias_attribute :content, :text
end