Skip to content

Instantly share code, notes, and snippets.

@tizoc
Created October 24, 2010 01:50
Show Gist options
  • Save tizoc/642962 to your computer and use it in GitHub Desktop.
Save tizoc/642962 to your computer and use it in GitHub Desktop.
require 'set'
module Fiasco
class Table < BasicObject
def inspect
"<Table>"
end
def method_missing(method, *args)
attr = method.to_s
attr = attr.chop if attr[-1] == '='
::Fiasco::Table.send(:attr_accessor, attr)
__send__(method, *args)
end
end
class TableInspectable < BasicObject
def initialize
@__values__ = ::Set.new
end
def inspect
values = @__values__.inject([]) do |acc, v|
acc << "#{v}=#{__send__(v).inspect}"
end.join(' ')
"<Fiasco::Context #{values}>"
end
def method_missing(method, *args)
attr = method.to_s
attr = attr.chop if attr[-1] == '='
::Fiasco::TableInspectable.send(:attr_reader, attr)
::Fiasco::TableInspectable.module_eval <<-EOS
def #{attr}=(value)
@__values__.add(:#{attr})
@#{attr} = value
end
EOS
__send__(method, *args)
end
end
class TableStrict < BasicObject
def initialize
@__values__ = ::Set.new
end
def inspect
values = @__values__.inject([]) do |acc, v|
acc << "#{v}=#{__send__(v).inspect}"
end.join(' ')
"<Fiasco::Context #{values}>"
end
def method_missing(method, *args)
attr = method.to_s
attr = attr.chop if attr[-1] == '='
::Fiasco::TableStrict.module_eval <<-EOS
def #{attr}
if @__values__.include? :#{attr}
@#{attr}
else
::Kernel.raise(::NoMethodError, '#{attr}')
end
end
EOS
::Fiasco::TableStrict.module_eval <<-EOS
def #{attr}=(value)
@__values__.add(:#{attr})
@#{attr} = value
end
EOS
__send__(method, *args)
end
end
end
class LTable < BasicObject
def inspect
"<LTable>"
end
def method_missing(meth, *args)
att = meth.to_s
att = att.chop if att[-1] == '='
::LTable.send(:attr_accessor, att)
__send__(meth, *args)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment