Sha256: b4ba0d5ed9481fc7a9f2a859d38fff294a625bb2ed6bd4c081c5e56e43885911

Contents?: true

Size: 937 Bytes

Versions: 2

Compression:

Stored size: 937 Bytes

Contents

require 'external_includes'
require_relative 'as_class_name.rb'
require_relative 'get_finite.rb'

def get_finite sym, *args
  class_name = as_class_name sym
  class_not_defined = not(Object.const_defined? class_name)
  should_force = args.any? {|x| x == :force}
  if class_not_defined or should_force
    anon = Class.new ActiveRecord::Base
    Object.const_set class_name, anon
  else
    anon = Object.const_get class_name
  end
  if anon.respond_to? :table_exists? and anon.table_exists?
    anon.class_eval do
      method_name = anon.send(:new).attributes.keys[0]
      define_method(:get_value) do
        self.send method_name
      end
      define_method(:set_with_existing_value) do |value|
        result = anon.send 'find_by_' + method_name + '!', value 
        self.send anon.primary_key + '=', result.send(anon.primary_key)
        self.send method_name + '=', result.send(method_name)
      end
    end
  end
  anon
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_finite-0.4.2 lib/get_finite.rb
active_finite-0.4.1 lib/get_finite.rb