module FeideeUtils::Record::Accessors::ClassMethods

Protected Instance Methods

define_indexed_accessors() click to toggle source

NOTE: Here we assume the underlying database schema does not change. The assumption is safe in the sense that it is generally expected to restart and/or recompile your application after updating the schema.

# File lib/feidee_utils/record/accessors.rb, line 52
def define_indexed_accessors
  return if !const_defined? :IndexedAccessorFieldMappings

  self::IndexedAccessorFieldMappings.each do |name, column_name|
    if method_defined? name
      raise "Accessor #{name} already exists in #{self.name}."
    end

    index = self.column_names.index column_name
    if index.nil?
      raise "Cannot find column #{column_name} in #{inspect}."
    end
    define_method name do column_at_index(index) end
  end
end

Private Instance Methods

define_accessors(field_mappings) click to toggle source
# File lib/feidee_utils/record/accessors.rb, line 16
def define_accessors field_mappings
  field_mappings.each do |name, key|
    if method_defined? name
      raise "Accessor #{name} already exists in #{self.name}."
    end
    define_method name do column(key) end
  end
end
define_entity_accessor(poid_callback_name, target_class_name = nil) click to toggle source
# File lib/feidee_utils/record/accessors.rb, line 25
def define_entity_accessor poid_callback_name, target_class_name = nil
  accessor_name = poid_callback_name.to_s.chomp!("_poid")
  if accessor_name == nil
    raise "No trailing 'poid' in callback name #{poid_callback_name}."
  end

  if not target_class_name
    target_class_name = accessor_name
  end
  target_class_name = target_class_name.to_s.clone
  target_class_name.gsub!(/(^|_)(.)/) { $2.upcase }

  define_method accessor_name do
    poid = method(poid_callback_name).call
    self.class.environment.const_get(target_class_name).find_by_id(poid)
  end
end
register_indexed_accessors(field_mappings) click to toggle source
# File lib/feidee_utils/record/accessors.rb, line 43
def register_indexed_accessors field_mappings
  # The indexes of those columns are unknown until we see the schema.
  const_set :IndexedAccessorFieldMappings, field_mappings
end