Class: Datacaster::DefinitionDSL

Inherits:
Object
  • Object
show all
Includes:
Predefined
Defined in:
lib/datacaster/definition_dsl.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Predefined

#absent, #any, #array, #array_schema, #attribute, #cast, #cast_around, #check, #choosy_hash_schema, #choosy_schema, #compare, #decimal, #default, #float, #hash_schema, #hash_value, #hash_with_symbolized_keys, #included_in, #integer, #integer32, #iso8601, #merge_message_keys, #must_be, #non_empty_string, #optional, #optional_param, #partial_schema, #pass, #pass_if, #pattern, #pick, #relate, #remove, #responds_to, #run, #schema, #steps, #strict_hash_schema, #string, #switch, #to_boolean, #to_float, #to_integer, #transform, #transform_if_present, #transform_to_hash, #transform_to_value, #try, #uuid, #validate, #with

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Raises:

  • (RuntimeError)


31
32
33
34
# File 'lib/datacaster/definition_dsl.rb', line 31

def method_missing(m, *args)
  arg_string = args.empty? ? "" : "(#{args.map(&:inspect).join(', ')})"
  raise RuntimeError, "Datacaster: unknown definition '#{m}#{arg_string}'", caller
end

Class Method Details

.eval(&block) ⇒ Object



27
28
29
# File 'lib/datacaster/definition_dsl.rb', line 27

def self.eval(&block)
  new.instance_eval(&block)
end

.expand(definition) ⇒ Object

Translates hashes like <IntegerChecker> to <HashSchema <IntegerChecker>>

and arrays like [<IntegerChecker>] to <ArraySchema <IntegerChecker>>


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/datacaster/definition_dsl.rb', line 10

def self.expand(definition)
  case definition
  when Datacaster::Base
    definition
  when Array
    if definition.length != 1
      raise ArgumentError.new("Datacaster: DSL array definitions must have exactly 1 element in the array, e.g. [integer]")
    end
    ArraySchema.new(expand(definition.first))
  when Hash
    HashSchema.new(definition.transform_values { |x| expand(x) })
  else
    return definition if definition.respond_to?(:call)
    raise ArgumentError.new("Datacaster: Unknown definition #{definition.inspect}, which doesn't respond to #call")
  end
end