Class: FastAttributes::TypeCast

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_attributes/type_cast.rb

Defined Under Namespace

Classes: InvalidValueError, UnknownTypeCastingError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ TypeCast

Returns a new instance of TypeCast.



9
10
11
12
13
14
15
# File 'lib/fast_attributes/type_cast.rb', line 9

def initialize(type)
  @type              = type
  @if_conditions     = []
  @else_condition    = %q(raise FastAttributes::TypeCast::UnknownTypeCastingError, 'Type casting is not defined')
  @rescue_conditions = nil
  @default_rescue    = %(raise FastAttributes::TypeCast::InvalidValueError, %(Invalid value "\#{%s}" for attribute "%a" of type "#{@type}"))
end

Class Method Details

.escape_template(template, attribute_name, argument_name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fast_attributes/type_cast.rb', line 18

def escape_template(template, attribute_name, argument_name)
  template.gsub(/%+a|%+s/) do |match|
    match.each_char.each_slice(2).map do |placeholder|
      case placeholder
        when %w[% a] then attribute_name
        when %w[% s] then argument_name
        when %w[% %] then '%'
        else placeholder.join
      end
    end.join
  end
end

Instance Method Details

#compile_method_body(attribute_name, argument_name) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/fast_attributes/type_cast.rb', line 72

def compile_method_body(attribute_name, argument_name)
  method_body = "begin\n" +
                "  #{type_casting_template}\n" +
                "#{rescue_template}\n" +
                "end"

  self.class.escape_template(method_body, attribute_name, argument_name)
end

#from(condition, options = {}) ⇒ Object



32
33
34
# File 'lib/fast_attributes/type_cast.rb', line 32

def from(condition, options = {})
  @if_conditions << [condition, options[:to]]
end

#on_error(error, options = {}) ⇒ Object



40
41
42
43
# File 'lib/fast_attributes/type_cast.rb', line 40

def on_error(error, options = {})
  @rescue_conditions ||=[]
  @rescue_conditions << [error, options[:act]]
end

#otherwise(else_condition) ⇒ Object



36
37
38
# File 'lib/fast_attributes/type_cast.rb', line 36

def otherwise(else_condition)
  @else_condition = else_condition
end

#rescue_templateObject



64
65
66
67
68
69
70
# File 'lib/fast_attributes/type_cast.rb', line 64

def rescue_template
  rescues = @rescue_conditions || [['', @default_rescue]]
  rescues.map do |error, action|
    "rescue #{error} => e\n" +
    "  #{action}"
  end.join("\n")
end

#type_casting_templateObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fast_attributes/type_cast.rb', line 45

def type_casting_template
  @type_casting_template ||= begin
    if @if_conditions.any?
      conditions = @if_conditions.map do |from, to|
        "when #{from}\n" +
        "  #{to}\n"
      end

      "case %s\n" +
      conditions.join +
      "else\n" +
      "  #{@else_condition}\n" +
      "end"
    else
      @else_condition
    end
  end
end