CompositeType
Composite Types for Ruby
Usage
Composite Types can be constructed to match deeper data structures:
h = { "a" => 1, "b" => :symbol }
Hash.of(String.with(Integer|Symbol)) === h # => true
Defining types through Modules:
module Even
def self.=== x
Integer === x and x.even?
end
end
Array.of(Even) === [ 2, 4, 10 ]
Composite types create dynamic Modules that redefine the #=== pattern matching operator. Thus composite types can be used in "case when" clauses:
case h
when Hash.of(String.with(Users)) ...
when Hash.of(Symbol.with(Object)) ...
end
Logical operators: #|, #&, #~ are supported:
a = [ 1, 2, 3 ]
Array.of(Positive & Integer) === a # => true
Array.of(~ NilClass) === a # => true
b = [ 1, -2, 3 ]
Array.of(Positive & Integer) === b # => false
c = [ 1, 1.5, 3 ]
Array.of(Positive & Integer) === c # => false
d = [ 1, nil, 3 ]
Array.of(~ NilClass) === d # => false
Composite types are cached indefinitely, therefore anonymous Modules cannot be composed.
See spec/lib/composite_type_spec.rb for more examples.
Installation
Add this line to your application's Gemfile:
gem 'composite_type'
And then execute:
$ bundle
Or install it yourself as:
$ gem install composite_type
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request