Class: CapriccIo::Message

Inherits:
CObject
  • Object
show all
Defined in:
lib/capriccio/runtime/message.rb

Instance Attribute Summary collapse

Attributes inherited from CObject

#proto, #slots, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CObject

#[], #[]=, #clone, #def

Constructor Details

#initialize(name, line) ⇒ Message

Returns a new instance of Message.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/capriccio/runtime/message.rb', line 10

def initialize(name, line)
  @name = name
  @args = []
  @line = line
  @cached_value = case @name
                  when /^\d+/
                    Runtime['Number'].clone(@name.to_i)
                  when /^"(.*)"$/
                    Runtime['String'].clone(Regexp.last_match(1))
                  end

  @terminator = %W[. \n].include?(@name)
  super(Runtime['Message'])
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



8
9
10
# File 'lib/capriccio/runtime/message.rb', line 8

def args
  @args
end

#cached_valueObject

Returns the value of attribute cached_value.



8
9
10
# File 'lib/capriccio/runtime/message.rb', line 8

def cached_value
  @cached_value
end

#lineObject

Returns the value of attribute line.



8
9
10
# File 'lib/capriccio/runtime/message.rb', line 8

def line
  @line
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/capriccio/runtime/message.rb', line 8

def name
  @name
end

#nextObject

Returns the value of attribute next.



8
9
10
# File 'lib/capriccio/runtime/message.rb', line 8

def next
  @next
end

Class Method Details

.parse(code) ⇒ Object



51
52
53
# File 'lib/capriccio/runtime/message.rb', line 51

def self.parse(code)
  Parser.parse(code, 1).last
end

Instance Method Details

#call(receiver, context = receiver, *_args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/capriccio/runtime/message.rb', line 25

def call(receiver, context = receiver, *_args)
  value = if @terminator
            context
          elsif @cached_value
            @cached_value
          else
            receiver[name].call(receiver, context, *@args)
          end

  return @next.call(value, context) if @next

  value
rescue CapriccIo::Error => e
  e.current_message ||= self
  raise
end

#to_s(level = 0) ⇒ Object Also known as: inspect



42
43
44
45
46
47
48
# File 'lib/capriccio/runtime/message.rb', line 42

def to_s(level = 0)
  s = '  ' * level
  s << "<Message @name=#{@name}"
  s << (", @args=#{@args.inspect}") unless @args.empty?
  s << (", @next=\n#{@next.to_s(level + 1)}") if @next
  "#{s}>"
end