Class: Consciously::Try

Inherits:
BasicObject
Defined in:
lib/y_support/try.rb

Constant Summary collapse

DECORATE =
-> str, prefix: '', postfix: '' {
  str.to_s.tap { |ς| return ς.empty? ? '' : prefix + ς + postfix }
}
TRANSITIVE =
::Hash.new do |, key| "#{key}ing %s" end
      .update( is: "being %s",
has: "having %s" )
STATE =
::Hash.new do |, key| "#{key} %s" end
      .update( is: "%s",
has: "has %s" )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

This



47
48
49
50
# File 'lib/y_support/try.rb', line 47

def initialize( object: nil, text: nil, &block )
  @__obj__, @__txt__, @__bl__ = object, text, block
  @__facts__ = ::Hash.new do |hsh, key| hsh[key] = [ {} ] end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



89
90
91
# File 'lib/y_support/try.rb', line 89

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

Instance Attribute Details

#__bl__Object (readonly)

Returns the value of attribute __bl__.



44
45
46
# File 'lib/y_support/try.rb', line 44

def __bl__
  @__bl__
end

#__facts__Object (readonly)

Returns the value of attribute __facts__.



44
45
46
# File 'lib/y_support/try.rb', line 44

def __facts__
  @__facts__
end

#__obj__Object (readonly)

Returns the value of attribute __obj__.



44
45
46
# File 'lib/y_support/try.rb', line 44

def __obj__
  @__obj__
end

#__txt__Object (readonly)

Returns the value of attribute __txt__.



44
45
46
# File 'lib/y_support/try.rb', line 44

def __txt__
  @__txt__
end

Instance Method Details

#__circumstances__Object



104
105
106
107
108
109
110
111
112
# File 'lib/y_support/try.rb', line 104

def __circumstances__
  __facts__.reject { |subj, _| subj == __obj__ }.map { |subj, _|
    thing, statements = __describe__( subj )
    thing + DECORATE.( statements.map { |v, o|
                         TRANSITIVE[v] % o
                       }.join( ', ' ),
                       prefix: ' ' )
  }.join( ', ' )
end

#__describe__(obj = __obj__) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/y_support/try.rb', line 93

def __describe__ obj=__obj__
  facts = __facts__[obj].dup
  statements = if facts.last.is_a? ::Hash then facts.pop else {} end
  fs = facts.join ', '
  if statements.empty? then
    return fs, statements
  else
    return facts.empty? ? obj.to_s : fs, statements
  end
end

#__invoke__(*args) ⇒ Object

Invokes the Try object’s block.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/y_support/try.rb', line 68

def __invoke__ *args
  begin
    instance_exec *args, &__bl__
  rescue ::StandardError => err
    txt1 = "When trying #{__txt__}"
    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

The syntax of this method, available inside the #try block, is:

note "Concatenation of Foo and Bar", is: "FooBar", has: "6 letters"


56
57
58
59
60
61
62
63
64
# File 'lib/y_support/try.rb', line 56

def note *subjects, **statements, &block
  return Array( subjects ).each { |s| __facts__[s].push_ordered s } if
    statements.empty?
  subjects << __obj__ if subjects.empty?
  Array( subjects ).each { |subj|
    statements.each { |verb, obj| __facts__[subj].push_named verb => obj }
  }
  return statements.first[1]
end

#try(*args, &block) ⇒ Object



85
86
87
# File 'lib/y_support/try.rb', line 85

def try *args, &block
  __obj__.try *args, &block
end