Sha256: 97c0a7996fa3fe481b1ebd82bd62112d1abcade5c03d04a55854bbd7a51c5724

Contents?: true

Size: 614 Bytes

Versions: 3

Compression:

Stored size: 614 Bytes

Contents

# frozen_string_literal: true

module TJSON
  # TJSON object type (i.e. hash/dict-alike)
  class Object < ::Hash
    def []=(tagged_name, value)
      # NOTE: this regex is sloppy. The real parsing is performed in TJSON::DataType#parse
      result = tagged_name.match(/\A(?<name>.*):(?<tag>[A-Za-z0-9\<]+[\>]*)\z/)

      raise ParseError, "invalid tag: #{tagged_name.inspect}" unless result
      raise DuplicateNameError, "duplicate member name: #{result[:name].inspect}" if key?(result[:name])

      type = TJSON::DataType.parse(result[:tag])
      super(result[:name], type.convert(value))
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tjson-0.4.0 lib/tjson/object.rb
tjson-0.3.0 lib/tjson/object.rb
tjson-0.2.0 lib/tjson/object.rb