y_support 2.0.3 → 2.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/y_support/conscience.rb +132 -0
- data/lib/y_support/name_magic.rb +18 -15
- data/lib/y_support/version.rb +1 -1
- data/test/conscience_test.rb +75 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e5d5e63041346d0129e1ddbd75122c7e878cf92
|
4
|
+
data.tar.gz: b7b2e14abfcac7f070cb3b4a0a264c2086f73bd1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 771724a6d0d9c63655ef996443d1861f3c0cb59d94516393f3d7689669c860f6889fa4bf0cdb1673dda77fdfb6bb2498483cdce8b39e97db55ca8727c827c4e1
|
7
|
+
data.tar.gz: 0f4502be79ac39d5415ee965dab65419b58cb5b6dad5d76f646bc1037ba9ae01a65e9c0ad615d9bf584683e70ceb0c7ee21ad939d34842b0b436ad977759eb22
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'y_support'
|
3
|
+
require 'active_support/core_ext/array/extract_options'
|
4
|
+
|
5
|
+
# FIXME: Now, here, in order for the token game to work properly with
|
6
|
+
# guarding system, several things have to be done. Firstly,
|
7
|
+
# Place#set_marking( marking, blame ) will have to be implemented,
|
8
|
+
# and in Place#add, blame argument will have to be added
|
9
|
+
# Now with that blame, the transition will have to explain what it is
|
10
|
+
# trying to do. So actually it should be a block
|
11
|
+
#
|
12
|
+
# It's gonna be a mixin, Conscience, or something like that, and it will
|
13
|
+
# imbue the receiving class with the ability to try something.
|
14
|
+
#
|
15
|
+
# Transition.try "to fire!" do
|
16
|
+
# if assignment_action? then
|
17
|
+
# note has: "assignment action"
|
18
|
+
# note "Δt", is: Δt
|
19
|
+
# note "domain marking" is: domain_marking
|
20
|
+
# act = note "Action" do Array action( Δt ) end
|
21
|
+
# codomain.each_with_index do |place, i|
|
22
|
+
# try "set the marking of place #{place} to the #{i}-th element of the action vector (#{act[i]})" do
|
23
|
+
# place.marking = act[i]
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
# else
|
27
|
+
# etc. etc. etc.
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# Should produce error messages like this:
|
31
|
+
# When trying to fire! the transition #{self}, having assignment action, with Δt being #{Δt}, action has
|
32
|
+
# been computed to #{Array action(Δt)}, when trying to set the marking of plac #{place} to the #{i}-th
|
33
|
+
# element of the action vector (#{act[i]}), GuardError was encountered, with the message: ..."
|
34
|
+
#
|
35
|
+
# Yes, this is a managed code miniframework that I need here.
|
36
|
+
#
|
37
|
+
module Conscience
|
38
|
+
class Try
|
39
|
+
PUSH_ORDERED = -> ary, e {
|
40
|
+
named = ary.extract_options!
|
41
|
+
ary << e << named
|
42
|
+
}
|
43
|
+
PUSH_NAMED = -> ary, k, v {
|
44
|
+
named = ary.extract_options!
|
45
|
+
ary << named.update( k => v )
|
46
|
+
}
|
47
|
+
DECORATE = -> str, prefix: '', postfix: '' {
|
48
|
+
str.to_s.tap { |ς| return ς.empty? ? '' : prefix + ς + postfix }
|
49
|
+
}
|
50
|
+
TRANSITIVE = Hash.new do |ꜧ, key| "#{key}ing %s" end
|
51
|
+
.update( is: "being %s",
|
52
|
+
has: "having %s" )
|
53
|
+
STATE = Hash.new do |ꜧ, key| "#{key} %s" end
|
54
|
+
.update( is: "%s",
|
55
|
+
has: "has %s" )
|
56
|
+
|
57
|
+
attr_reader :_object_, :_text_, :_block_, :_facts_
|
58
|
+
|
59
|
+
def initialize( object: nil, text: nil, &block )
|
60
|
+
@_object_, @_text_, @_block_ = object, text, block
|
61
|
+
@_facts_ = Hash.new do |ꜧ, key| ꜧ[key] = [ {} ] end
|
62
|
+
end
|
63
|
+
|
64
|
+
def note *subjects, **statements, &block
|
65
|
+
return *Array( subjects ).each do |s|
|
66
|
+
PUSH_ORDERED.( _facts_[s], s )
|
67
|
+
end if statements.empty?
|
68
|
+
subjects << _object_ if subjects.empty?
|
69
|
+
Array( subjects ).each do |subj|
|
70
|
+
statements.each do |verb, object|
|
71
|
+
PUSH_NAMED.( _facts_[subj], verb, object )
|
72
|
+
end
|
73
|
+
end
|
74
|
+
return statements.first[1]
|
75
|
+
end
|
76
|
+
|
77
|
+
def call *args
|
78
|
+
begin
|
79
|
+
instance_exec *args, &_block_
|
80
|
+
rescue StandardError => err
|
81
|
+
txt1 = "When trying #{_text_}"
|
82
|
+
thing, statements = _describe_
|
83
|
+
txt2 = DECORATE.( thing, prefix: ' ' )
|
84
|
+
txt3 = DECORATE.( statements.map { |verb, object|
|
85
|
+
STATE[verb] % object
|
86
|
+
}.join( ', ' ),
|
87
|
+
prefix: ' (', postfix: ')' )
|
88
|
+
txt4 = DECORATE.( _circumstances_, prefix: ', ' )
|
89
|
+
txt5 = DECORATE.( "#{err.class} occurred: #{err}", prefix: ', ' )
|
90
|
+
raise err, txt1 + txt2 + txt3 + txt4 + txt5
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def method_missing sym, *args
|
95
|
+
_object_.send sym, *args
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def _describe_ obj=_object_
|
101
|
+
facts = _facts_[obj].dup
|
102
|
+
statements = facts.extract_options!
|
103
|
+
fs = facts.join ', '
|
104
|
+
if statements.empty? then
|
105
|
+
return fs, statements
|
106
|
+
else
|
107
|
+
return facts.empty? ? obj.to_s : fs, statements
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def _circumstances_
|
112
|
+
_facts_.reject { |subj, _| subj == _object_ }.map { |subj, _|
|
113
|
+
thing, statements = _describe_( subj )
|
114
|
+
thing + DECORATE.( statements.map { |v, o|
|
115
|
+
TRANSITIVE[v] % o
|
116
|
+
}.join( ', ' ),
|
117
|
+
prefix: ' ' )
|
118
|
+
}.join( ', ' )
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Try method taxes two textual arguments and one block. The first (optional)
|
123
|
+
# textual argument describes what is the receiver of #try. The second argument
|
124
|
+
# describes in plain speech what activity is #try attempting. The block that
|
125
|
+
# follows then contains the code, which performs that activity. In the block,
|
126
|
+
# #note method is available, that builds up the context for the error message,
|
127
|
+
# if any.
|
128
|
+
#
|
129
|
+
def try object=self, to_do_something, &block
|
130
|
+
Try.new( object: object, text: to_do_something, &block ).call
|
131
|
+
end
|
132
|
+
end
|
data/lib/y_support/name_magic.rb
CHANGED
@@ -42,29 +42,32 @@ module NameMagic
|
|
42
42
|
# Attach the decorators etc.
|
43
43
|
target.extend ::NameMagic::ClassMethods
|
44
44
|
target.extend ::NameMagic::NamespaceMethods
|
45
|
-
# Attach namespace methods
|
45
|
+
# Attach namespace methods also to the namespace, if given.
|
46
46
|
begin
|
47
|
-
target.namespace
|
48
|
-
target.namespace
|
47
|
+
if target.namespace == target then
|
48
|
+
target.define_singleton_method :namespace do target end
|
49
|
+
else
|
50
|
+
target.namespace.extend ::NameMagic::NamespaceMethods
|
51
|
+
end
|
49
52
|
rescue NoMethodError
|
50
53
|
end
|
51
54
|
else # it is a Module; we'll infect it with this #included method
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
if
|
55
|
+
target_included = target.method( :included )
|
56
|
+
this_included = self.method( :included )
|
57
|
+
target_pre_included = begin
|
58
|
+
target.method( :pre_included )
|
59
|
+
rescue NameError
|
60
|
+
end
|
61
|
+
if target_pre_included then # target has #pre_included hook
|
59
62
|
target.define_singleton_method :included do |ç|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
+
target_pre_included.( ç )
|
64
|
+
this_included.( ç )
|
65
|
+
target_included.( ç )
|
63
66
|
end
|
64
67
|
else # target has no #pre_included hook
|
65
68
|
target.define_singleton_method :included do |ç|
|
66
|
-
|
67
|
-
|
69
|
+
this_included.( ç )
|
70
|
+
target_included.( ç )
|
68
71
|
end
|
69
72
|
end
|
70
73
|
end
|
data/lib/y_support/version.rb
CHANGED
@@ -0,0 +1,75 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
|
3
|
+
require 'minitest/spec'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
# require 'y_support/conscience' # tested component itself
|
6
|
+
require './../lib/y_support/conscience'
|
7
|
+
|
8
|
+
include Conscience
|
9
|
+
|
10
|
+
describe Try do
|
11
|
+
before do
|
12
|
+
@try = Try.new object: "Dummy", text: "to fire" do
|
13
|
+
note is: "dummy"
|
14
|
+
note has: "no care in the world"
|
15
|
+
n = note "the number", is: 42
|
16
|
+
raise TypeError, 'foo'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should have basic functionality" do
|
21
|
+
assert_equal "to fire", @try._text_
|
22
|
+
assert_equal "Dummy", @try._object_
|
23
|
+
assert_equal 0, @try._facts_.size # haven't tried anything yet
|
24
|
+
@try._facts_["something"]
|
25
|
+
assert_equal 1, @try._facts_.size
|
26
|
+
@try.note is: 'dummy'
|
27
|
+
@try.note has: 'no care in the world'
|
28
|
+
assert_equal 2, @try._facts_.size
|
29
|
+
assert_equal ["something", "Dummy"], @try._facts_.keys
|
30
|
+
assert_equal( [ { is: "dummy", has: "no care in the world" } ],
|
31
|
+
@try._facts_["Dummy"] )
|
32
|
+
assert_equal " hello!", Try::DECORATE.( :hello, prefix: ' ', postfix: '!' )
|
33
|
+
assert_equal( ['Dummy', {is: 'dummy', has: 'no care in the world'}],
|
34
|
+
@try.send( :_describe_, "Dummy" ) )
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'case 1' do
|
38
|
+
it "should work" do
|
39
|
+
begin
|
40
|
+
@try.call
|
41
|
+
rescue TypeError => err
|
42
|
+
expected_msg = "When trying to fire Dummy (dummy, has no care in " +
|
43
|
+
"the world), the number being 42, TypeError occurred: foo"
|
44
|
+
assert_equal expected_msg, err.message
|
45
|
+
else
|
46
|
+
flunk "Expected TypeError error not raised!"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'case 2' do
|
52
|
+
it "should work" do
|
53
|
+
begin
|
54
|
+
try "to call constant Nonexistant" do Nonexistant end
|
55
|
+
rescue NameError => err
|
56
|
+
expected_msg = 'When trying to call constant Nonexistant, ' +
|
57
|
+
'NameError occurred: uninitialized constant Nonexistant'
|
58
|
+
assert_equal( expected_msg, err.message )
|
59
|
+
else
|
60
|
+
flunk "Expected NameError error not raised!"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe 'case 3' do
|
66
|
+
it "should work" do
|
67
|
+
o = Object.new
|
68
|
+
class << o
|
69
|
+
def to_s; "THIS OBJECT" end
|
70
|
+
def hello!; "hello hello" end
|
71
|
+
end
|
72
|
+
o.try "to call a missing method" do hello! end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: y_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- boris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- lib/y_support.rb
|
54
54
|
- lib/y_support/abstract_algebra.rb
|
55
55
|
- lib/y_support/all.rb
|
56
|
+
- lib/y_support/conscience.rb
|
56
57
|
- lib/y_support/core_ext.rb
|
57
58
|
- lib/y_support/core_ext/array.rb
|
58
59
|
- lib/y_support/core_ext/array/misc.rb
|
@@ -94,6 +95,7 @@ files:
|
|
94
95
|
- lib/y_support/unicode.rb
|
95
96
|
- lib/y_support/version.rb
|
96
97
|
- test/abstract_algebra_test.rb
|
98
|
+
- test/conscience_test.rb
|
97
99
|
- test/inert_recorder_test.rb
|
98
100
|
- test/kde_test.rb
|
99
101
|
- test/local_object_test.rb
|
@@ -131,6 +133,7 @@ summary: LocalObject, RespondTo, InertRecorder, NullObject, NameMagic, core exte
|
|
131
133
|
typing etc.
|
132
134
|
test_files:
|
133
135
|
- test/abstract_algebra_test.rb
|
136
|
+
- test/conscience_test.rb
|
134
137
|
- test/inert_recorder_test.rb
|
135
138
|
- test/kde_test.rb
|
136
139
|
- test/local_object_test.rb
|