Sha256: 1ae55d6f4e60f4952ed199adaed93ca2b8007244f19164cd931dea8e64d549ca

Contents?: true

Size: 831 Bytes

Versions: 2

Compression:

Stored size: 831 Bytes

Contents

require 'active_support/core_ext/string/inflections'
require 'ostruct'

module Saxy
  class Element
    attr_reader :attributes, :value

    def initialize
      @attributes = {}
      @value = nil
    end

    def set_attribute(name, value)
      name = attribute_name(name)
      attributes[name] ||= []
      attributes[name] << value
    end

    def append_value(string)
      unless (string = string.strip).empty?
        @value ||= ""
        @value << string
      end
    end

    def as_object
      if attributes.any?
        object = OpenStruct.new
        attributes.each do |name, value|
          value = value.first if value.size == 1
          object.send("#{name}=", value)
        end
        object
      else
        value
      end
    end

    def attribute_name(name)
      name.underscore
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
saxy-0.2.0 lib/saxy/element.rb
saxy-0.1.2 lib/saxy/element.rb