domain 1.0.0.rc1 → 1.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
data/domain.noespec CHANGED
@@ -7,7 +7,7 @@ variables:
7
7
  upper:
8
8
  Domain
9
9
  version:
10
- 1.0.0.rc1
10
+ 1.0.0.rc2
11
11
  summary: |-
12
12
  Provide tools for implementing domains (aka data types)
13
13
  description: |-
data/lib/domain.rb CHANGED
@@ -10,4 +10,6 @@ module Domain
10
10
  include API
11
11
  extend Factory
12
12
 
13
+ TRUE_PREDICATE = lambda{|*args,&bl| true }
14
+
13
15
  end # module Domain
data/lib/domain/api.rb CHANGED
@@ -1,16 +1,6 @@
1
1
  module Domain
2
2
  module API
3
3
 
4
- # Returns the domain predicate, nil if no such predicate
5
- #
6
- # @return [Proc]
7
- # the domain predicate (possibly nil)
8
- #
9
- # @api public
10
- def predicate
11
- nil
12
- end
13
-
14
4
  # Returns the super domain of `self`.
15
5
  #
16
6
  # @return [Class]
@@ -44,5 +34,18 @@ module Domain
44
34
  sub_domains.include?(dom)
45
35
  end
46
36
 
37
+ # Raises a type error for `args`.
38
+ #
39
+ # @param [Array] args
40
+ # arguments passed to `new` or another factory method
41
+ # @raise TypeError
42
+ #
43
+ # @api protected
44
+ def domain_error!(first, *args)
45
+ first = [first]+args unless args.empty?
46
+ raise TypeError, "Can't convert `#{first.inspect}` into #{self}", caller
47
+ end
48
+ protected :domain_error!
49
+
47
50
  end # module API
48
51
  end # module Domain
@@ -2,23 +2,25 @@ module Domain
2
2
  module Reuse
3
3
 
4
4
  def self.new(reuse_domain, predicate = nil, &bl)
5
- ImplDomain.new [ Methods, class_module(predicate || bl) ],
6
- [ instance_module(reuse_domain) ]
5
+ predicate = predicate || bl || Domain::TRUE_PREDICATE
6
+ DomainFactory.factor [ Helpers, class_module(reuse_domain, predicate) ],
7
+ [ instance_module(reuse_domain) ]
7
8
  end
8
9
 
9
- def self.class_module(predicate)
10
- Module.new{ define_method(:predicate){ predicate } }
10
+ def self.class_module(reuse_domain, predicate)
11
+ Module.new{
12
+ define_method(:predicate) do
13
+ predicate
14
+ end
15
+ define_method(:domain_check!) do |i|
16
+ domain_error!(i) unless reuse_domain===i && predicate.call(i)
17
+ end
18
+ }
11
19
  end
12
20
 
13
21
  def self.instance_module(reuse_domain)
14
22
  Module.new{
15
23
  define_method(:initialize) do |arg|
16
- unless reuse_domain===arg
17
- raise ArgumentError, "#{reuse_domain} expected, got `#{arg.inspect}`"
18
- end
19
- if self.class.predicate && !self.class.predicate.call(arg)
20
- raise ArgumentError, "Invalid value `#{arg.inspect}` for `#{self}`"
21
- end
22
24
  @reused_instance = arg
23
25
  end
24
26
  define_method(:reused_instance) do
@@ -29,25 +31,21 @@ module Domain
29
31
  }
30
32
  end
31
33
 
32
- module Methods
34
+ module Helpers
33
35
 
34
36
  def reuse(*methods)
35
37
  methods.each do |m|
36
- define_method(m) do |*args, &bl|
37
- reused_instance.send(m, *args, &bl)
38
- end
38
+ define_method(m){|*args, &bl| reused_instance.send(m, *args, &bl)}
39
39
  end
40
40
  end
41
41
 
42
42
  def recoat(*methods)
43
43
  methods.each do |m|
44
- define_method(m) do |*args, &bl|
45
- self.class.new reused_instance.send(m, *args, &bl)
46
- end
44
+ define_method(m){|*args, &bl| self.class.new reused_instance.send(m, *args, &bl)}
47
45
  end
48
46
  end
