Class: Conscience::Try

Inherits:
Object show all
Defined in:
lib/y_support/conscience.rb

Constant Summary collapse

PUSH_ORDERED =
-> ary, e {
  named = ary.extract_options!
  ary << e << named
}
PUSH_NAMED =
-> ary, k, v {
  named = ary.extract_options!
  ary << named.update( k => v )
}
DECORATE =
-> str, prefix: '', postfix: '' {
  str.to_s.tap { |
TRANSITIVE =
Hash.new do |
STATE =
Hash.new do |

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object: nil, text: nil, &block) ⇒ Try

Returns a new instance of Try.



59
60
61
62
# File 'lib/y_support/conscience.rb', line 59

def initialize( object: nil, text: nil, &block )
  @_object_, @_text_, @_block_ = object, text, block
  @_facts_ = Hash.new do |

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



94
95
96
# File 'lib/y_support/conscience.rb', line 94

def method_missing sym, *args
  _object_.send sym, *args
end

Instance Attribute Details

#_block_Object (readonly)

Returns the value of attribute block.



57
58
59
# File 'lib/y_support/conscience.rb', line 57

def _block_
  @_block_
end

#_facts_Object (readonly)

Returns the value of attribute facts.



57
58
59
# File 'lib/y_support/conscience.rb', line 57

def _facts_
  @_facts_
end

#_object_Object (readonly)

Returns the value of attribute object.



57
58
59
# File 'lib/y_support/conscience.rb', line 57

def _object_
  @_object_
end

#_text_Object (readonly)

Returns the value of attribute text.



57
58
59
# File 'lib/y_support/conscience.rb', line 57

def _text_
  @_text_
end

Instance Method Details

#call(*args) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/y_support/conscience.rb', line 77

def call *args
  begin
    instance_exec *args, &_block_
  rescue StandardError => err
    txt1 = "When trying #{_text_}"
    thing, statements = _describe_
    txt2 = DECORATE.( thing, prefix: ' ' )
    txt3 = DECORATE.( statements.map { |verb, object|
                        STATE[verb] % object
                      }.join( ', ' ),
                      prefix: ' (', postfix: ')' )
    txt4 = DECORATE.( _circumstances_, prefix: ', ' )
    txt5 = DECORATE.( "#{err.class} occurred: #{err}", prefix: ', ' )
    raise err, txt1 + txt2 + txt3 + txt4 + txt5
  end
end

#note(*subjects, **statements, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/y_support/conscience.rb', line 64

def note *subjects, **statements, &block
  return *Array( subjects ).each do |s|
    PUSH_ORDERED.( _facts_[s], s )
  end if statements.empty?
  subjects << _object_ if subjects.empty?
  Array( subjects ).each do |subj|
    statements.each do |verb, object|
      PUSH_NAMED.( _facts_[subj], verb, object )
    end
  end
  return statements.first[1]
end