composite_type 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -38
- data/lib/composite_type/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79b73ddf6ca69a94ade50421d460271d88bfa71c
|
4
|
+
data.tar.gz: eff100ef24de4b9136566c47bd123eafe63491d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0be9100aa76f309188b51710feb30837f760c03556fa7a76f92541227811bb3499e62cbc8de7b395a045b47f17c98349968758c6800813773d4527263f805fe3
|
7
|
+
data.tar.gz: d05c6b593fee921e608bcb581c3478fb1b2b1d85ba3f2dd5c7582dac0ff3d12d5ad976be1afd3f27657aee22e3054590725b73f6de2422a7bfc5da570fbb7d8e
|
data/README.md
CHANGED
@@ -1,42 +1,13 @@
|
|
1
|
-
#
|
1
|
+
# CompositeType
|
2
2
|
|
3
|
-
|
3
|
+
Composite Types for Ruby
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
7
|
-
TypedAttr simplifies typed functional programming in Ruby.
|
8
|
-
|
9
|
-
The creation of data types is central to functional programming.
|
10
|
-
Ruby does not enforce any typing of object attributes or method parameters.
|
11
|
-
|
12
|
-
TypedAttr introduces a class macro "typed_attr". It constructs an #initialize method
|
13
|
-
given a list of attributes and their expected types.
|
14
|
-
|
15
|
-
Example:
|
16
|
-
|
17
|
-
require 'typed_attr'
|
18
|
-
class Account
|
19
|
-
typed_attr name: String, amount: Money
|
20
|
-
end
|
21
|
-
Account.new("Foo", Money.new(1234))
|
22
|
-
Account.new("Foo", 1234) # => raise TypeError
|
23
|
-
|
24
|
-
Use "typecheck" to perform checks on values:
|
25
|
-
|
26
|
-
def m x, y
|
27
|
-
typecheck x, String
|
28
|
-
typecheck y, Positive, Integer
|
29
|
-
x * y
|
30
|
-
end
|
31
|
-
m("string", -1) # => raise TypeError
|
32
|
-
m("string", 2) # => "stringstring"
|
33
|
-
|
34
|
-
The type assertions use the #=== matching operator.
|
35
|
-
|
36
7
|
Composite Types can be constructed to match deeper data structures:
|
37
8
|
|
38
9
|
h = { "a" => 1, "b" => :symbol }
|
39
|
-
|
10
|
+
Hash.of(String.with(Integer|Symbol)) === h # => true
|
40
11
|
|
41
12
|
Defining types through Modules:
|
42
13
|
|
@@ -58,22 +29,24 @@ Thus composite types can be used in "case when" clauses:
|
|
58
29
|
Logical operators: #|, #&, #~ are supported:
|
59
30
|
|
60
31
|
a = [ 1, 2, 3 ]
|
61
|
-
|
62
|
-
|
32
|
+
Array.of(Positive & Numeric) === a # => true
|
33
|
+
Array.of(~ NilClass) === a # => false
|
63
34
|
|
64
35
|
b = [ 1, -2, 3 ]
|
65
|
-
|
36
|
+
Array.of(Positive & Numeric) === b # => false
|
66
37
|
|
67
38
|
c = [ 1, nil, 3 ]
|
68
|
-
|
39
|
+
Array.of(~ NilClass) === c # => false
|
69
40
|
|
70
41
|
Composite types are cached indefinitely, therefore anonymous Modules cannot be composed.
|
71
42
|
|
43
|
+
See spec/lib/composite_type_spec.rb for more examples.
|
44
|
+
|
72
45
|
## Installation
|
73
46
|
|
74
47
|
Add this line to your application's Gemfile:
|
75
48
|
|
76
|
-
gem '
|
49
|
+
gem 'composite_type'
|
77
50
|
|
78
51
|
And then execute:
|
79
52
|
|
@@ -81,7 +54,7 @@ And then execute:
|
|
81
54
|
|
82
55
|
Or install it yourself as:
|
83
56
|
|
84
|
-
$ gem install
|
57
|
+
$ gem install composite_type
|
85
58
|
|
86
59
|
## Contributing
|
87
60
|
|