myrrha 2.0.0 → 3.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 3.0.0 / FIX ME
2
+
3
+ * Implementation of domains has been moved to the domain gem. The syntax to create domains
4
+ has therefore changed. Myrrha::Domain does no longer exists.
5
+
1
6
  # 2.0.0 / 2012-09-26
2
7
 
3
8
  * Removed support for ruby 1.8.7
@@ -24,7 +29,7 @@
24
29
  a Coercions instance under `coercions`.
25
30
 
26
31
  class Point
27
- include Domain::Impl.new(:x, :y)
32
+ include Domain::Impl.new([:x, :y])
28
33
 
29
34
  coercions do |c|
30
35
  c.coercion(String){|v,t| ...}
data/Gemfile CHANGED
@@ -1,5 +1,9 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
+ group :runtime do
4
+ gem "domain", "= 1.0.0.rc1"
5
+ end
6
+
3
7
  group :test do
4
8
  gem "rake", "~> 0.9.2"
5
9
  gem "rspec", "~> 2.8.0"
data/Gemfile.lock CHANGED
@@ -3,6 +3,7 @@ GEM
3
3
  specs:
4
4
  bluecloth (2.2.0)
5
5
  diff-lcs (1.1.3)
6
+ domain (1.0.0.rc1)
6
7
  rake (0.9.2.2)
7
8
  rspec (2.8.0)
8
9
  rspec-core (~> 2.8.0)
@@ -21,6 +22,7 @@ PLATFORMS
21
22
 
22
23
  DEPENDENCIES
23
24
  bluecloth (~> 2.2.0)
25
+ domain (= 1.0.0.rc1)
24
26
  rake (~> 0.9.2)
25
27
  rspec (~> 2.8.0)
26
28
  wlang (~> 0.10.2)
@@ -1,6 +1,6 @@
1
1
  require 'myrrha'
2
2
 
3
- PosInt = Myrrha::Domain.sbyc(Integer){|i| i>0}
3
+ PosInt = Domain.sbyc(Integer){|i| i>0}
4
4
 
5
5
  ###
6
6
 
data/lib/myrrha/coerce.rb CHANGED
@@ -1,9 +1,6 @@
1
1
  require 'myrrha'
2
2
  module Myrrha
3
-
4
- class Boolean < Object
5
- extend Myrrha::Domain::SByC.new(Object, [TrueClass, FalseClass], lambda{|x| (x==true) || (x==false)})
6
- end
3
+ Boolean = Domain.union(TrueClass, FalseClass)
7
4
 
8
5
  #
9
6
  # Coerces _s_ to a Boolean
@@ -225,8 +225,8 @@ module Myrrha
225
225
  def subdomain?(child, parent)
226
226
  if child == parent
227
227
  true
228
- elsif parent.respond_to?(:superdomain_of?)
229
- parent.superdomain_of?(child)
228
+ elsif parent.respond_to?(:super_domain_of?)
229
+ parent.super_domain_of?(child)
230
230
  elsif child.respond_to?(:superclass) && child.superclass
231
231
  subdomain?(child.superclass, parent)
232
232
  else
@@ -0,0 +1,16 @@
1
+ module Domain
2
+ module CoercionMethods
3
+
4
+ def coercions(&bl)
5
+ @coercions ||= ::Myrrha::Coercions.new{|c| c.main_target_domain = self}
6
+ @coercions.append(&bl) if bl
7
+ @coercions
8
+ end
9
+
10
+ def coerce(arg)
11
+ coercions.coerce(arg, self)
12
+ end
13
+
14
+ end # module CoercionMethods
15
+ include CoercionMethods
16
+ end # module Domain
data/lib/myrrha/loader.rb CHANGED
@@ -0,0 +1 @@
1
+ require "domain"
@@ -1,9 +1,9 @@
1
1
  module Myrrha
2
2
  module Version
3
3
 
4
- MAJOR = 2
4
+ MAJOR = 3
5
5
  MINOR = 0
6
- TINY = 0
6
+ TINY = "0.rc1"
7
7
 
8
8
  def self.to_s
9
9
  [ MAJOR, MINOR, TINY ].join('.')
data/lib/myrrha.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  require_relative "myrrha/version"
2
2
  require_relative "myrrha/loader"
3
3
  require_relative 'myrrha/errors'
4
+ require_relative 'myrrha/ext/domain'
4
5
  #
5
6
  # Myrrha -- the missing coercion framework for Ruby
6
7
  #
7
8
  module Myrrha
