Module: Believer::Values

Included in:
Column
Defined in:
lib/believer/values.rb

Instance Method Summary collapse

Instance Method Details

#convert_collection_elements(col, element_type = nil) ⇒ Object



73
74
75
76
77
# File 'lib/believer/values.rb', line 73

def convert_collection_elements(col, element_type = nil)
  return col if element_type.nil?
  meth = convert_method(element_type)
  col.map {|el| send(meth, el)}
end

#convert_method(value_type) ⇒ Object



16
17
18
19
# File 'lib/believer/values.rb', line 16

def convert_method(value_type)
  return nil if value_type.nil?
  "convert_to_#{value_type}".to_sym
end

#convert_to_array(v, element_type = nil) ⇒ Object



61
62
63
64
65
# File 'lib/believer/values.rb', line 61

def convert_to_array(v, element_type = nil)
  return nil if v.nil?
  arr = v.is_a?(Array) ? v : Array.new(v)
  convert_collection_elements(arr, element_type)
end

#convert_to_boolean(v) ⇒ Object



36
37
38
39
40
41
# File 'lib/believer/values.rb', line 36

def convert_to_boolean(v)
  return true if v.is_a?(TrueClass)
  return false if v.is_a?(FalseClass)
  return v.to_bool if v.respond_to?(:to_bool)
  nil
end

#convert_to_counter(v) ⇒ Object



55
56
57
58
59
# File 'lib/believer/values.rb', line 55

def convert_to_counter(v)
  return nil if v.nil?
  return v if v.is_a?(Counter)
  Counter.new(v.to_i)
end

#convert_to_float(v) ⇒ Object



31
32
33
34
# File 'lib/believer/values.rb', line 31

def convert_to_float(v)
  return v.to_f unless v.nil?
  nil
end

#convert_to_hash(v, key_type = nil, value_type = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/believer/values.rb', line 79

def convert_to_hash(v, key_type = nil, value_type = nil)
  return nil if v.nil?
  hash = v.is_a?(Hash) ? v : Hash.new(v)
  unless key_type.nil? && value_type.nil?
    key_conv_meth = convert_method(key_type)
    val_conv_meth = convert_method(value_type)
    hash_conv = {}
    hash.each do |key, value|
      key_conv = key_conv_meth.nil? ? key : send(key_conv_meth, key)
      value_conv = val_conv_meth.nil? ? value : send(val_conv_meth, value)
      hash_conv[key_conv] = value_conv
    end
    return hash_conv
  end
  hash
end

#convert_to_integer(v) ⇒ Object



26
27
28
29
# File 'lib/believer/values.rb', line 26

def convert_to_integer(v)
  return v.to_i unless v.nil?
  nil
end

#convert_to_set(v, element_type = nil) ⇒ Object



67
68
69
70
71
# File 'lib/believer/values.rb', line 67

def convert_to_set(v, element_type = nil)
  return nil if v.nil?
  s = v.is_a?(Set) ? v : Set.new(v)
  Set.new(convert_collection_elements(s, element_type))
end

#convert_to_string(v) ⇒ Object



21
22
23
24
# File 'lib/believer/values.rb', line 21

def convert_to_string(v)
  return v.to_s unless v.nil?
  nil
end

#convert_to_symbol(v) ⇒ Object



50
51
52
53
# File 'lib/believer/values.rb', line 50

def convert_to_symbol(v)
  return nil if v.nil?
  return v.to_sym
end

#convert_to_time(v) ⇒ Object



43
44
45
46
47
48
# File 'lib/believer/values.rb', line 43

def convert_to_time(v)
  return nil if v.nil?
  return v if v.is_a?(Time)
  return Time.parse(v) if v.is_a?(String)
  Time.at(v.to_i)
end

#convert_value_to_type(v, value_type) ⇒ Object

Converts the value to a one that conforms to the type of this column

Parameters:

  • v (Object)

    the value

  • value_type (Symbol)

    value type



10
11
12
13
14
# File 'lib/believer/values.rb', line 10

def convert_value_to_type(v, value_type)
  meth = convert_method(value_type)
  return send(meth, v) if respond_to?(meth)
  v
end