Sha256: e139ae77267760668e45761e93affd007f50180c6f5c5b8cd3b807476e3b393d

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'pathname'
require 'find'
require 'active_support/inflector'
require 'active_record'
require 'json'

def active_finite sym
  anon = Class.new ActiveRecord::Base
  klass = Object.const_set(sym.to_s.singularize.capitalize, anon)
  klass
end

def create_finite args
  modify_finite args do |vs, klass, column_name|
    vs.each do |v|
      obj = klass.new
      obj.send column_name.to_s + '=', v
      obj.save
    end
  end
end

def drop_finite args
  modify_finite args do |vs, klass, column_name|
    vs.each do |v|
      objs = klass.where column_name => v
      objs.each do |o|
        klass.destroy o
      end
    end
  end
end

def modify_finite args
  table_name = args[:in_table]
  if table_name.nil?
    raise 'A table name must be specified for :in_table .'
  end
  file_name   = args[:from_file] 
  values      = args[:values]
  column_name = args[:column_name] || :value
  if values.nil? and file_name.nil?
    raise 'Either :from_file or :values must be specified.'
  end

  to_be_modified = Array.new
  if not values.nil?
    to_be_modified = to_be_modified.concat values
  end

  if not file_name.nil?
    to_be_modified = to_be_modified.concat JSON.load open file_name
  end

  klass = active_finite table_name
  yield to_be_modified, klass, column_name
end

def default_column_name
  :value
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_finite-0.1.0 lib/active_finite.rb