49
47
 
50
- end # module Methods
48
+ end # module Helpers
51
49
 
52
50
  end # module Reuse
53
51
  end # module Domain
@@ -2,8 +2,32 @@ module Domain
2
2
  module SByC
3
3
 
4
4
  def self.new(super_domain = Object, predicate = nil, &bl)
5
- FakeDomain.new(super_domain, [], predicate || bl)
5
+ predicate = predicate || bl || Domain::TRUE_PREDICATE
6
+ DomainFactory.factor [ Methods, class_module(super_domain, predicate) ]
6
7
  end
7
8
 
9
+ def self.class_module(super_domain, predicate)
10
+ Module.new{
11
+ define_method(:super_domain){ super_domain }
12
+ define_method(:predicate) { predicate }
13
+ }
14
+ end
15
+
16
+ module Methods
17
+
18
+ # Creates a new instance of this domain
19
+ def new(first = nil, *args)
20
+ return first if args.empty? && self===first
21
+ return new super(first, *args) if superclass.respond_to?(:new) && (superclass != Object)
22
+ domain_error!(first, *args)
23
+ end
24
+
25
+ # Checks if `value` belongs to this domain
26
+ def ===(value)
27
+ superclass===value && predicate.call(value)
28
+ end
29
+
30
+ end # module Methods
31
+
8
32
  end # module SByC
9
33
  end # module Domain
@@ -2,7 +2,7 @@ module Domain
2
2
  module Scalar
3
3
 
4
4
  def self.new(*component_names)
5
- ImplDomain.new [], instance_module(component_names)
5
+ DomainFactory.factor [], instance_module(component_names)
6
6
  end
7
7
 
8
8
  def self.instance_module(component_names)
@@ -2,10 +2,34 @@ module Domain
2
2
  module Union
3
3
 
4
4
  def self.new(*sub_domains)
5
- FakeDomain.new(Object, sub_domains, lambda{|t| sub_domains.any?{|d| d===t } })
5
+ DomainFactory.factor [ Methods, class_module(sub_domains) ]
6
6
  end
7
7
 
8
+ def self.class_module(sub_domains)
9
+ Module.new{
10
+ define_method(:sub_domains){ sub_domains }
11
+ }
12
+ end
13
+
14
+ module Methods
15
+
16
+ # Creates a new instance of this domain
17
+ def new(first = nil, *args)
18
+ return first if args.empty? && self===first
19
+ return new super(first, *args) if superclass.respond_to?(:new) && (superclass != Object)
20
+ domain_error!(first, *args)
21
+ end
22
+
23
+ def predicate
24
+ lambda{|value| sub_domains.any?{|d| d===value } }
25
+ end
26
+
27
+ # Checks if `value` belongs to this domain
28
+ def ===(value)
29
+ predicate.call(value)
30
+ end
31
+
32
+ end # module Methods
33
+
8
34
  end # class Union
9
35
  end # module Domain
10
-
11
-
@@ -1,3 +1,2 @@
1
1
  require_relative 'support/equalizer'
2
- require_relative 'support/fake_domain'
3
- require_relative 'support/impl_domain'
2
+ require_relative 'support/domain_factory'
@@ -0,0 +1,21 @@
1
+ module Domain
2
+ module DomainFactory
3
+
4
+ def self.factor(c_methods = [], i_methods = [])
5
+ i_methods, c_methods = Array(i_methods), Array(c_methods).unshift(Domain)
6
+ Module.new{
7
+ # include all class methods first
8
+ c_methods.each{|c_m| include(c_m) }
9
+
10
+ # Ensure that classes that are extended will include all instance methods
11
+ define_singleton_method(:extend_object) do |obj|
12
+ if obj.is_a?(Class)
13
+ obj.module_eval{ i_methods.each{|i_m| include(i_m) } }
14
+ end
15
+ super(obj)
16
+ end
17
+ }
18
+ end
19
+
20
+ end # module DomainFactory
21
+ end # module Domain
@@ -3,7 +3,7 @@ module Domain
3
3
 
4
4
  MAJOR = 1
5
5
  MINOR = 0
6
- TINY = "0.rc1"
6
+ TINY = "0.rc2"
7
7
 
