Class: Typist::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/typist/data.rb

Overview

Instances of this class may be included like a module.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Data

Create a new data type with the given name. The block will be evaluated in the context of the new instance.



7
8
9
10
11
12
13
# File 'lib/typist/data.rb', line 7

def initialize(name, &block)
  @name = name
  @constructors = []
  @funcs = []
  @data_funcs = []
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



3
4
5
# File 'lib/typist/data.rb', line 3

def block
  @block
end

#constructorsObject (readonly)

Returns the value of attribute constructors.



3
4
5
# File 'lib/typist/data.rb', line 3

def constructors
  @constructors
end

#data_funcsObject (readonly)

Returns the value of attribute data_funcs.



3
4
5
# File 'lib/typist/data.rb', line 3

def data_funcs
  @data_funcs
end

#funcsObject (readonly)

Returns the value of attribute funcs.



3
4
5
# File 'lib/typist/data.rb', line 3

def funcs
  @funcs
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/typist/data.rb', line 3

def name
  @name
end

Instance Method Details

#constructor(*args, &block) ⇒ Object

Define a constructor for this data type.



16
17
18
# File 'lib/typist/data.rb', line 16

def constructor(*args, &block)
  constructors << Typist::Constructor.new(*args, &block)
end

#data_func(*args, &block) ⇒ Object

Define a function whose receiver is the data type.



26
27
28
# File 'lib/typist/data.rb', line 26

def data_func(*args, &block)
  data_funcs << Typist::DataFunc.new(*args, &block)
end

#define!(mod = Kernel) ⇒ Object

Define the module, constructors, and functions.



36
37
38
39
40
41
42
43
44
# 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) ⇒ Object

Define a function which may pattern-matched against



21
22
23
# File 'lib/typist/data.rb', line 21

def func(*args, &block)
  funcs << Typist::Func.new(*args, &block)
end

#get_moduleObject

Get the module that is defined.



31
32
33
# File 'lib/typist/data.rb', line 31

def get_module
  @module ||= Module.new
end