8
9
 
9
- require_relative 'myrrha/domain'
10
10
  require_relative 'myrrha/coercions'
11
11
 
12
12
  # Builds a set of coercions rules.
data/myrrha.gemspec CHANGED
@@ -128,7 +128,7 @@ Gem::Specification.new do |s|
128
128
  s.add_development_dependency("yard", "~> 0.7.4")
129
129
  s.add_development_dependency("bluecloth", "~> 2.2.0")
130
130
  s.add_development_dependency("wlang", "~> 0.10.2")
131
-
131
+ s.add_dependency("domain", "= 1.0.0.rc1")
132
132
 
133
133
  # The version of ruby required by this gem
134
134
  #
data/myrrha.noespec CHANGED
@@ -10,7 +10,7 @@ variables:
10
10
  upper:
11
11
  Myrrha
12
12
  version:
13
- 2.0.0
13
+ 3.0.0.rc1
14
14
  summary: |-
15
15
  Myrrha provides the coercion framework which is missing to Ruby.
16
16
  description: |-
@@ -26,11 +26,12 @@ variables:
26
26
  - http://github.com/blambeau/myrrha
27
27
  - http://rubygems.org/gems/myrrha
28
28
  dependencies:
29
- - {name: rake, version: "~> 0.9.2", groups: [test, release]}
30
- - {name: rspec, version: "~> 2.8.0", groups: [test, release]}
31
- - {name: yard, version: "~> 0.7.4", groups: [doc ]}
32
- - {name: bluecloth, version: "~> 2.2.0", groups: [doc ]}
33
- - {name: wlang, version: "~> 0.10.2", groups: [release ]}
29
+ - {name: domain, version: "= 1.0.0.rc1", groups: [runtime] }
30
+ - {name: rake, version: "~> 0.9.2", groups: [test, release]}
31
+ - {name: rspec, version: "~> 2.8.0", groups: [test, release]}
32
+ - {name: yard, version: "~> 0.7.4", groups: [doc ]}
33
+ - {name: bluecloth, version: "~> 2.2.0", groups: [doc ]}
34
+ - {name: wlang, version: "~> 0.10.2", groups: [release ]}
34
35
  rake_tasks:
35
36
  debug_mail:
36
37
  rx_changelog_sections: /^# /
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+ describe Domain, "coerce" do
3
+
4
+ let(:domain){ Domain.scalar(:x, :y) }
5
+
6
+ before do
7
+ domain.coercions do |c|
8
+ c.coercion(String){|v,_| domain.new(1,2) }
9
+ end
10
+ end
11
+
12
+ it 'should delegate to coercions' do
13
+ domain.coerce("blah").should eq(domain.new(1,2))
14
+ end
15
+
16
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+ describe Domain, "coercions" do
3
+
4
+ let(:domain){ Domain.scalar(:x, :y) }
5
+
6
+ context 'without a block' do
7
+ subject{ domain.coercions }
8
+
9
+ it{ should be_a(Myrrha::Coercions) }
10
+
11
+ it 'should have the main target domain defined' do
12
+ subject.main_target_domain.should eq(domain)
13
+ end
14
+ end
15
+
16
+ context 'with a block' do
17
+ subject{ domain.coercions{|c| @seen=c} }
18
+
19
+ it 'should yield the coercions' do
20
+ subject
21
+ @seen.should eq(subject)
22
+ end
23
+ end
24
+
25
+ end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
  describe "Myrrha.coercions" do
3
3
 
4
4
  it "should support using user-defined domains" do
5
- name = Myrrha::Domain.sbyc{|s| s.is_a?(Symbol)}
5
+ name = Domain.sbyc{|s| s.is_a?(Symbol)}
6
6
  rules = Myrrha.coercions do |r|
7
7
  r.coercion String, name, lambda{|s,t| s.to_sym}
8
8
  r.coercion name, String, lambda{|s,t| s.to_s}
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myrrha
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
5
- prerelease:
4
+ version: 3.0.0.rc1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Bernard Lambeau
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-26 00:00:00.000000000 Z
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -91,6 +91,22 @@ dependencies:
91
91
  - - ~>
92
92
  - !ruby/object:Gem::Version
93
93
  version: 0.10.2
94
+ - !ruby/object:Gem::Dependency
95
+ name: domain
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - '='
100
+ - !ruby/object:Gem::Version
101
+ version: 1.0.0.rc1
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - '='
108
+ - !ruby/object:Gem::Version
109
+ version: 1.0.0.rc1
94
110
  description: ! "Myrrha provides the coercion framework which is missing to Ruby. Coercions\nare
