class Keisan::Parsing::Hash

Attributes

key_value_pairs[R]

Public Class Methods

new(key_value_pairs) click to toggle source
# File lib/keisan/parsing/hash.rb, line 6
def initialize(key_value_pairs)
  @key_value_pairs = Array(key_value_pairs).map {|key_value_pair|
    validate_and_extract_key_value_pair(key_value_pair)
  }
end

Private Instance Methods

allowed_key?(key) click to toggle source
# File lib/keisan/parsing/hash.rb, line 26
def allowed_key?(key)
  case key
  when Tokens::String, Tokens::Boolean, Tokens::Null, Tokens::Number
    true
  else
    false
  end
end
validate_and_extract_key_value_pair(key_value_pair) click to toggle source
# File lib/keisan/parsing/hash.rb, line 14
def validate_and_extract_key_value_pair(key_value_pair)
  key, value = Util.array_split(key_value_pair) {|token| token.is_a?(Tokens::Colon)}
  raise Exceptions::ParseError.new("Invalid hash") unless key.size == 1 && value.size >= 1

  key = key.first
  if allowed_key?(key)
    [Parsing::String.new(key.value), Parsing::RoundGroup.new(value)]
  else
    raise Exceptions::ParseError.new("Invalid hash (keys must be constants)")
  end
end