module Hobby::JSON::Keys::Singleton

Constants

TypeOfValueMustBe
ValueMayExistFor
ValueMustExistFor

Public Instance Methods

Both(key_parser, value_parser) click to toggle source
# File lib/hobby/json/keys.rb, line 86
def Both key_parser, value_parser
  -> json {
    if value = key_parser[json]
      value_parser[value]
    end
  }
end
initialize_copy() click to toggle source
Calls superclass method
# File lib/hobby/json/keys.rb, line 121
def initialize_copy
  super
  @keys = {}
end
key(key, type = nil) click to toggle source
# File lib/hobby/json/keys.rb, line 65
def key key, type = nil
  parser = if type
             Both ValueMustExistFor[key], TypeOfValueMustBe[type]
           else
             ValueMustExistFor[key]
           end

  self.keys[key] = parser
end
keys() click to toggle source
# File lib/hobby/json/keys.rb, line 61
def keys
  @keys ||= {}
end
optional(&block) click to toggle source
# File lib/hobby/json/keys.rb, line 114
def optional &block
  new_class = dup
  new_class.define_singleton_method :key, &method(:optional_key)
  new_class.instance_exec &block
  self.keys.merge! new_class.keys
end
optional_key(key, type = nil) click to toggle source
# File lib/hobby/json/keys.rb, line 75
def optional_key key, type = nil
  parser = if type
             Both ValueMayExistFor[key], TypeOfValueMustBe[type]
           else
             ValueMayExistFor[key]
           end

  self.keys[key] = parser
end
type(symbol, &default) click to toggle source
# File lib/hobby/json/keys.rb, line 131
def type symbol, &default
  types[symbol] = Type.new &default
  define_singleton_method symbol do |&custom|
    types[symbol].expand &custom
  end
end
types() click to toggle source
# File lib/hobby/json/keys.rb, line 127
def types
  @types ||= {}
end