Class: Cuttlebone::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/cuttlebone/definition.rb

Defined Under Namespace

Modules: DSL Classes: Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_or_class, options = {}, &definition) ⇒ Definition

Returns a new instance of Definition.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
# File 'lib/cuttlebone/definition.rb', line 33

def initialize object_or_class, options={}, &definition
  raise ArgumentError, 'missing block' unless block_given?
  @object_or_class = object_or_class
  @options         = options
  @commands        = [] #Array.new(proc { |c| [:self, c, '', nil] })
  @prompt          = proc { |c| '' }

  @parser = Parser.new(self)
  @parser.instance_eval(&definition)
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



31
32
33
# File 'lib/cuttlebone/definition.rb', line 31

def commands
  @commands
end

#promptObject

Returns the value of attribute prompt.



31
32
33
# File 'lib/cuttlebone/definition.rb', line 31

def prompt
  @prompt
end

Instance Method Details

#match(object) ⇒ Object



46
47
48
49
# File 'lib/cuttlebone/definition.rb', line 46

def match object
  @object_or_class === object
  # TODO :if :unless options
end

#proc_for(command) ⇒ Object



51
52
53
54
55
# File 'lib/cuttlebone/definition.rb', line 51

def proc_for command
  string_or_regexp, block, description = commands.find { |(sr,b,d)| sr === command } || raise(UnknownCommandError, "Unknown command: #{command.inspect}!")
  return([ block, $~.captures ]) if $~
  return([ block, [] ])
end