95
111
  simply defined as a set of rules for converting values from source to target\ndomains
96
112
  (in an abstract sense). As a typical and useful example, it comes with \na coerce()
@@ -122,12 +138,8 @@ files:
122
138
  - examples/to_ruby_literal_noext.rb
123
139
  - lib/myrrha/coerce.rb
124
140
  - lib/myrrha/coercions.rb
125
- - lib/myrrha/domain/coercion_methods.rb
126
- - lib/myrrha/domain/impl.rb
127
- - lib/myrrha/domain/sbyc.rb
128
- - lib/myrrha/domain/value_methods.rb
129
- - lib/myrrha/domain.rb
130
141
  - lib/myrrha/errors.rb
142
+ - lib/myrrha/ext/domain.rb
131
143
  - lib/myrrha/loader.rb
132
144
  - lib/myrrha/to_ruby_literal.rb
133
145
  - lib/myrrha/version.rb
@@ -139,10 +151,8 @@ files:
139
151
  - spec/coercions/test_delegate.rb
140
152
  - spec/coercions/test_dup.rb
141
153
  - spec/coercions/test_subdomain.rb
142
- - spec/domain/impl/test_behavior.rb
143
- - spec/domain/impl/test_value_methods.rb
144
- - spec/domain/sbyc/test_behavior.rb
145
- - spec/domain/test_sbyc.rb
154
+ - spec/ext/domain/test_coerce.rb
155
+ - spec/ext/domain/test_coercions.rb
146
156
  - spec/myrrha/test_coercions.rb
147
157
  - spec/shared/a_value.rb
148
158
  - spec/spec_helper.rb
@@ -179,15 +189,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
179
189
  - - ! '>='
180
190
  - !ruby/object:Gem::Version
181
191
  version: '0'
182
- segments:
183
- - 0
184
- hash: -1652815196407861424
185
192
  required_rubygems_version: !ruby/object:Gem::Requirement
186
193
  none: false
187
194
  requirements:
188
- - - ! '>='
195
+ - - ! '>'
189
196
  - !ruby/object:Gem::Version
190
- version: '0'
197
+ version: 1.3.1
191
198
  requirements: []
192
199
  rubyforge_project:
193
200
  rubygems_version: 1.8.24
@@ -201,10 +208,8 @@ test_files:
201
208
  - spec/coercions/test_delegate.rb
202
209
  - spec/coercions/test_dup.rb
203
210
  - spec/coercions/test_subdomain.rb
204
- - spec/domain/impl/test_behavior.rb
205
- - spec/domain/impl/test_value_methods.rb
206
- - spec/domain/sbyc/test_behavior.rb
207
- - spec/domain/test_sbyc.rb
211
+ - spec/ext/domain/test_coerce.rb
212
+ - spec/ext/domain/test_coercions.rb
208
213
  - spec/myrrha/test_coercions.rb
209
214
  - spec/shared/a_value.rb
210
215
  - spec/spec_helper.rb
