Sha256: cc256fdcc69bca7ebcc28213f5a6b56e1d4de99c296cb2d6785aeb2311964294

Contents?: true

Size: 1.23 KB

Versions: 7

Compression:

Stored size: 1.23 KB

Contents

module ONIX

  # Internal class representing 2-digit XML content
  #
  # In context:
  #  <element attribute="XMLAttributeRef">
  #   XMLTwoDigitRef
  #  </element>
  class TwoDigitType < ROXML::XMLRef # ::nodoc::
    attr_reader :cdata, :content

    def initialize(accessor, args, &block)
      super(accessor, args, &block)
      @content = args.content?
      @cdata = args.cdata?
    end

    # Updates the text in the given _xml_ block to
    # the _value_ provided.
    def update_xml(xml, value)
      parent = wrap(xml)
      value = value.to_i
      if value < 10
        value = "0#{value}"
      elsif value < 100
        value = value.to_s
      else
        value = "00"
      end
      add(parent.child_add(LibXML::XML::Node.new_element(name)), value)
      xml
    end

    def value(xml)
      if content
        value = xml.content.to_i
      else
        child = xml.search(name).first
        value = child.content.to_i if child
      end
      block ? block.call(value) : value
    end

    private

    def add(dest, value)
      if cdata
        dest.child_add(LibXML::XML::Node.new_cdata(value.to_utf))
      else
        dest.content = value.to_utf
      end
    end
  end
end

ROXML::TypeRegistry.register(:twodigit, ONIX::TwoDigitType)

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
onix-0.4.2 lib/onix/two_digit_type.rb
onix-0.4.3 lib/onix/two_digit_type.rb
onix-0.4.0 lib/onix/two_digit_type.rb
onix-0.4.4 lib/onix/two_digit_type.rb
onix-0.4.7 lib/onix/two_digit_type.rb
onix-0.4.5 lib/onix/two_digit_type.rb
onix-0.4.6 lib/onix/two_digit_type.rb