Module: Xommelier::Xml::Element::Structure::ClassMethods

Defined in:
lib/xommelier/xml/element/structure.rb

Instance Method Summary collapse

Instance Method Details

#any(&block) ⇒ Object



113
114
115
# 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 = {}) ⇒ Object

Defines containing attribute



102
103
104
105
106
# 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 = {}) ⇒ Object

Defines containing element

Examples:

element :author, type: Xommelier::Atom::Person


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ Object

Parameters:



52
53
54
55
# File 'lib/xommelier/xml/element/structure.rb', line 52

def inherited(child)
  child.elements   = elements.dup
  child.attributes = attributes.dup
end

#many(&block) ⇒ Object



117
118
119
# File 'lib/xommelier/xml/element/structure.rb', line 117

def many(&block)
  with_options(count: :many) { |many| many.instance_eval(&block) }
end

#may(&block) ⇒ Object



121
122
123
# File 'lib/xommelier/xml/element/structure.rb', line 121

def may(&block)
  with_options(count: :may) { |may| may.instance_eval(&block) }
end

#textObject

Defines containing text



109
110
111
# File 'lib/xommelier/xml/element/structure.rb', line 109

def text(*)
  define_text_accessors
end