Class: Speq::Expression

Inherits:
Group
  • Object
show all
Defined in:
lib/speq.rb,
lib/speq/string_fmt.rb

Overview

An expression group carries context for evaluation and associated questions

Instance Attribute Summary collapse

Attributes inherited from Group

#context, #parent, #units

Instance Method Summary collapse

Methods inherited from Group

#<<, #error?, #fail?, #full_context, #newline, #outcome, #pass?, #report

Constructor Details

#initialize(parent) ⇒ Expression

Returns a new instance of Expression.



104
105
106
107
# File 'lib/speq.rb', line 104

def initialize(parent)
  super(parent)
  @result = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/speq.rb', line 148

def method_missing(method_name, *args, &block)
  if Question.question?(method_name)
    if result.nil?
      parent << self
      evaluate_result
    end
    self << Question.for(result.value, method_name, *args, &block)
    self
  else
    super
  end
end

Instance Attribute Details

#resultObject

Returns the value of attribute result.



102
103
104
# File 'lib/speq.rb', line 102

def result
  @result
end

Instance Method Details

#does(val, description = nil, &block) ⇒ Object



127
128
129
130
131
# File 'lib/speq.rb', line 127

def does(val, description = nil, &block)
  context.message = Message.new(val, description)
  speq &block if block_given?
  self
end

#evaluate_resultObject



113
114
115
# File 'lib/speq.rb', line 113

def evaluate_result
  self.result = full_context.evaluate
end

#indentObject



37
38
39
# File 'lib/speq/string_fmt.rb', line 37

def indent
  parent.indent
end

#is(thing, description = nil, &block) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/speq.rb', line 140

def is(thing, description = nil, &block)
  if thing.is_a?(Symbol)
    does(thing, description, &block)
  else
    on(thing, description, &block)
  end
end

#on(val, description = nil, &block) ⇒ Object



121
122
123
124
125
# File 'lib/speq.rb', line 121

def on(val, description = nil, &block)
  context.subject = Subject.new(val, description)
  speq &block if block_given?
  self
end

#respond_to_missing?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/speq.rb', line 161

def respond_to_missing?(method_name, *)
  Question.question?(method_name) || super
end

#speq(description = "#{context}...", &block) ⇒ Object



117
118
119
# File 'lib/speq.rb', line 117

def speq(description = "#{context}...", &block)
  parent << Test.new(description, self, &block)
end

#to_sObject



41
42
43
# File 'lib/speq/string_fmt.rb', line 41

def to_s
  "#{newline}  #{outcome} #{context} #{units.join('; ')}."
end

#with(*args, &block) ⇒ Object Also known as: of



133
134
135
136
# File 'lib/speq.rb', line 133

def with(*args, &block)
  context.arguments = Arguments.new(*args, &block)
  self
end