8
8
  def self.to_s
9
9
  [ MAJOR, MINOR, TINY ].join('.')
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ module Domain
3
+ describe Reuse, "domain_check!" do
4
+
5
+ it 'raises an ArgumentError when not correct reused instance' do
6
+ lambda{
7
+ List.domain_check!('foo')
8
+ }.should raise_error(TypeError)
9
+ end
10
+
11
+ it 'raises an ArgumentError when the predicate is not satisfied' do
12
+ lambda{
13
+ Tuple.domain_check!("foo" => :bar)
14
+ }.should raise_error(TypeError)
15
+ end
16
+
17
+ end
18
+ end
@@ -10,17 +10,5 @@ module Domain
10
10
  Tuple.new(:foo => "bar").should be_a(Tuple)
11
11
  end
12
12
 
13
- it 'raises an ArgumentError when not correct reused instance' do
14
- lambda{
15
- List.new("foo")
16
- }.should raise_error(ArgumentError)
17
- end
18
-
19
- it 'raises an ArgumentError when the predicate is not satisfied' do
20
- lambda{
21
- Tuple.new("foo" => :bar)
22
- }.should raise_error(ArgumentError)
23
- end
24
-
25
13
  end
26
14
  end
@@ -6,16 +6,16 @@ module Domain
6
6
  NegInt.new(-12).should eq(-12)
7
7
  end
8
8
 
9
- it 'raises an argument error when the value does not satisfy the predicate' do
9
+ it 'raises a type error when the value does not satisfy the predicate' do
10
10
  lambda{
11
11
  NegInt.new(12)
12
- }.should raise_error(ArgumentError)
12
+ }.should raise_error(TypeError)
13
13
  end
14
14
 
15
- it 'raises an argument error when the value is not a superclazz value' do
15
+ it 'raises a type error when the value is not a superclazz value' do
16
16
  lambda{
17
17
  NegInt.new("bla")
18
- }.should raise_error(ArgumentError)
18
+ }.should raise_error(TypeError)
19
19
  end
20
20
 
21
21
  end
@@ -7,10 +7,10 @@ module Domain
7
7
  Boolean.new(false).should be(false)
8
8
  end
9
9
 
10
- it 'raises an argument error when the value is not a superclazz value' do
10
+ it 'raises a type when the value is not a superclazz value' do
11
11
  lambda{
12
12
  Boolean.new("12")
13
- }.should raise_error(ArgumentError)
13
+ }.should raise_error(TypeError)
14
14
  end
15
15
 
16
16
  end
@@ -0,0 +1,12 @@
1
+ module OrderedSet
2
+ extend Domain::Reuse::Helpers
3
+
4
+ reuse :size
5
+ recoat :map
6
+ end
7
+
8
+ class AttrList
9
+ extend Domain::Reuse.new(Array)
10
+ include OrderedSet
11
+
12
+ end
@@ -2,7 +2,6 @@ shared_examples_for 'a domain class' do
2
2
 
3
3
  it{ should be_a(Class) }
4
4
 
5
- it{ should respond_to(:predicate) }
6
5
  it{ should respond_to(:sub_domains) }
7
6
  it{ should respond_to(:super_domain) }
8
7
 
data/spec/spec_helper.rb CHANGED
@@ -9,6 +9,7 @@ require_relative 'fixtures/boolean'
9
9
  require_relative 'fixtures/point'
10
10
  require_relative 'fixtures/list'
11
11
  require_relative 'fixtures/tuple'
12
+ require_relative 'fixtures/attr_list'
12
13
 
13
14
  module Domain
14
15
 
@@ -19,3 +19,13 @@ describe List do
19
19
  subject{ List }
20
20
  it_should_behave_like 'a domain class'
21
21
  end
22
+
23
+ describe AttrList do
24
+ subject{ AttrList }
25
+
26
+ it_should_behave_like 'a domain class'
27
+
28
+ it 'should have OrderedSet helpers' do
29
+ AttrList.new([1, 2, 3]).map{|x| x*2}.should eq(AttrList.new([2, 4, 6]))
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domain
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc1
4
+ version: 1.0.0.rc2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-27 00:00:00.000000000 Z
12
+ date: 2012-09-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -66,9 +66,8 @@ files:
66
66
  - lib/domain/factory/union.rb
