class Hashema::Compiler

Public Class Methods

compile(thing, options={}) click to toggle source
# File lib/hashema/compiler.rb, line 5
def self.compile(thing, options={})
  new(options).compile(thing)
end
new(options={}) click to toggle source
# File lib/hashema/compiler.rb, line 9
def initialize(options={})
  @options = options
end

Public Instance Methods

compile(thing) click to toggle source
# File lib/hashema/compiler.rb, line 13
def compile(thing)
  case thing
  when Hashema::Optional
    Hashema::OptionalValueInHash.new(compile(thing.expected))
  when ::Array
    if thing.size == 1
      Hashema::Array.new(compile(thing[0]))
    else
      Hashema::Alternatives.new(*(thing.map { |element| compile element }))
    end
  when ::Hash
    compile_hash(thing)
  else
    Hashema::Atom.new(thing)
  end
end

Private Instance Methods

compile_hash(hash) click to toggle source
# File lib/hashema/compiler.rb, line 32
def compile_hash(hash)
  with_compiled_values = ::Hash[hash.map { |k, v| [k, compile(v)]}]
  klass = @options[:indifferent_access] ?
    Hashema::HashWithIndifferentAccess :
    Hashema::Hash
  klass.new(with_compiled_values)
end