Module: DCA::Models::BinderHelper::ClassMethods

Defined in:
lib/dca/models/binder_helper.rb

Instance Method Summary collapse

Instance Method Details

#convert(value, type) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dca/models/binder_helper.rb', line 7

def convert value, type
  case type
    when :integer
      value.to_s.gsub(/[^\d.,]/,'').gsub(/,/,'.').to_i
    when :float
      value.to_s.gsub(/[^\d.,]/,'').gsub(/,/,'.').to_f
    when :string
      value.to_s.strip
    when :symbol
      value.to_s.to_sym
    when :datetime
      DateTime.parse(value).to_time.utc unless value.nil?
    else
      value
  end
end

#find_type(object, field, polymorphic = nil) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/dca/models/binder_helper.rb', line 37

def find_type object, field, polymorphic = nil
  type_name = field.to_s.singularize.camelize
  type_name = "#{object.send(polymorphic).to_s.camelize}#{type_name}" if polymorphic
  type = type_name.safe_constantize
  type = "#{object.class.to_s.deconstantize}::#{type_name}".constantize if type.nil?

  type
end

#parse_options(object, value, options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dca/models/binder_helper.rb', line 24

def parse_options object, value, options
  result = value
  if result.nil?
    result = options[:default]  unless options[:default].nil?
  else
    result = value[options[:regex], 1] unless options[:regex].nil?
  end

  result = object.send(options[:parser], result) unless options[:parser].nil?

  result
end