Skip to content

Instantly share code, notes, and snippets.

@chad
Created March 21, 2009 20:22
Show Gist options
  • Save chad/82969 to your computer and use it in GitHub Desktop.
Save chad/82969 to your computer and use it in GitHub Desktop.
require 'pathname'
class FileDB
class ModuleDefiner
class ClassDefiner
end
end
class << self
def from_dir(path)
with_module_for(module_name_for(path)) do |m|
Dir["#{path}/*.schema"].each do |schema_file|
m.const_set(class_name_for(schema_file), class_for(schema_file))
end
end
end
def module_name_for(file_path)
camel(File.basename(file_path))
end
def with_module_for(module_name)
m = Object.const_get(module_name) rescue Object.const_set(module_name, Module.new)
yield m
m
end
def class_name_for(file_path)
camel(Pathname.new(file_path).basename.sub(/\.schema$/, ''))
end
def camel(name)
name.to_s.split(/_/).map{|component| component.capitalize}.join
end
def class_for(file_path)
Class.new do
definition = IO.readlines(file_path)
definition.map{|line| line.chomp.split(/:/)}.each do |name, type|
p "Doing #{name}"
instance_variable_name = "@#{name}"
define_method(name) do
instance_variable_get(instance_variable_name)
end
define_method("#{name}=") do |value|
instance_variable_set(instance_variable_name, value)
end
end
end
end
end
end
if __FILE__ == $0
m = FileDB.from_dir(File.join(File.dirname(__FILE__), 'schema'))
p m
p = Schema::Person.new
p.name = "Chad"
p p
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment