Sha256: 584b183c5252adb6c8da86c9261ee4d5dcd7c5b48cb815890da0c875c4a55419

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module SAXMachine
  class SAXConfig
    class ElementConfig
      attr_reader :name, :setter

      def initialize(name, options)
        @name  = name.to_s
        @class = options[:class]

        if options.has_key?(:with)
          # for faster comparisons later
          @with = options[:with].to_a.flatten.collect {|o| o.to_s}
        else
          @with = nil
        end

        if options.has_key?(:value)
          @value = options[:value].to_s
        else
          @value = nil
        end

        @as = options[:as]
        @collection = options[:collection]

        if @collection
          @setter = "add_#{options[:as]}"
        else
          @setter = "#{@as}="
        end
      end

      def value_from_attrs(attrs)
        attrs.index(@value) ? attrs[attrs.index(@value) + 1] : nil
      end

      def attrs_match?(attrs)
        if @with
          @with == (@with & attrs)
        else
          true
        end
      end

      def has_value_and_attrs_match?(attrs)
        !@value.nil? && attrs_match?(attrs)
      end

      def collection?
        @collection
      end

      def handler
        SAXHandler.new(@class.new) if @class
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
UnderpantsGnome-sax-machine-0.0.14 lib/sax-machine/sax_element_config.rb