Sha256: 2e52aaffbd8af04b4022c7519443c5befd96a4884d77b51e63e4c802dd9daefb

Contents?: true

Size: 1.28 KB

Versions: 7

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module OoxmlParser
  # Class for working with any tag contained only value
  class ValuedChild < OOXMLDocumentObject
    # @return [String] type of value
    attr_reader :type

    def initialize(type = :string, parent: nil)
      @type = type
      super(parent: parent)
    end

    # Parse ValuedChild object
    # @param node [Nokogiri::XML:Element] node to parse
    # @return [ValuedChild] result of parsing
    def parse(node)
      node.attributes.each do |key, value|
        case key
        when 'val'
          @value = value.value.to_s if type == :string
          @value = value_to_symbol(value) if type == :symbol
          @value = value.value.to_i if type == :integer
          @value = value.value.to_f if type == :float
          @value = parse_boolean(value.value.to_s) if type == :boolean
        end
      end
      self
    end

    # @return [Object] value of parameter
    def value
      return true if type == :boolean && @value.nil?

      @value
    end

    private

    # Handle boolean value
    # @param [String] value to parse
    # @return [Boolean] result
    def parse_boolean(value)
      return true if value == '1'
      return true if value == 'true'
      return false if value == '0'
      return false if value == 'false'
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ooxml_parser-0.37.1 lib/ooxml_parser/common_parser/common_data/valued_child.rb
ooxml_parser-0.37.0 lib/ooxml_parser/common_parser/common_data/valued_child.rb
ooxml_parser-0.36.1 lib/ooxml_parser/common_parser/common_data/valued_child.rb
ooxml_parser-0.36.0 lib/ooxml_parser/common_parser/common_data/valued_child.rb
ooxml_parser-0.35.0 lib/ooxml_parser/common_parser/common_data/valued_child.rb
ooxml_parser-0.34.2 lib/ooxml_parser/common_parser/common_data/valued_child.rb
ooxml_parser-0.34.1 lib/ooxml_parser/common_parser/common_data/valued_child.rb