module Dynamini::TypeHandler

Constants

GETTER_PROCS
SETTER_PROCS

Public Instance Methods

attribute_callback(procs, handle, value, validate) click to toggle source
# File lib/dynamini/type_handler.rb, line 95
def attribute_callback(procs, handle, value, validate)
  value = handle[:options][:default] if value.nil?
  callback = procs[handle[:format]]
  if should_convert_elements?(handle, value)
    result = convert_elements(value, procs[handle[:options][:of]])
    callback.call(result)
  elsif validate && invalid_enumerable_value?(handle, value)
    raise ArgumentError, "Can't write a non-enumerable value to field handled as #{handle[:format]}"
  else
    callback.call(value)
  end
end
convert_elements(enumerable, callback) click to toggle source
# File lib/dynamini/type_handler.rb, line 116
def convert_elements(enumerable, callback)
  enumerable.map { |e| callback.call(e) }
end
define_handled_getter(column, format_class, _options = {}) click to toggle source
# File lib/dynamini/type_handler.rb, line 52
def define_handled_getter(column, format_class, _options = {})
  proc = GETTER_PROCS[format_class]
  fail 'Unsupported data type: ' + format_class.to_s if proc.nil?

  define_method(column) do
    read_attribute(column)
  end
end
define_handled_setter(column, format_class) click to toggle source
# File lib/dynamini/type_handler.rb, line 61
def define_handled_setter(column, format_class)
  method_name = (column.to_s + '=')
  proc = SETTER_PROCS[format_class]
  fail 'Unsupported data type: ' + format_class.to_s if proc.nil?
  define_method(method_name) do |value|
    write_attribute(column, value)
  end
end
format_default(format_class) click to toggle source
# File lib/dynamini/type_handler.rb, line 70
def format_default(format_class)
  case format_class
    when :array
      []
    when :set
      Set.new
  end
end
handle(column, format_class, options = {}) click to toggle source
# File lib/dynamini/type_handler.rb, line 40
def handle(column, format_class, options = {})
  validate_handle(format_class, options)

  options[:default] ||= format_default(format_class)
  options[:default] ||= Set.new if format_class == :set

  self.handles = self.handles.merge(column => { format: format_class, options: options })

  define_handled_getter(column, format_class, options)
  define_handled_setter(column, format_class)
end
handled_as?(handle, type) click to toggle source
# File lib/dynamini/type_handler.rb, line 120
def handled_as?(handle, type)
  type.include? handle[:format]
end
handled_key(column, value) click to toggle source
# File lib/dynamini/type_handler.rb, line 87
def handled_key(column, value)
  if handles[column]
    attribute_callback(GETTER_PROCS, handles[column], value, false)
  else
    value
  end
end
invalid_enumerable_value?(handle, value) click to toggle source
# File lib/dynamini/type_handler.rb, line 112
def invalid_enumerable_value?(handle, value)
  handled_as?(handle, [:array, :set]) && !value.is_a?(Enumerable)
end
should_convert_elements?(handle, value) click to toggle source
# File lib/dynamini/type_handler.rb, line 108
def should_convert_elements?(handle, value)
  handle[:options][:of] && (value.is_a?(Array) || value.is_a?(Set))
end
validate_handle(format, options) click to toggle source
# File lib/dynamini/type_handler.rb, line 79
def validate_handle(format, options)
  if format == :set
    if options[:of] && [:set, :array].include?(options[:of])
      raise ArgumentError, 'Invalid handle: cannot store non-primitive datatypes within a set.'
    end
  end
end