-
Notifications
You must be signed in to change notification settings - Fork 1
/
v1.1.0.txt
92 lines (67 loc) · 3.23 KB
/
v1.1.0.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
myrrha version 1.1.0 has been released!
Myrrha provides the coercion framework which is missing to Ruby, IMHO.
* <http://github.com/blambeau/myrrha>
* <http://rubydoc.info/gems/myrrha>
* <http://rubygems.org/gems/myrrha>
Myrrha provides the coercion framework which is missing to Ruby. Coercions are
simply defined as a set of rules for converting values from source to target
domains (in an abstract sense). As a typical and useful example, it comes with
a coerce() method providing a unique entry point for converting a string to a
numeric, a boolean, a date, a time, an URI, and so on.
Changes:
# 1.1.0 / 2011-07-28
## Enhancements to coerce()
* Added coercion rules from Symbol/String to Module/Class
coerce("Integer", Class) # => Integer
coerce(:Integer, Class) # => Integer
coerce("Myrrha::Version", Module) # => Myrrha::Version
[... and so on ...]
* Added following coercion rules for Booleans
coerce("true", TrueClass) # => true
coerce("false", FalseClass) # => false
* Added coercion rule from any Object to String through ruby String(). Note
that even with this coercion rule, coerce(nil, String) returns nil as
that
rule has higher priority.
* require('time') is automatically issued when trying to coerce a String to
a Time. Time.parse is obviously needed.
* Myrrha::Boolean (Boolean with core extensions) is now a sbyc domain (see
below). Therefore, it is now a Class instance.
## Enhancements to the general coercion mechanism
* An optimistic coercion is tried when a rule is encountered whose target
domain is a super domain of the requested one. Coercion only succeeds if
the coerced value correctly belongs to the latter domain. Example:
rules = Myrrha.coercions do |r|
r.coercion String, Numeric, lambda{|s,t| Integer(s)}
end
rules.coerce("12", Integer) # => 12 in 1.1.0 while it failed in 1.0.0
rules.coerce("12", Float) # => Myrrha::Error
* You can now specify a coercion path, through an array of domains. For
example (completely contrived, of course):
rules = Myrrha.coercions do |r|
r.coercion String, Symbol, lambda{|s,t| s.to_sym }
r.coercion Float, String, lambda{|s,t| s.to_s }
r.coercion Integer, Float, lambda{|s,t| Float(s) }
r.coercion Integer, Symbol, [Float, String]
end
rules.coerce(12, Symbol) # => :"12.0" as
Symbol(String(Float(12)))
* You can now define domains through specialization by constraint (sbyc) on
ruby
classes, using Myrrha.domain:
# Create a positive integer domain, as ... positive integers
PosInt = Myrrha.domain(Integer){|i| i > 0 }
Created domain is a real Class instance, that correctly responds to :===
and :superclass. The feature is mainly introduced for supporting the
following
kind of coercion scenarios (see README for more about this):
rules = Myrrha.coercions do |r|
r.coercion String, Integer, lambda{|s,t| Integer(s)}
end
rules.coerce("12", PosInt) # => 12
rules.coerce("-12", PosInt) # => ArgumentError, "Invalid value -12 for
PosInt"
## Bug fixes
* Fixed Coercions#dup when a set of rules has a main target domain. This
fixes
the duplication of ToRubyLiteral rules, among others.