Class: Clomp::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/clomp/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options: {}, tracks: [], operation: nil) ⇒ Result

Returns a new instance of Result.



5
6
7
8
9
10
11
12
# File 'lib/clomp/result.rb', line 5

def initialize(options: {}, tracks: [], operation: nil)
  @report         = {}
  @operation      = set_prop :operation, operation || Operation.new
  @tracks         = set_prop :tracks, tracks || []
  @options        = {}
  @immutable_data = set_prop :options, options
  @state          = ->(tracks) {tracks.select {|track| track.failure?}.count.zero?}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/clomp/result.rb', line 38

def method_missing(method, *args)
  if @operation.respond_to?(method)
    @operation.send(method, *args)
  else
    super
  end
end

Instance Attribute Details

#operationObject (readonly)

Returns the value of attribute operation.



3
4
5
# File 'lib/clomp/result.rb', line 3

def operation
  @operation
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/clomp/result.rb', line 3

def options
  @options
end

#stateObject (readonly)

Returns the value of attribute state.



3
4
5
# File 'lib/clomp/result.rb', line 3

def state
  @state
end

Instance Method Details

#[](key) ⇒ Object



46
47
48
49
50
# File 'lib/clomp/result.rb', line 46

def [](key)
  sym_key = to_sym_key(key)
  
  self.instance_variable_get(sym_key)
end

#[]=(key, value) ⇒ Object



52
53
54
55
# File 'lib/clomp/result.rb', line 52

def []=(key, value)
  sym_key = to_sym_key(key)
  self.instance_variable_set(sym_key, value)
end

#dataObject



14
15
16
# File 'lib/clomp/result.rb', line 14

def data
  options[:mutable_data]
end

#failure?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/clomp/result.rb', line 26

def failure?
  @state.(self[:tracks]) === false
end

#immutable_dataObject



18
19
20
# File 'lib/clomp/result.rb', line 18

def immutable_data
  options[:immutable_data]
end

#set_prop(key, value) ⇒ Object



57
58
59
60
61
# File 'lib/clomp/result.rb', line 57

def set_prop(key, value)
  sym_key = to_sym_key(key)
  
  self.instance_variable_set(sym_key, value)
end

#state_statementObject



30
31
32
# File 'lib/clomp/result.rb', line 30

def state_statement
  success? ? 'Successful' : 'Failure'
end

#success?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/clomp/result.rb', line 22

def success?
  @state.(self[:tracks]) === true
end

#to_sObject



34
35
36
# File 'lib/clomp/result.rb', line 34

def to_s
  "#{self.class.name} > #{state_statement}: #{executed_tracks}"
end