Module: Compel::Builder::CommonValue

Included in:
Date, DateTime, Float, Integer, String, Time
Defined in:
lib/compel/builder/common_value.rb

Instance Method Summary collapse

Instance Method Details

#coerce_value!(value, method) ⇒ Object



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

def coerce_value!(value, method)
  begin
    fail if value.nil?

    Coercion.coerce!(value, self.type)
  rescue
    raise_value_error(method)
  end
end

#coerce_values_ary!(values, method) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/compel/builder/common_value.rb', line 22

def coerce_values_ary!(values, method)
  begin
    fail if values.nil?

    Coercion.coerce!(values, Coercion::Array)
  rescue
    raise_array_error(method)
  end

  values.map{ |value| Coercion.coerce!(value, self.type) }

  rescue
    raise_array_values_error(method)
end

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



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

def in(value, options = {})
  build_option :in, coerce_values_ary!(value, :in), options
end

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



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

def max(value, options = {})
  build_option :max, coerce_value!(value, :max), options
end

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



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

def min(value, options = {})
  build_option :min, coerce_value!(value, :min), options
end

#raise_array_error(method) ⇒ Object

Raises:



47
48
49
50
# File 'lib/compel/builder/common_value.rb', line 47

def raise_array_error(method)
  raise TypeError, "#{self.class.human_name} ##{method} " \
                   "value must an Array"
end

#raise_array_values_error(method) ⇒ Object

Raises:



52
53
54
55
# File 'lib/compel/builder/common_value.rb', line 52

def raise_array_values_error(method)
  raise TypeError, "All #{self.class.human_name} ##{method} values " \
                   "must be a valid #{self.type.human_name}"
end

#raise_value_error(method) ⇒ Object

Raises:



57
58
59
60
# File 'lib/compel/builder/common_value.rb', line 57

def raise_value_error(method)
  raise TypeError, "#{self.class.human_name} ##{method} value " \
                   "must be a valid #{self.type.human_name}"
end

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



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

def range(value, options = {})
  build_option :range, coerce_values_ary!(value, :range), options
end