class AwesomeXmlDsl::Tag

Public Class Methods

new(name, generator, depth, options = {}) click to toggle source
# File lib/awesome_xml_dsl/tag.rb, line 5
def initialize(name, generator, depth, options = {})
  @name = name
  @generator = generator
  @depth = depth
  @options = options

  @attributes = []
  @tags = []
end

Public Instance Methods

a(name, options_or_value = {}) click to toggle source

Block evaluation

# File lib/awesome_xml_dsl/tag.rb, line 16
def a(name, options_or_value = {})
  # It is options
  if options_or_value.is_a? Hash
    AttributeOptionsParser.parse(options_or_value).each do |value|
      attribute = Attribute.new(name, value)
      @attributes.push attribute
    end

    return
  end

  # It is a value
  attribute = Attribute.new(name, options_or_value)
  @attributes.push attribute
end
attributes_as_xml(spaces) click to toggle source
# File lib/awesome_xml_dsl/tag.rb, line 54
def attributes_as_xml(spaces)
  return '' unless @attributes.any?
  return " #{@attributes.first.to_xml}" if @attributes.length == 1

  ([''] + @attributes.map(&:to_xml).compact.map { |attribute| "#{spaces}#{attribute}" }).join("\n")
end
method_missing(m, *args, &block) click to toggle source
# File lib/awesome_xml_dsl/tag.rb, line 61
def method_missing(m, *args, &block)
  return @options[:locals][m] if @options[:locals]&.key?(m)

  @generator.send(m, *args, &block)
end
partial(name, options = {}) click to toggle source
# File lib/awesome_xml_dsl/tag.rb, line 40
def partial(name, options = {})
  @generator.partial(name, options, self, @options)
end
tag(name, options = {}, &block) click to toggle source
# File lib/awesome_xml_dsl/tag.rb, line 32
def tag(name, options = {}, &block)
  OptionsParser.parse(options, @options).each do |parsed_options|
    xml_tag = Tag.new(name, @generator, @depth + 1, parsed_options)
    @tags.push xml_tag
    xml_tag.instance_eval(&block) if block_given?
  end
end
to_xml() click to toggle source

XML Generation

# File lib/awesome_xml_dsl/tag.rb, line 45
def to_xml
  spaces = ' ' * @depth * 2
  start = "#{spaces}<#{@name}#{attributes_as_xml(spaces + '  ')}"

  return start + ' />' if @tags.length.zero?

  [start + '>', @tags.map(&:to_xml).join("\n"), "#{spaces}</#{@name}>"].join "\n"
end