Sha256: 8d0fb8b9ec3f136d2ba7c2c9dbc905b96d0823e66e1dd8d4559ad87c50e02e8f

Contents?: true

Size: 844 Bytes

Versions: 10

Compression:

Stored size: 844 Bytes

Contents

module Georgia
  class MenuAncestryParser

    def initialize serialized_string
      @ancestry = serialized_string
    end

    def to_hash
      pairs = split_parent_child_pairs
      parsed_hash = {}
      pairs.each_with_index do |p, index|
        attributes = LinkAttributes.new(p)
        parsed_hash[attributes.id] = {position: index+1, parent_id: attributes.parent_id}
      end
      parsed_hash
    end

    private

    def split_parent_child_pairs
      @ancestry.split('&')
    end

    class LinkAttributes

      def initialize serialized_string
        @serialized_string = serialized_string
      end

      def id
        @serialized_string.split('=')[0][5..-2]
      end

      def parent_id
        parent_id = @serialized_string.split('=')[1]
        parent_id == 'null' ? nil : parent_id.to_i
      end
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
georgia-0.8.0 app/parsers/georgia/menu_ancestry_parser.rb
georgia-0.7.8 app/parsers/georgia/menu_ancestry_parser.rb
georgia-0.7.7 app/parsers/georgia/menu_ancestry_parser.rb
georgia-0.7.6 app/parsers/georgia/menu_ancestry_parser.rb
georgia-0.7.5 app/parsers/georgia/menu_ancestry_parser.rb
georgia-0.7.4 app/parsers/georgia/menu_ancestry_parser.rb
georgia-0.7.3 app/parsers/georgia/menu_ancestry_parser.rb
georgia-0.7.2 app/parsers/georgia/menu_ancestry_parser.rb
georgia-0.7.1 app/parsers/georgia/menu_ancestry_parser.rb
georgia-0.7.0 app/parsers/georgia/menu_ancestry_parser.rb