67
67
  - lib/domain/factory.rb
68
68
  - lib/domain/loader.rb
69
+ - lib/domain/support/domain_factory.rb
69
70
  - lib/domain/support/equalizer.rb
70
- - lib/domain/support/fake_domain.rb
71
- - lib/domain/support/impl_domain.rb
72
71
  - lib/domain/support.rb
73
72
  - lib/domain/version.rb
74
73
  - lib/domain.rb
@@ -76,6 +75,7 @@ files:
76
75
  - Manifest.txt
77
76
  - Rakefile
78
77
  - README.md
78
+ - spec/factory/reuse_domain/test_domain_check.rb
79
79
  - spec/factory/reuse_domain/test_new.rb
80
80
  - spec/factory/reuse_domain/test_predicate.rb
81
81
  - spec/factory/reuse_domain/test_recoat.rb
@@ -100,6 +100,7 @@ files:
100
100
  - spec/factory/union_domain/test_super_domain.rb
101
101
  - spec/factory/union_domain/test_super_domain_of.rb
102
102
  - spec/factory/union_domain/test_triple_equal.rb
103
+ - spec/fixtures/attr_list.rb
103
104
  - spec/fixtures/boolean.rb
104
105
  - spec/fixtures/list.rb
105
106
  - spec/fixtures/neg_int.rb
@@ -139,6 +140,7 @@ signing_key:
139
140
  specification_version: 3
140
141
  summary: Provide tools for implementing domains (aka data types)
141
142
  test_files:
143
+ - spec/factory/reuse_domain/test_domain_check.rb
142
144
  - spec/factory/reuse_domain/test_new.rb
143
145
  - spec/factory/reuse_domain/test_predicate.rb
144
146
  - spec/factory/reuse_domain/test_recoat.rb
@@ -163,6 +165,7 @@ test_files:
163
165
  - spec/factory/union_domain/test_super_domain.rb
164
166
  - spec/factory/union_domain/test_super_domain_of.rb
165
167
  - spec/factory/union_domain/test_triple_equal.rb
168
+ - spec/fixtures/attr_list.rb
166
169
  - spec/fixtures/boolean.rb
167
170
  - spec/fixtures/list.rb
168
171
  - spec/fixtures/neg_int.rb
@@ -1,38 +0,0 @@
1
- module Domain
2
- module FakeDomain
3
-
4
- def self.new(super_domain, sub_domains, predicate)
5
- Module.new{
6
- include Domain, Methods
7
- define_method(:super_domain){ super_domain }
8
- define_method(:sub_domains) { sub_domains }
9
- define_method(:predicate) { predicate }
10
- }
11
- end
12
-
13
- module Methods
14
-
15
- # Creates a new instance of this domain
16
- def new(*args)
17
- if (args.size == 1) && self===args.first
18
- return args.first
19
- elsif superclass.respond_to?(:new) && (superclass != Object)
20
- return new super(*args)
21
- end
22
- args_error_on_new(args)
23
- end
24
-
25
- # Checks if `value` belongs to this domain
26
- def ===(value)
27
- value.is_a?(superclass) && predicate && predicate.call(value)
28
- end
29
-
30
- private
31
-
32
- def args_error_on_new(args)
33
- raise ArgumentError, "Invalid value #{args.join(' ')} for #{self}", caller
34
- end
35
-
36
- end # module Methods
37
- end # module FakeDomain
38
- end # module Domain
@@ -1,17 +0,0 @@
1
- module Domain
2
- module ImplDomain
3
-
4
- def self.new(c_methods = [], i_methods = [])
5
- i_methods = Array(i_methods)
6
- c_methods = Array(c_methods).unshift(Domain)
7
- Module.new{
8
- c_methods.each{|c_m| include(c_m)}
9
- define_singleton_method(:extend_object) do |obj|
10
- obj.module_eval{ i_methods.each{|i_m| include(i_m)} } if obj.is_a?(Class)
11
- super(obj)
12
- end
13
- }
14
- end
15
-
16
- end # module ImplDomain
17
- end # module Domain