Module: Compel::Builder::Common

Included in:
Schema
Defined in:
lib/compel/builder/common.rb

Instance Method Summary collapse

Instance Method Details

#coerce_if_proc(proc) ⇒ Object

this is lovely, refactor later



38
39
40
41
42
43
44
45
46
# File 'lib/compel/builder/common.rb', line 38

def coerce_if_proc(proc)
  if proc && proc.is_a?(Proc) &&
    (proc.arity == 1 || proc.arity == 0 &&
      (proc.call.is_a?(::Symbol) || proc.call.is_a?(::String)))
    proc
  else
    fail
  end
end

#default(value, options = {}) ⇒ Object



14
15
16
# File 'lib/compel/builder/common.rb', line 14

def default(value, options = {})
  build_option :default, Coercion.coerce!(value, self.type), options
end

#if(lambda = nil, options = {}, &block) ⇒ Object



30
31
32
33
34
35
# File 'lib/compel/builder/common.rb', line 30

def if(lambda = nil, options = {}, &block)
  build_option :if, coerce_if_proc(lambda || block), options

  rescue
    raise Compel::TypeError, 'invalid proc for if'
end

#is(value, options = {}) ⇒ Object



6
7
8
# File 'lib/compel/builder/common.rb', line 6

def is(value, options = {})
  build_option :is, Coercion.coerce!(value, self.type), options
end

#length(value, options = {}) ⇒ Object



18
19
20
# File 'lib/compel/builder/common.rb', line 18

def length(value, options = {})
  build_option :length, Coercion.coerce!(value, Coercion::Integer), options
end

#max_length(value, options = {}) ⇒ Object



26
27
28
# File 'lib/compel/builder/common.rb', line 26

def max_length(value, options = {})
  build_option :max_length, Coercion.coerce!(value, Coercion::Integer), options
end

#min_length(value, options = {}) ⇒ Object



22
23
24
# File 'lib/compel/builder/common.rb', line 22

def min_length(value, options = {})
  build_option :min_length, Coercion.coerce!(value, Coercion::Integer), options
end

#required(options = {}) ⇒ Object



10
11
12
# File 'lib/compel/builder/common.rb', line 10

def required(options = {})
  build_option :required, true, options
end