class Typist::Data
Instances of this class may be included like a module.
Attributes
block[R]
constructors[R]
data_funcs[R]
funcs[R]
name[R]
Public Class Methods
new(name, &block)
click to toggle source
Create a new data type with the given name. The block will be evaluated in the context of the new instance.
# File lib/typist/data.rb, line 7 def initialize(name, &block) @name = name @constructors = [] @funcs = [] @data_funcs = [] @block = block end
Public Instance Methods
constructor(*args, &block)
click to toggle source
Define a constructor for this data type.
# File lib/typist/data.rb, line 16 def constructor(*args, &block) constructors << Typist::Constructor.new(*args, &block) end
data_func(*args, &block)
click to toggle source
Define a function whose receiver is the data type.
# File lib/typist/data.rb, line 26 def data_func(*args, &block) data_funcs << Typist::DataFunc.new(*args, &block) end
define!(mod = Kernel)
click to toggle source
Define the module, constructors, and functions.
# File lib/typist/data.rb, line 36 def define!(mod = Kernel) get_module.tap do |context| mod.const_set(name, context) instance_eval(&block) unless block.nil? constructors.each { |constructor| constructor.define!(context) } funcs.each { |func| func.define!(context) } data_funcs.each { |func| func.define!(context) } end end
func(*args, &block)
click to toggle source
Define a function which may pattern-matched against
# File lib/typist/data.rb, line 21 def func(*args, &block) funcs << Typist::Func.new(*args, &block) end
get_module()
click to toggle source
Get the module that is defined.
# File lib/typist/data.rb, line 31 def get_module @module ||= Module.new end