@@ -1,17 +0,0 @@
1
- module Myrrha
2
- module Domain
3
- module CoercionMethods
4
-
5
- def coercions(&bl)
6
- @coercions ||= Coercions.new{|c| c.main_target_domain = self}
7
- @coercions.append(&bl) if bl
8
- @coercions
9
- end
10
-
11
- def coerce(arg)
12
- coercions.coerce(arg, self)
13
- end
14
-
15
- end # module CoercionMethods
16
- end # module Domain
17
- end # module Myrrha
@@ -1,50 +0,0 @@
1
- module Myrrha
2
- module Domain
3
- class Impl < Module
4
-
5
- def initialize(component_names)
6
- @component_names = component_names.freeze
7
- define_initialize
8
- define_component_readers
9
- include_value_methods
10
- end
11
-
12
- def included(clazz)
13
- define_component_names_on(clazz)
14
- define_coercion_methods_on(clazz)
15
- super
16
- end
17
-
18
- private
19
-
20
- def define_initialize
21
- component_names = @component_names
22
- define_method(:initialize){|*args|
23
- component_names.zip(args).each do |n,arg|
24
- instance_variable_set(:"@#{n}", arg)
25
- end
26
- }
27
- end
28
-
29
- def define_component_readers
30
- @component_names.each do |n|
31
- define_method(n){ instance_variable_get(:"@#{n}") }
32
- end
33
- end
34
-
35
- def include_value_methods
36
- module_eval{ include ValueMethods }
37
- end
38
-
39
- def define_component_names_on(clazz)
40
- component_names = @component_names
41
- clazz.define_singleton_method(:component_names){ component_names }
42
- end
43
-
44
- def define_coercion_methods_on(clazz)
45
- clazz.extend(CoercionMethods)
46
- end
47
-
48
- end # class Impl
49
- end # module Domain
50
- end # module Myrrha
@@ -1,78 +0,0 @@
1
- module Myrrha
2
- module Domain
3
- class SByC < Module
4
-
5
- def initialize(super_domain, sub_domains, predicate)
6
- @super_domain = super_domain
7
- @sub_domains = sub_domains
8
- @predicate = predicate
9
- define
10
- end
11
-
12
- private
13
-
14
- def define
15
- define_super_domain_method
16
- define_sub_domains_method
17
- define_predicate_method
18
- include_type_methods
19
- include_coercion_methods
20
- end
21
-
22
- def define_super_domain_method
23
- super_domain = @super_domain
24
- define_method(:super_domain){ super_domain }
25
- end
26
-
27
- def define_sub_domains_method
28
- sub_domains = @sub_domains
29
- define_method(:sub_domains){ sub_domains }
30
- end
31
-
32
- def define_predicate_method
33
- predicate = @predicate
34
- define_method(:predicate){ predicate }
35
- end
36
-
37
- def include_type_methods
38
- module_eval{ include TypeMethods }
39
- end
40
-
41
- def include_coercion_methods
42
- module_eval{ include CoercionMethods }
43
- end
44
-
45
- module TypeMethods
46
-
47
- # Creates a new instance of this domain
48
- def new(*args)
49
- if (args.size == 1) && (superclass===args.first)
50
- raise ArgumentError, "Invalid value #{args.join(' ')} for #{self}" unless self===args.first
51
- args.first
52
- elsif superclass.respond_to?(:new)
53
- new(super(*args))
54
- else
55
- raise ArgumentError, "Invalid value #{args.join(' ')} for #{self}"
56
- end
57
- end
58
-
59
- # (see Class.superclass)
60
- def superclass
61
- super_domain || super
62
- end
63
-
64
- # Returns true if clazz if an explicit sub domain of self or if it's the case in Ruby.
65
- def superdomain_of?(child)
66
- sub_domains.include?(child)
67
- end
68
-
69
- # Checks if `value` belongs to this domain
70
- def ===(value)
71
- (superclass === value) && predicate.call(value)
72
- end
73
-
74
- end # module TypeMethods
75
-
76
- end # module SByC
77
- end # module Domain
78
- end # module Myrrha
@@ -1,60 +0,0 @@
1
- module Myrrha
2
- module Domain
3
- module ValueMethods
4
-
5
- # Parts of this module have been extracted from Virtus, MIT Copyright (c) 2011-2012
6
- # Piotr Solnica.
7
-
8
- # Returns a hash code for the value
9
- #
10
- # @return Integer
11
- #
12
- # @api public
13
- def hash
14
- component_names.map{|key| send(key).hash }.reduce(self.class.hash, :^)
15
- end
16
-
17
- # Compare the object with other object for equality
18
- #
19
- # @example
20
- # object.eql?(other) # => true or false
21
- #
22
- # @param [Object] other
23
- # the other object to compare with
24
- #
25
- # @return [Boolean]
26
- #
27
- # @api public
28
- def eql?(other)
29
- instance_of?(other.class) and cmp?(__method__, other)
30
- end
31
-
32
- # Compare the object with other object for equivalency
33
- #
34
- # @example
35
- # object == other # => true or false
36
- #
37
- # @param [Object] other
38
- # the other object to compare with
39
- #
40
- # @return [Boolean]
41
- #
42
- # @api public
43
- def ==(other)
44
- return false unless self.class <=> other.class
45
- cmp?(__method__, other)
46
- end
47
-
48
- private
49
-
50
- def cmp?(comparator, other)
51
- component_names.all?{|key| send(key).send(comparator, other.send(key)) }
52
- end
53
-
54
- def component_names
55
- self.class.component_names
56
- end
57
-
58
- end # module ValueMethods
59
- end # module Domain
60
- end # module Myrrha
data/lib/myrrha/domain.rb DELETED
@@ -1,18 +0,0 @@
1
- module Myrrha
2
- module Domain
3
-
4
- # Creates a domain instance by specialization by constraint
5
- #
6
- # @param [Class] superdom the superdomain of the created domain
7
- # @param [Proc] pred the domain predicate
8
- # @return [Class] the created domain
9
- def self.sbyc(superdom = Object, subdoms = [], &pred)
10
- Class.new(superdom).extend SByC.new(superdom, subdoms, pred)
11
- end
12
-
13
- end # module Domain
14
- end # module Myrrha
15
- require_relative 'domain/value_methods'
16
- require_relative 'domain/coercion_methods'
17
- require_relative 'domain/impl'
18
- require_relative 'domain/sbyc'
@@ -1,41 +0,0 @@
1
- require 'spec_helper'
2
- module Myrrha
3
- describe Domain, 'when included' do
4
-
5
- let(:point){
6
- Class.new{
7
- include Myrrha::Domain::Impl.new([:r, :theta])
8
-
9
- coercions do |c|
10
- c.upon(NilClass) {|v,t| new(0.0,0.0) }
11
- c.coercion(String) {|v,t| new(*v.split(',').map(&:to_f)) }
12
- c.coercion(TrueClass){|v,t| throw :nextrule }
13
- c.fallback(TrueClass){|v,t| new(1.0, 1.0) }
14
- end
15
- }
16
- }
17
-
18
- let(:origin){ point.new(0.0, 0.0) }
19
- let(:oneone){ point.new(1.0, 1.0) }
20
-
21
- it 'installs the domain component names' do
22
- point.component_names.should eq([:r, :theta])
23
- end
24
-
25
- it 'installs the component readers' do
26
- origin.r.should eq(0.0)
27
- origin.theta.should eq(0.0)
28
- end
29
-
30
- it 'has coercions properly installed' do
31
- point.coercions.main_target_domain.should eq(point)
32
- end
33
-
34
- it 'supports expected coercions' do
35
- point.coerce(nil).should eq(origin)
36
- point.coerce(true).should eq(oneone)
37
- point.coerce("2.0,3.0").should eq(point.new(2.0, 3.0))
38
- end
39
-
40
- end
41
- end
@@ -1,101 +0,0 @@
1
- require 'spec_helper'
2
- module Myrrha
3
- module Domain
4
- class Impl
5
- describe ValueMethods do
6
-
7
- let(:domain){
8
- Class.new{
9
- def self.component_names
10
- [:a, :b]
11
- end
12
- include ValueMethods
13
- attr_reader :a, :b
14
- def initialize(a, b)
15
- @a, @b = a, b
16
- end
17
- }
18
- }
19
-
20
- let(:object){ domain.new(1, 2) }
21
-
22
- describe "==" do
23
- subject{ object == other }
24
-
25
- context 'with itself' do
26
- let(:other){ object }
27
- it{ should be_true }
28
- end
29
-
30
- context 'with dupped' do
31
- let(:other){ object.dup }
32
- it{ should be_true }
33
- end
34
-
35
- context 'with equivalent' do
36
- let(:other){ domain.new(1, 2) }
37
- it{ should be_true }
38
- end
39
-
40
- context 'with equivalent of a subclass' do
41
- let(:other){ Class.new(domain).new(1, 2) }
42
- it{ should be_true }
43
- end
44
-
45
- context 'with non equivalent' do
46
- let(:other){ domain.new(1, 3) }
47
- it{ should be_false }
48
- end
49
-
50
- context 'with other class' do
51
- let(:other){ self }
52
- it{ should be_false }
53
- end
54
- end
55
-
56
- describe "eql?" do
57
- subject{ object.eql?(other) }
58
-
59
- context 'with itself' do
60
- let(:other){ object }
61
- it{ should be_true }
62
- end
63
-
64
- context 'with dupped' do
65
- let(:other){ object.dup }
66
- it{ should be_true }
67
- end
68
-
69
- context 'with equivalent' do
70
- let(:other){ domain.new(1, 2) }
71
- it{ should be_true }
72
- end
73
-
74
- context 'with equivalent of a subclass' do
75
- let(:other){ Class.new(domain).new(1, 2) }
76
- it{ should be_false }
77
- end
78
-
79
- context 'with non equivalent' do
80
- let(:other){ domain.new(1, 3) }
81
- it{ should be_false }
82
- end
83
-
84
- context 'with other class' do
85
- let(:other){ self }
86
- it{ should be_false }
87
- end
88
- end
89
-
90
- describe "hash" do
91
- subject{ object.hash }
92
-
93
- it 'should be consistent with equal' do
94
- subject.should eq(domain.new(1,2).hash)
95
- end
96
- end
97
-
98
- end
99
- end
100
- end
101
- end
@@ -1,46 +0,0 @@
1
- require 'spec_helper'
2
- module Myrrha
3
- module Domain
4
- describe SByC, 'when used by extension' do
5
-
6
- class NegInt < Integer
7
- extend Myrrha::Domain::SByC.new(Integer, [], lambda{|i| i<0})
8
- end
9
-
10
- specify("#name") {
11
- NegInt.name.should eq("Myrrha::Domain::NegInt")
12
- }
13
-
14
- specify("#new") {
15
- NegInt.new(-12).should eq(-12)
16
- lambda {
17
- NegInt.new(12)
18
- }.should raise_error(ArgumentError)
19
- }
20
-
21
- specify("#superclass"){
22
- NegInt.superclass.should eql(Integer)
23
- }
24
-
25
- specify("#superdomain_of?"){
26
- NegInt.superdomain_of?(Object).should be_false
27
- NegInt.superdomain_of?(Integer).should be_false
28
- }
29
-
30
- specify("#coercions"){
31
- NegInt.coercions.should be_a(Myrrha::Coercions)
32
- NegInt.coercions.main_target_domain.should eq(NegInt)
33
- }
34
-
35
- it "should be usable in a case" do
36
- [-12, 12].map{|i|
37
- case i
38
- when NegInt then :negint
39
- when Integer then :integer
40
- end
41
- }.should eq([:negint, :integer])
42
- end
43
-
44
- end
45
- end
46
- end
@@ -1,81 +0,0 @@
1
- require 'spec_helper'
2
- module Myrrha
3
- module Domain
4
- describe ".sbyc" do
5
-
6
- specify "the basic contract" do
7
- subject = Myrrha::Domain.sbyc{|s| s == 12}
8
- subject.should be_a(Class)
9
- subject.superclass.should eq(Object)
10
- (subject === 12).should be_true
11
- (subject === 13).should be_false
12
- end
13
-
14
- specify "with a ruby superclass" do
15
- subject = Myrrha::Domain.sbyc(Integer){|i| i > 0}
16
- subject.should be_a(Class)
17
- subject.superclass.should eq(Integer)
18
- (subject === 12).should be_true
19
- (subject === 0).should be_false
20
- end
21
-
22
- describe "A factored sub domain of Integer" do
23
- PosInt = Myrrha::Domain.sbyc(Integer){|i| i > 0}
24
- specify("#name") {
25
- PosInt.name.should eq("Myrrha::Domain::PosInt")
26
- }
27
- specify("#new") {
28
- PosInt.new(12).should eq(12)
29
- lambda {
30
- PosInt.new(0)
31
- }.should raise_error(ArgumentError)
32
- }
33
- specify("#superclass"){
34
- PosInt.superclass.should eql(Integer)
35
- }
36
- specify("#superdomain_of?"){
37
- PosInt.superdomain_of?(Object).should be_false
38
- PosInt.superdomain_of?(Integer).should be_false
39
- }
40
- it "should be usable in a case" do
41
- [-12, 12].collect{|i|
42
- case i
43
- when PosInt
44
- :posint
45
- when Integer
46
- :integer
47
- end
48
- }.should eq([:integer, :posint])
49
- end
50
- end
51
-
52
- describe "A factored sub domain of a user-defined Class" do
53
- class Color
54
- attr_reader :r
55
- attr_reader :g
56
- attr_reader :b
57
- def initialize(r,g,b)
58
- raise ArgumentError unless [r,g,b].all?{|i| i.is_a?(Integer)}
59
- @r, @g, @b = r, g, b
60
- end
61
- end
62
- RedToZero = Myrrha::Domain.sbyc(Color){|c| c.r == 0}
63
- specify("#===") {
64
- (RedToZero === Color.new(0,1,1)).should be_true
65
- (RedToZero === Color.new(1,1,1)).should be_false
66
- }
67
- specify("#new") {
68
- RedToZero.new(Color.new(0,1,1)).should be_a(Color)
69
- RedToZero.new(0, 1, 1).should be_a(Color)
70
- lambda{
71
- RedToZero.new(Color.new(1,1,1))
72
- }.should raise_error(ArgumentError)
73
- lambda{
74
- RedToZero.new(1, 1, 1)
75
- }.should raise_error(ArgumentError)
76
- }
77
- end
78
-
79
- end
80
- end
81
- end