Class: AcDc::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/acdc/element.rb

Overview

Basic XML Element

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, options = {}, tag = nil) ⇒ Element

Constructor with the following:

Parameters:

  • value (Object)

    Any value to place in the tags

  • tag (String) (defaults to: nil)

    A tag name to use if not Element

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :single (Boolean)

    False if object is a collection



12
13
14
15
16
17
# File 'lib/acdc/element.rb', line 12

def initialize(value, options={}, tag=nil)
  @tag = tag ||= self.class.to_s.split("::").last
  @value = value
  @options = options
  @attributes = options.delete(:attributes)
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/acdc/element.rb', line 6

def attributes
  @attributes
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/acdc/element.rb', line 6

def options
  @options
end

#tagObject

Returns the value of attribute tag.



6
7
8
# File 'lib/acdc/element.rb', line 6

def tag
  @tag
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/acdc/element.rb', line 6

def value
  @value
end

Instance Method Details

#acdcObject

Converts the object to XML



20
21
22
23
24
25
26
27
28
# File 'lib/acdc/element.rb', line 20

def acdc
  xml = Builder::XmlMarkup.new
  if value.nil?
    xml.tag!(tag_content)
  else
    has_many? ? render_many(xml) : xml.tag!(tag_content) {|elem| elem << content}
  end
  xml.target!
end

#eql?(other) ⇒ Boolean

Overridden to compare the values and not the whole object

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/acdc/element.rb', line 41

def eql?(other)
  return false if other.nil?
  return false unless other.kind_of?(self.class)
  value.eql?(other.value)
end

#has_many?Boolean

True if object has a collection of values

Returns:

  • (Boolean)


31
32
33
# File 'lib/acdc/element.rb', line 31

def has_many?
  options.has_key?(:single) and !options[:single] and value.size > 1
end

#tag_nameObject

The name to use for the tag



36
37
38
# File 'lib/acdc/element.rb', line 36

def tag_name
  tag.to_s.camelize
end