class Bumblebee::SimpleConverter

Subclass of Converter that provides a simple implementation for each Type.

Constants

DEFAULT_BIG_DECIMAL
DEFAULT_DATE

Private Instance Methods

null_or_empty?(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 95
def null_or_empty?(val)
  val.nil? || val.to_s.empty?
end
null_or_empty_default(val, default) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 91
def null_or_empty_default(val, default)
  null_or_empty?(val) ? default : val
end
nully?(val) click to toggle source

rubocop:disable Style/DoubleNegation

# File lib/bumblebee/simple_converter.rb, line 100
def nully?(val)
  null_or_empty?(val) || !!(val.to_s =~ /(nil|null)$/i)
end
process_bigdecimal(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 75
def process_bigdecimal(val)
  return nil if nullable? && null_or_empty?(val)

  BigDecimal(null_or_empty_default(val, DEFAULT_BIG_DECIMAL).to_s)
end
process_boolean(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 81
def process_boolean(val)
  if nullable? && nully?(val)
    nil
  elsif truthy?(val)
    true
  else
    false
  end
end
process_date(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 51
def process_date(val)
  return nil if nullable? && null_or_empty?(val)

  Date.strptime(null_or_empty_default(val, DEFAULT_DATE).to_s, date_format)
end
process_float(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 69
def process_float(val)
  return nil if nullable? && null_or_empty?(val)

  val.to_f
end
process_function(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 45
def process_function(val)
  raise ArgumentError, 'function is required for function type' unless function

  function.call(val)
end
process_ignore(_val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 33
def process_ignore(_val)
  nil
end
process_integer(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 63
def process_integer(val)
  return nil if nullable? && null_or_empty?(val)

  val.to_i
end
process_join(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 37
def process_join(val)
  Array(val).map { |v| per.convert(v) }.join(separator)
end
process_pluck_join(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 18
def process_pluck_join(val)
  raise ArgumentError, 'sub_property is required for a pluck_join' unless sub_property

  Array(val).map { |h| per.convert(resolver.get(h, sub_property)) }
            .join(separator)
end
process_pluck_split(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 25
def process_pluck_split(val)
  raise ArgumentError, 'sub_property is required for a pluck_split' unless sub_property

  process_split(val).map do |v|
    object_class.new.tap { |h| resolver.set(h, sub_property, v) }
  end
end
process_split(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 41
def process_split(val)
  val.to_s.split(separator).map { |v| per.convert(v) }
end
process_string(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 57
def process_string(val)
  return nil if nullable? && null_or_empty?(val)

  val.to_s
end
truthy?(val) click to toggle source
# File lib/bumblebee/simple_converter.rb, line 104
def truthy?(val)
  !!(val.to_s =~ /(true|t|yes|y|1)$/i)
end