striuct 0.2.3 → 0.3.0

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/History.rdoc CHANGED
@@ -1,3 +1,8 @@
1
+ === 0.3.0 2012-4-3
2
+
3
+ * modify:
4
+ * use validation library (spin-off library)
5
+
1
6
  === 0.2.3 2012-4-2
2
7
 
3
8
  * fix:
data/Manifest.txt CHANGED
@@ -7,15 +7,12 @@ LICENSE
7
7
  lib/striuct.rb
8
8
  lib/striuct/version.rb
9
9
  lib/striuct/abstract.rb
10
- lib/striuct/conditions.rb
11
- lib/striuct/flavors.rb
12
10
  lib/striuct/containable.rb
13
11
  lib/striuct/containable/classutil.rb
14
12
  lib/striuct/containable/basic.rb
15
13
  lib/striuct/containable/safety.rb
16
14
  lib/striuct/containable/handy.rb
17
15
  lib/striuct/containable/hashlike.rb
18
- lib/striuct/containable/yaml.rb
19
16
  lib/striuct/containable/inner.rb
20
17
  lib/striuct/containable/eigen.rb
21
18
  lib/striuct/containable/eigen/basic.rb
data/Rakefile CHANGED
@@ -13,6 +13,7 @@ $hoe = Hoe.spec 'striuct' do
13
13
  developer 'Kenichi Kamiya', 'kachick1+ruby@gmail.com'
14
14
  self.rubyforge_name = self.name
15
15
  require_ruby_version '>= 1.9.2'
16
+ dependency 'validation', '~> 0.0.3', :runtime
16
17
  dependency 'yard', '~> 0.7.5', :development
17
18
  end
18
19
 
@@ -63,7 +63,7 @@ class Striuct; module Containable
63
63
  def []=(key, value)
64
64
  true_name = nil
65
65
  __subscript__(key){|name|true_name = name; __set__ name, value}
66
- rescue ConditionError
66
+ rescue Validation::InvalidWritingError
67
67
  $@ = [
68
68
  "#{$@[-1].sub(/[^:]+\z/){''}}in `[#{key.inspect}(#{true_name})]=': #{$!.message}",
69
69
  $@[-1]
@@ -47,7 +47,7 @@ class Striuct; module Containable; module Eigen
47
47
  if strict &&
48
48
  ! (invalids = each_name.select{|name|! instance.valid?(name)}).empty?
49
49
 
50
- raise ConditionError, "invalids members are, yet '#{invalids.inspect} in #{self}'"
50
+ raise Validation::InvalidWritingError, "invalids members are, yet '#{invalids.inspect} in #{self}'"
51
51
  end
52
52
 
53
53
  instance.lock if lock
@@ -145,7 +145,7 @@ class Striuct; module Containable; module Eigen
145
145
  end
146
146
 
147
147
  def __setter__!(name, condition, &flavor)
148
- __set_condition__! name, condition unless ANYTHING.equal? condition
148
+ __set_condition__! name, condition unless Validation::Condition::ANYTHING.equal? condition
149
149
  __set_flavor__! name, &flavor if block_given?
150
150
 
151
151
  define_method "#{name}=" do |value|
@@ -157,7 +157,7 @@ class Striuct; module Containable; module Eigen
157
157
  end
158
158
 
159
159
  def __set_condition__!(name, condition)
160
- if conditionable? condition
160
+ if ::Validation.conditionable? condition
161
161
  _set_condition name, condition
162
162
  else
163
163
  raise TypeError, 'wrong object for condition'
@@ -167,7 +167,7 @@ class Striuct; module Containable; module Eigen
167
167
  end
168
168
 
169
169
  def __set_flavor__!(name, &flavor)
170
- if (arity = flavor.arity) == 1
170
+ if ::Validation.adjustable? flavor
171
171
  _set_flavor name, flavor
172
172
  else
173
173
  raise ArgumentError, "wrong number of block argument #{arity} for 1"
@@ -184,7 +184,7 @@ class Striuct; module Containable; module Eigen
184
184
  member?(name) and
185
185
  _caller.instance_of?(self)
186
186
 
187
- raise ArgumentError unless conditionable? family
187
+ raise ArgumentError unless Validation.conditionable? family
188
188
 
189
189
  _set_condition name, family
190
190
  _remove_inference name
@@ -29,7 +29,7 @@ class Striuct; module Containable; module Eigen
29
29
  # @param [#===, Proc, Method, ANYTHING] condition
30
30
  # @param [Hash] options
31
31
  # @return [nil]
32
- def add_member(name, condition=ANYTHING, options=DEFAULT_MEMBER_OPTIONS, &flavor)
32
+ def add_member(name, condition=Validation::Condition::ANYTHING, options=DEFAULT_MEMBER_OPTIONS, &flavor)
33
33
  raise "already closed to add member in #{self}" if closed?
34
34
  options = DEFAULT_MEMBER_OPTIONS.merge options
35
35
  raise ArgumentError, 'invalid option parameter is' unless (options.keys - VALID_MEMBER_OPTIONS).empty?
@@ -1,13 +1,7 @@
1
- require_relative '../conditions'
2
- require_relative '../flavors'
3
-
4
1
  class Striuct; module Containable
5
2
 
6
3
  # @author Kenichi Kamiya
7
- module Eigen
8
- include Conditions
9
- include Flavors
10
-
4
+ module Eigen
11
5
  NAMING_RISKS = {
12
6
  conflict: 10,
13
7
  no_identifier: 9,
@@ -17,7 +17,7 @@ class Striuct; module Containable
17
17
  value = @db[name]
18
18
 
19
19
  if safety_getter?(name) and !accept?(name, value)
20
- raise ConditionError,
20
+ raise ::Validation::InvalidReadingError,
21
21
  "#{value.inspect} is deficient for #{name} in #{self.class}"
22
22
  end
23
23
 
@@ -33,12 +33,12 @@ class Striuct; module Containable
33
33
  begin
34
34
  value = instance_exec value, &flavor_for(name)
35
35
  rescue Exception
36
- raise ConditionError
36
+ raise ::Validation::UnmanagebleError
37
37
  end
38
38
  end
39
39
 
40
40
  if safety_setter?(name) and !accept?(name, value)
41
- raise ConditionError,
41
+ raise ::Validation::InvalidWritingError,
42
42
  "#{value.inspect} is deficient for #{name} in #{self.class}"
43
43
  end
44
44
 
@@ -47,7 +47,7 @@ class Striuct; module Containable
47
47
  end
48
48
 
49
49
  @db[name] = value
50
- rescue ConditionError
50
+ rescue ::Validation::InvalidError
51
51
  unless /in \[\]=/ =~ caller[1].slice(/([^:]+)\z/)
52
52
  $@.delete_if{|s|/#{Regexp.escape(File.dirname __FILE__)}/ =~ s}
53
53
  $@.first.sub!(/([^:]+)\z/){"in `#{name}='"}
@@ -5,21 +5,6 @@ class Striuct; module Containable
5
5
  delegate_class_methods :restrict?, :has_condition?,
6
6
  :safety_getter?, :safety_reader?, :safety_setter?, :safty_writer?, :inference?
7
7
 
8
- # @param [Object] value
9
- # @param [Proc, Method, #===] condition
10
- def pass?(value, condition)
11
- case condition
12
- when Conditions::ANYTHING
13
- true
14
- when Proc
15
- instance_exec value, &condition
16
- when Method
17
- condition.call value
18
- else
19
- condition === value
20
- end ? true : false
21
- end
22
-
23
8
  # @param [Symbol, String] name
24
9
  # @param [Object] value - no argument and use own
25
10
  # passed under any condition
@@ -27,7 +12,7 @@ class Striuct; module Containable
27
12
  name = originalkey_for(keyable_for name)
28
13
  return true unless restrict? name
29
14
 
30
- pass? value, condition_for(name)
15
+ _valid? condition_for(name), value
31
16
  end
32
17
 
33
18
  alias_method :accept?, :sufficient?
@@ -35,5 +35,4 @@ require_relative 'containable/basic'
35
35
  require_relative 'containable/safety'
36
36
  require_relative 'containable/handy'
37
37
  require_relative 'containable/hashlike'
38
- require_relative 'containable/yaml'
39
38
  require_relative 'containable/inner'
@@ -1,4 +1,4 @@
1
1
  class Striuct
2
- VERSION = '0.2.3'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  Version = VERSION
4
4
  end
data/lib/striuct.rb CHANGED
@@ -2,11 +2,13 @@
2
2
  # Striuct
3
3
  # Provides a Struct++ class.
4
4
 
5
+ require 'validation'
6
+
5
7
  # @author Kenichi Kamiya
6
8
  # @abstract
7
9
  class Striuct
8
- class ConditionError < ArgumentError; end
9
-
10
+ include Validation
11
+
10
12
  # namespace for .to_struct_class, #to_struct
11
13
  module Structs
12
14
  end
data/test/test_helper.rb CHANGED
@@ -1,3 +1,2 @@
1
- require 'stringio'
2
1
  require 'test/unit'
3
- require File.dirname(__FILE__) + '/../lib/striuct'
2
+ require_relative '../lib/striuct'
data/test/test_striuct.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  $VERBOSE = true
2
- require File.dirname(__FILE__) + '/test_helper.rb'
3
-
4
- class User < Striuct.new
5
- member :id, Integer
6
- member :last_name, /\A\w+\z/
7
- member :family_name, /\A\w+\z/
8
- member :address, /\A((\w+) ?)+\z/
9
- member :age, ->age{(20..140).include? age}
10
- end
2
+ require_relative 'test_helper'
11
3
 
12
4
  class TestStriuctSubclassEigen < Test::Unit::TestCase
5
+ class User < Striuct.new
6
+ member :id, Integer
7
+ member :last_name, /\A\w+\z/
8
+ member :family_name, /\A\w+\z/
9
+ member :address, /\A((\w+) ?)+\z/
10
+ member :age, ->age{(20..140).include? age}
11
+ end
12
+
13
13
  User2 = Striuct.define do
14
14
  member :name, AND(String, NOT(''))
15
15
  member :age, Fixnum
@@ -55,7 +55,7 @@ class TestStriuctSubclassEigen < Test::Unit::TestCase
55
55
  assert_same false, user.lock?
56
56
  assert_equal true, user.strict?
57
57
 
58
- assert_raises Striuct::ConditionError do
58
+ assert_raises Validation::InvalidWritingError do
59
59
  User2.define{|r|r.age = 1; r.name = 'a'; r.name.clear}
60
60
  end
61
61
 
@@ -87,6 +87,14 @@ class TestStriuctSubclassEigen < Test::Unit::TestCase
87
87
  end
88
88
 
89
89
  class TestStriuctSubclassInstance1 < Test::Unit::TestCase
90
+ class User < Striuct.new
91
+ member :id, Integer
92
+ member :last_name, /\A\w+\z/
93
+ member :family_name, /\A\w+\z/
94
+ member :address, /\A((\w+) ?)+\z/
95
+ member :age, ->age{(20..140).include? age}
96
+ end
97
+
90
98
  def test_setter
91
99
  user = User.new
92
100
  user[:last_name] = 'foo'
@@ -94,15 +102,15 @@ class TestStriuctSubclassInstance1 < Test::Unit::TestCase
94
102
  user.last_name = 'bar'
95
103
  assert_equal user[:last_name], 'bar'
96
104
 
97
- assert_raises Striuct::ConditionError do
105
+ assert_raises Validation::InvalidWritingError do
98
106
  user[:last_name] = 'foo s'
99
107
  end
100
108
 
101
- assert_raises Striuct::ConditionError do
109
+ assert_raises Validation::InvalidWritingError do
102
110
  User.new 'asda'
103
111
  end
104
112
 
105
- assert_raises Striuct::ConditionError do
113
+ assert_raises Validation::InvalidWritingError do
106
114
  user.age = 19
107
115
  end
108
116
  end
@@ -117,6 +125,14 @@ class TestStriuctSubclassInstance1 < Test::Unit::TestCase
117
125
  end
118
126
 
119
127
  class TestStriuctSubclassInstance2 < Test::Unit::TestCase
128
+ class User < Striuct.new
129
+ member :id, Integer
130
+ member :last_name, /\A\w+\z/
131
+ member :family_name, /\A\w+\z/
132
+ member :address, /\A((\w+) ?)+\z/
133
+ member :age, ->age{(20..140).include? age}
134
+ end
135
+
120
136
  def setup
121
137
  @user = User.new 9999, 'taro', 'yamada', 'Tokyo Japan', 30
122
138
  end
@@ -137,15 +153,15 @@ class TestStriuctSubclassInstance2 < Test::Unit::TestCase
137
153
  end
138
154
 
139
155
  def test_setter_fail
140
- assert_raises Striuct::ConditionError do
156
+ assert_raises Validation::InvalidWritingError do
141
157
  @user.id = 2139203509295.0
142
158
  end
143
159
 
144
- assert_raises Striuct::ConditionError do
160
+ assert_raises Validation::InvalidWritingError do
145
161
  @user.last_name = 'ignore name'
146
162
  end
147
163
 
148
- assert_raises Striuct::ConditionError do
164
+ assert_raises Validation::InvalidWritingError do
149
165
  @user.age = 19
150
166
  end
151
167
  end
@@ -161,6 +177,14 @@ class TestStriuctSubclassInstance2 < Test::Unit::TestCase
161
177
  end
162
178
 
163
179
  class TestStriuctSubclassInstance3 < Test::Unit::TestCase
180
+ class User < Striuct.new
181
+ member :id, Integer
182
+ member :last_name, /\A\w+\z/
183
+ member :family_name, /\A\w+\z/
184
+ member :address, /\A((\w+) ?)+\z/
185
+ member :age, ->age{(20..140).include? age}
186
+ end
187
+
164
188
  def setup
165
189
  @user = User.new 9999, 'taro', 'yamada', 'Tokyo Japan', 30
166
190
  @user2 = User.new 9999, 'taro', 'yamada', 'Tokyo Japan', 30
@@ -282,7 +306,7 @@ class TestStriuctSubclassInstance4 < Test::Unit::TestCase
282
306
  @sth.bool = false
283
307
  assert_same false, @sth.bool
284
308
 
285
- assert_raises Striuct::ConditionError do
309
+ assert_raises Validation::InvalidWritingError do
286
310
  @sth.bool = nil
287
311
  end
288
312
 
@@ -295,11 +319,11 @@ class TestStriuctSubclassInstance4 < Test::Unit::TestCase
295
319
  @sth.sth = Class.class
296
320
  assert_same Class.class, @sth.sth
297
321
 
298
- assert_raises Striuct::ConditionError do
322
+ assert_raises Validation::InvalidWritingError do
299
323
  @sth.lambda = 9
300
324
  end
301
325
 
302
- assert_raises Striuct::ConditionError do
326
+ assert_raises Validation::InvalidWritingError do
303
327
  @sth.lambda = 7
304
328
  end
305
329
 
@@ -359,7 +383,7 @@ class TestStriuctProcedure < Test::Unit::TestCase
359
383
  @sth.age = 10.0
360
384
  assert_same 10, @sth.age
361
385
 
362
- assert_raises Striuct::ConditionError do
386
+ assert_raises Validation::UnmanagebleError do
363
387
  @sth.age = '10.0'
364
388
  end
365
389
 
@@ -396,7 +420,7 @@ class TestStriuctDefaultValue < Test::Unit::TestCase
396
420
  default :lank2, '10'
397
421
  end
398
422
 
399
- assert_raises Striuct::ConditionError do
423
+ assert_raises Validation::InvalidWritingError do
400
424
  klass.new
401
425
  end
402
426
 
@@ -419,7 +443,7 @@ class TestStriuctDefaultValue < Test::Unit::TestCase
419
443
  default :lank, &->own, name{(seef = own); rand}
420
444
  end
421
445
 
422
- assert_raises Striuct::ConditionError do
446
+ assert_raises Validation::InvalidWritingError do
423
447
  klass.new
424
448
  end
425
449
 
@@ -445,7 +469,7 @@ class TestStriuctFunctionalCondition < Test::Unit::TestCase
445
469
  sth.lank = 2
446
470
  assert_equal 2, sth.lank
447
471
 
448
- assert_raises Striuct::ConditionError do
472
+ assert_raises Validation::InvalidWritingError do
449
473
  sth.lank = 31
450
474
  end
451
475
  end
@@ -461,7 +485,7 @@ class TestStriuctFunctionalCondition < Test::Unit::TestCase
461
485
  sth.lank = 8
462
486
  assert_equal 8, sth.lank
463
487
 
464
- assert_raises Striuct::ConditionError do
488
+ assert_raises Validation::InvalidWritingError do
465
489
  sth.lank = 2
466
490
  end
467
491
  end
@@ -475,7 +499,7 @@ class TestStriuctFunctionalCondition < Test::Unit::TestCase
475
499
  sth.lank = 8
476
500
  assert_equal 8, sth.lank
477
501
 
478
- assert_raises Striuct::ConditionError do
502
+ assert_raises Validation::InvalidWritingError do
479
503
  sth.lank = 6
480
504
  end
481
505
  end
@@ -645,12 +669,12 @@ class TestStriuctInference < Test::Unit::TestCase
645
669
  def test_inference
646
670
  klass = Striuct.define do
647
671
  member :n, Numeric, inference: true
648
- member :m, anything, inference: true
672
+ member :m, ANYTHING?, inference: true
649
673
  end
650
674
 
651
675
  sth, sth2 = klass.new, klass.new
652
676
 
653
- assert_raises Striuct::ConditionError do
677
+ assert_raises Validation::InvalidWritingError do
654
678
  sth.n = '1'
655
679
  end
656
680
 
@@ -658,11 +682,11 @@ class TestStriuctInference < Test::Unit::TestCase
658
682
 
659
683
  assert_equal 1.1, sth.n
660
684
 
661
- assert_raises Striuct::ConditionError do
685
+ assert_raises Validation::InvalidWritingError do
662
686
  sth.n = 1
663
687
  end
664
688
 
665
- assert_raises Striuct::ConditionError do
689
+ assert_raises Validation::InvalidWritingError do
666
690
  sth2.n = 1
667
691
  end
668
692
 
@@ -675,11 +699,11 @@ class TestStriuctInference < Test::Unit::TestCase
675
699
 
676
700
  assert_equal 1, sth2.m
677
701
 
678
- assert_raises Striuct::ConditionError do
702
+ assert_raises Validation::InvalidWritingError do
679
703
  sth.m = 1.0
680
704
  end
681
705
 
682
- assert_raises Striuct::ConditionError do
706
+ assert_raises Validation::InvalidWritingError do
683
707
  sth2.m = 1.0
684
708
  end
685
709
 
@@ -894,15 +918,15 @@ class TestStriuctAliasMember < Test::Unit::TestCase
894
918
  sth[:abc] = 6
895
919
  assert_equal 6, sth.bar
896
920
 
897
- assert_raises Striuct::ConditionError do
921
+ assert_raises Validation::InvalidWritingError do
898
922
  sth[:abc] = 'a'
899
923
  end
900
924
 
901
- assert_raises Striuct::ConditionError do
925
+ assert_raises Validation::InvalidWritingError do
902
926
  sth.abc = 'a'
903
927
  end
904
928
 
905
- assert_raises Striuct::ConditionError do
929
+ assert_raises Validation::InvalidWritingError do
906
930
  sth.bar = 'a'
907
931
  end
908
932
 
@@ -917,16 +941,16 @@ end
917
941
  class TestStriuctSpecificConditions < Test::Unit::TestCase
918
942
  Sth = Striuct.define do
919
943
  member :list_only_int, GENERICS(Integer)
920
- member :true_or_false, boolean
921
- member :like_str, stringable
944
+ member :true_or_false, BOOL?
945
+ member :like_str, STRINGABLE?
922
946
  member :has_x, CAN(:x)
923
947
  member :has_x_and_y, CAN(:x, :y)
924
948
  member :one_of_member, MEMBER_OF([1, 3])
925
949
  member :has_ignore, AND(1..5, 3..10)
926
950
  member :nand, NAND(1..5, 3..10)
927
951
  member :all_pass, OR(1..5, 3..10)
928
- member :catch_name_error, CATCH(NameError){|v|v.no_name!}
929
- member :no_exception, STILL(->v{v.class})
952
+ member :catch_error, CATCH(NoMethodError){|v|v.no_name!}
953
+ member :no_exception, QUIET(->v{v.class})
930
954
  member :not_integer, NOT(Integer)
931
955
  end
932
956
 
@@ -938,7 +962,7 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
938
962
  sth.not_integer = obj
939
963
  assert_same obj, sth.not_integer
940
964
 
941
- assert_raises Striuct::ConditionError do
965
+ assert_raises Validation::InvalidWritingError do
942
966
  sth.not_integer = 1
943
967
  end
944
968
  end
@@ -957,7 +981,7 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
957
981
  undef_method :class
958
982
  end
959
983
 
960
- assert_raises Striuct::ConditionError do
984
+ assert_raises Validation::InvalidWritingError do
961
985
  sth.no_exception = obj
962
986
  end
963
987
  end
@@ -967,24 +991,24 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
967
991
 
968
992
  obj = Object.new
969
993
 
970
- sth.catch_name_error = obj
971
- assert_same obj, sth.catch_name_error
972
- sth.catch_name_error = false
994
+ sth.catch_error = obj
995
+ assert_same obj, sth.catch_error
996
+ sth.catch_error = false
973
997
 
974
998
  obj.singleton_class.class_eval do
975
999
  def no_name!
976
1000
  end
977
1001
  end
978
1002
 
979
- assert_raises Striuct::ConditionError do
980
- sth.catch_name_error = obj
1003
+ assert_raises Validation::InvalidWritingError do
1004
+ sth.catch_error = obj
981
1005
  end
982
1006
  end
983
1007
 
984
1008
  def test_or
985
1009
  sth = Sth.new
986
1010
 
987
- assert_raises Striuct::ConditionError do
1011
+ assert_raises Validation::InvalidWritingError do
988
1012
  sth.all_pass = 11
989
1013
  end
990
1014
 
@@ -998,11 +1022,11 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
998
1022
  def test_and
999
1023
  sth = Sth.new
1000
1024
 
1001
- assert_raises Striuct::ConditionError do
1025
+ assert_raises Validation::InvalidWritingError do
1002
1026
  sth.has_ignore = 1
1003
1027
  end
1004
1028
 
1005
- assert_raises Striuct::ConditionError do
1029
+ assert_raises Validation::InvalidWritingError do
1006
1030
  sth.has_ignore= 2
1007
1031
  end
1008
1032
 
@@ -1010,7 +1034,7 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
1010
1034
  assert_equal 3, sth.has_ignore
1011
1035
  assert_equal true, sth.valid?(:has_ignore)
1012
1036
 
1013
- assert_raises Striuct::ConditionError do
1037
+ assert_raises Validation::InvalidWritingError do
1014
1038
  sth.has_ignore = []
1015
1039
  end
1016
1040
  end
@@ -1018,11 +1042,11 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
1018
1042
  def test_nand
1019
1043
  sth = Sth.new
1020
1044
 
1021
- assert_raises Striuct::ConditionError do
1045
+ assert_raises Validation::InvalidWritingError do
1022
1046
  sth.nand = 4
1023
1047
  end
1024
1048
 
1025
- assert_raises Striuct::ConditionError do
1049
+ assert_raises Validation::InvalidWritingError do
1026
1050
  sth.nand = 4.5
1027
1051
  end
1028
1052
 
@@ -1038,7 +1062,7 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
1038
1062
  def test_member_of
1039
1063
  sth = Sth.new
1040
1064
 
1041
- assert_raises Striuct::ConditionError do
1065
+ assert_raises Validation::InvalidWritingError do
1042
1066
  sth.one_of_member = 4
1043
1067
  end
1044
1068
 
@@ -1050,7 +1074,7 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
1050
1074
  def test_generics
1051
1075
  sth = Sth.new
1052
1076
 
1053
- assert_raises Striuct::ConditionError do
1077
+ assert_raises Validation::InvalidWritingError do
1054
1078
  sth.list_only_int = [1, '2']
1055
1079
  end
1056
1080
 
@@ -1067,7 +1091,7 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
1067
1091
  def test_boolean
1068
1092
  sth = Sth.new
1069
1093
 
1070
- assert_raises Striuct::ConditionError do
1094
+ assert_raises Validation::InvalidWritingError do
1071
1095
  sth.true_or_false = nil
1072
1096
  end
1073
1097
 
@@ -1085,7 +1109,7 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
1085
1109
  sth = Sth.new
1086
1110
  obj = Object.new
1087
1111
 
1088
- assert_raises Striuct::ConditionError do
1112
+ assert_raises Validation::InvalidWritingError do
1089
1113
  sth.like_str = obj
1090
1114
  end
1091
1115
 
@@ -1107,7 +1131,7 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
1107
1131
  sth = Sth.new
1108
1132
  obj = Object.new
1109
1133
 
1110
- assert_raises Striuct::ConditionError do
1134
+ assert_raises Validation::InvalidWritingError do
1111
1135
  sth.has_x = obj
1112
1136
  end
1113
1137
 
@@ -1125,7 +1149,7 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
1125
1149
  sth = Sth.new
1126
1150
  obj = Object.new
1127
1151
 
1128
- assert_raises Striuct::ConditionError do
1152
+ assert_raises Validation::InvalidWritingError do
1129
1153
  sth.has_x_and_y = obj
1130
1154
  end
1131
1155
 
@@ -1134,7 +1158,7 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
1134
1158
  end
1135
1159
  end
1136
1160
 
1137
- assert_raises Striuct::ConditionError do
1161
+ assert_raises Validation::InvalidWritingError do
1138
1162
  sth.has_x_and_y = obj
1139
1163
  end
1140
1164
 
@@ -1206,11 +1230,11 @@ class TestStriuctInherit < Test::Unit::TestCase
1206
1230
  sth = Sth.new
1207
1231
  substh = SubSth.new
1208
1232
 
1209
- assert_raises Striuct::ConditionError do
1233
+ assert_raises Validation::InvalidWritingError do
1210
1234
  substh.bar = 'str'
1211
1235
  end
1212
1236
 
1213
- assert_raises Striuct::ConditionError do
1237
+ assert_raises Validation::InvalidWritingError do
1214
1238
  substh.hoge = 4
1215
1239
  end
1216
1240
 
@@ -1224,7 +1248,7 @@ class TestStriuctInherit < Test::Unit::TestCase
1224
1248
 
1225
1249
  subsubsth = SubSubSth.new
1226
1250
 
1227
- assert_raises Striuct::ConditionError do
1251
+ assert_raises Validation::InvalidWritingError do
1228
1252
  subsubsth.rest = 4
1229
1253
  end
1230
1254
 
@@ -1244,7 +1268,7 @@ end
1244
1268
 
1245
1269
  class TestStriuctConstants < Test::Unit::TestCase
1246
1270
  def test_const_version
1247
- assert_equal '0.2.3', Striuct::VERSION
1271
+ assert_equal '0.3.0', Striuct::VERSION
1248
1272
  assert_equal true, Striuct::VERSION.frozen?
1249
1273
  assert_same Striuct::VERSION, Striuct::Version
1250
1274
  end
@@ -1302,7 +1326,7 @@ class TestStriuctFlavors < Test::Unit::TestCase
1302
1326
  Sth = Striuct.define do
1303
1327
  member :chomped, AND(Symbol, /[^\n]\z/), &WHEN(String, ->v{v.chomp.to_sym})
1304
1328
  member :no_reduced, Symbol, &->v{v.to_sym}
1305
- member :reduced, Symbol, &FLAVORS(->v{v.to_s}, ->v{v.to_sym})
1329
+ member :reduced, Symbol, &INJECT(->v{v.to_s}, ->v{v.to_sym})
1306
1330
  member :integer, Integer, &PARSE(Integer)
1307
1331
  member :myobj, ->v{v.instance_of? MyClass}, &PARSE(MyClass)
1308
1332
  end
@@ -1310,7 +1334,7 @@ class TestStriuctFlavors < Test::Unit::TestCase
1310
1334
  def test_WHEN
1311
1335
  sth = Sth.new
1312
1336
 
1313
- assert_raises Striuct::ConditionError do
1337
+ assert_raises Validation::InvalidWritingError do
1314
1338
  sth.chomped = :"a\n"
1315
1339
  end
1316
1340
 
@@ -1322,10 +1346,10 @@ class TestStriuctFlavors < Test::Unit::TestCase
1322
1346
  assert_equal :b, sth.chomped
1323
1347
  end
1324
1348
 
1325
- def test_REDUCE
1349
+ def test_INJECT
1326
1350
  sth = Sth.new
1327
1351
 
1328
- assert_raises Striuct::ConditionError do
1352
+ assert_raises Validation::UnmanagebleError do
1329
1353
  sth.no_reduced = 1
1330
1354
  end
1331
1355
 
@@ -1337,7 +1361,7 @@ class TestStriuctFlavors < Test::Unit::TestCase
1337
1361
  def test_PARSE
1338
1362
  sth = Sth.new
1339
1363
 
1340
- assert_raises Striuct::ConditionError do
1364
+ assert_raises Validation::UnmanagebleError do
1341
1365
  sth.integer = '1.0'
1342
1366
  end
1343
1367
 
@@ -1345,7 +1369,7 @@ class TestStriuctFlavors < Test::Unit::TestCase
1345
1369
 
1346
1370
  assert_equal 1, sth.integer
1347
1371
 
1348
- assert_raises Striuct::ConditionError do
1372
+ assert_raises Validation::UnmanagebleError do
1349
1373
  sth.myobj = '1'
1350
1374
  end
1351
1375
 
@@ -1365,7 +1389,7 @@ class TestGetterValidation < Test::Unit::TestCase
1365
1389
  def test_getter_validation
1366
1390
  sth = Sth.new
1367
1391
 
1368
- assert_raises Striuct::ConditionError do
1392
+ assert_raises Validation::InvalidWritingError do
1369
1393
  sth.plus_getter = ''
1370
1394
  end
1371
1395
 
@@ -1373,13 +1397,13 @@ class TestGetterValidation < Test::Unit::TestCase
1373
1397
  assert_equal 'abc', sth.plus_getter
1374
1398
  sth.plus_getter.clear
1375
1399
 
1376
- assert_raises Striuct::ConditionError do
1400
+ assert_raises Validation::InvalidReadingError do
1377
1401
  sth.plus_getter
1378
1402
  end
1379
1403
 
1380
1404
  sth.only_getter = ''
1381
1405
 
1382
- assert_raises Striuct::ConditionError do
1406
+ assert_raises Validation::InvalidReadingError do
1383
1407
  sth.only_getter
1384
1408
  end
1385
1409
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: striuct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-01 00:00:00.000000000 Z
12
+ date: 2012-04-02 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: validation
16
+ requirement: &25171236 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *25171236
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: rdoc
16
- requirement: &23052000 !ruby/object:Gem::Requirement
27
+ requirement: &25170780 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ~>
@@ -21,10 +32,10 @@ dependencies:
21
32
  version: '3.10'
22
33
  type: :development
23
34
  prerelease: false
24
- version_requirements: *23052000
35
+ version_requirements: *25170780
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: yard
27
- requirement: &23051652 !ruby/object:Gem::Requirement
38
+ requirement: &25169988 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ~>
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: 0.7.5
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *23051652
46
+ version_requirements: *25169988
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: newgem
38
- requirement: &23051292 !ruby/object:Gem::Requirement
49
+ requirement: &25169484 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: 1.5.3
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *23051292
57
+ version_requirements: *25169484
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: hoe
49
- requirement: &23050932 !ruby/object:Gem::Requirement
60
+ requirement: &25168956 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ~>
@@ -54,7 +65,7 @@ dependencies:
54
65
  version: '3.0'
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *23050932
68
+ version_requirements: *25168956
58
69
  description: Struct++
59
70
  email:
60
71
  - kachick1+ruby@gmail.com
@@ -76,15 +87,12 @@ files:
76
87
  - lib/striuct.rb
77
88
  - lib/striuct/version.rb
78
89
  - lib/striuct/abstract.rb
79
- - lib/striuct/conditions.rb
80
- - lib/striuct/flavors.rb
81
90
  - lib/striuct/containable.rb
82
91
  - lib/striuct/containable/classutil.rb
83
92
  - lib/striuct/containable/basic.rb
84
93
  - lib/striuct/containable/safety.rb
85
94
  - lib/striuct/containable/handy.rb
86
95
  - lib/striuct/containable/hashlike.rb
87
- - lib/striuct/containable/yaml.rb
88
96
  - lib/striuct/containable/inner.rb
89
97
  - lib/striuct/containable/eigen.rb
90
98
  - lib/striuct/containable/eigen/basic.rb
@@ -1,269 +0,0 @@
1
- class Striuct
2
-
3
- # Condition Builders and Useful Conditions
4
- #
5
- # Any conditions need #pass?(arg, condition) method
6
- # @author Kenichi Kamiya
7
- # @example overview
8
- # class Person < Striuct
9
- # member :name, OR(String, Symbol, CAN(:to_str))
10
- # member :friends, GENERICS(AND(Person, NOT(->v{equal? v})))
11
- module Conditions
12
- ANYTHING = Object.new.freeze
13
- BOOLEAN = ->v{[true, false].any?{|bool|bool.equal? v}}
14
-
15
- module_function
16
-
17
- # The getter for a special condition.
18
- # @return [ANYTHING] A condition always pass with any objects.
19
- def ANYTHING?
20
- ANYTHING
21
- end
22
-
23
- alias_method :ANYTHING, :ANYTHING?
24
- alias_method :anything, :ANYTHING?
25
- module_function :anything, :ANYTHING
26
-
27
- # true if object is sufficient for condition.
28
- # @param [Object] object
29
- def conditionable?(object)
30
- case object
31
- when ANYTHING
32
- true
33
- when Proc, Method
34
- object.arity == 1
35
- else
36
- object.respond_to? :===
37
- end
38
- end
39
-
40
- # A condition builder.
41
- # @return [lambda] A condition invert the original condition.
42
- def NOT(condition)
43
- unless conditionable? condition
44
- raise TypeError, 'wrong object for condition'
45
- end
46
-
47
- ->v{! pass?(v, condition)}
48
- end
49
-
50
- # A innner method for some condition builders.
51
- # For build conditions AND, NAND, OR, NOR, XOR, XNOR.
52
- # @return [lambda]
53
- def _logical_operator(delegated, *conditions)
54
- unless conditions.all?{|c|conditionable? c}
55
- raise TypeError, 'wrong object for condition'
56
- end
57
-
58
- ->v{
59
- conditions.__send__(delegated) {|condition|
60
- pass? v, condition
61
- }
62
- }
63
- end
64
-
65
- # A condition builder.
66
- # @return [lambda]
67
- # this lambda return true if match all conditions
68
- def AND(cond1, cond2, *conds)
69
- _logical_operator :all?, cond1, cond2, *conds
70
- end
71
-
72
- # A condition builder.
73
- # @return [lambda]
74
- def NAND(cond1, cond2, *conds)
75
- NOT AND(cond1, cond2, *conds)
76
- end
77
-
78
- # A condition builder.
79
- # @return [lambda]
80
- # this lambda return true if match a any condition
81
- def OR(cond1, cond2, *conds)
82
- _logical_operator :any?, cond1, cond2, *conds
83
- end
84
-
85
- # A condition builder.
86
- # @return [lambda]
87
- def NOR(cond1, cond2, *conds)
88
- NOT OR(cond1, cond2, *conds)
89
- end
90
-
91
- # A condition builder.
92
- # @return [lambda]
93
- def XOR(cond1, cond2, *conds)
94
- _logical_operator :one?, cond1, cond2, *conds
95
- end
96
-
97
- alias_method :ONE, :XOR
98
- module_function :ONE
99
-
100
- # A condition builder.
101
- # @return [lambda]
102
- def XNOR(cond1, cond2, *conds)
103
- NOT XOR(cond1, cond2, *conds)
104
- end
105
-
106
- # A condition builder.
107
- # @return [lambda]
108
- # this lambda return true if a argment match under #== method
109
- def EQUAL(obj)
110
- ->v{obj == v}
111
- end
112
-
113
- # A condition builder.
114
- # @return [lambda]
115
- # this lambda return true if a argment match under #equal? method
116
- def SAME(obj)
117
- ->v{obj.equal? v}
118
- end
119
-
120
- # A condition builder.
121
- # @param [Symbol, String] messages
122
- # @return [lambda]
123
- # this lambda return true if a argment respond to all messages
124
- def RESPONSIBLE(message1, *messages)
125
- messages = [message1, *messages]
126
- unless messages.all?{|s|
127
- [Symbol, String].any?{|klass|s.kind_of? klass}
128
- }
129
- raise TypeError, 'only Symbol or String for message'
130
- end
131
-
132
- ->v{
133
- messages.all?{|message|v.respond_to? message}
134
- }
135
- end
136
-
137
- alias_method :CAN, :RESPONSIBLE
138
- alias_method :RESPOND_TO, :RESPONSIBLE
139
- alias_method :responsible, :RESPONSIBLE
140
- module_function :CAN, :responsible, :RESPOND_TO
141
-
142
- # A condition builder.
143
- # @return [lambda]
144
- # this lambda return true
145
- # if face no exception when a argment checking under all conditions
146
- def NO_RAISES(condition1, *conditions)
147
- conditions = [condition1, *conditions]
148
- unless conditions.all?{|c|conditionable? c}
149
- raise TypeError, 'wrong object for condition'
150
- end
151
-
152
- ->v{
153
- conditions.all?{|condition|
154
- begin
155
- pass? v, condition
156
- rescue Exception
157
- false
158
- else
159
- true
160
- end
161
- }
162
- }
163
- end
164
-
165
- alias_method :STILL, :NO_RAISES
166
- module_function :STILL
167
-
168
- # A condition builder.
169
- # @return [lambda]
170
- # this lambda return true
171
- # if catch a kindly exception when a argment checking in a block parameter
172
- def CATCH(exception=Exception, &condition)
173
- raise ArgumentError unless conditionable? condition
174
- raise TypeError unless exception.ancestors.include? Exception
175
-
176
- ->v{
177
- begin
178
- pass? v, condition
179
- rescue exception
180
- true
181
- rescue Exception
182
- false
183
- else
184
- false
185
- end
186
- }
187
- end
188
-
189
- alias_method :RESCUE, :CATCH
190
- module_function :RESCUE
191
-
192
- # A condition builder.
193
- # @return [lambda]
194
- # this lambda return true
195
- # if all included objects match all conditions
196
- def GENERICS(condition1, *conditions)
197
- conditions = [condition1, *conditions]
198
- unless conditions.all?{|c|conditionable? c}
199
- raise TypeError, 'wrong object for condition'
200
- end
201
-
202
- ->list{
203
- enum = (
204
- (list.respond_to?(:each_value) && list.each_value) or
205
- (list.respond_to?(:all?) && list) or
206
- (list.respond_to?(:each) && list.each) or
207
- return false
208
- )
209
-
210
- conditions.all?{|condition|
211
- enum.all?{|v|
212
- pass? v, condition
213
- }
214
- }
215
- }
216
- end
217
-
218
- alias_method :generics, :GENERICS
219
- module_function :generics
220
-
221
- # A condition builder.
222
- # @return [lambda]
223
- # this lambda return true
224
- # if all lists including the object
225
- def MEMBER_OF(list1, *lists)
226
- lists = [list1, *lists]
227
- unless lists.all?{|l|l.respond_to? :all?}
228
- raise TypeError, 'list must respond #all?'
229
- end
230
-
231
- ->v{
232
- lists.all?{|list|list.include? v}
233
- }
234
- end
235
-
236
- alias_method :member_of, :MEMBER_OF
237
- module_function :member_of
238
-
239
- # A getter for a useful condition.
240
- # @return [BOOLEAN] "true or false"
241
- def BOOLEAN?
242
- BOOLEAN
243
- end
244
-
245
- alias_method :boolean, :BOOLEAN?
246
- alias_method :bool, :BOOLEAN?
247
- alias_method :BOOLEAN, :BOOLEAN?
248
- alias_method :BOOL, :BOOLEAN?
249
- alias_method :BOOL?, :BOOLEAN?
250
- module_function :boolean, :bool, :BOOLEAN, :BOOL, :BOOL?
251
-
252
- STRINGABLE = OR(String, Symbol, CAN(:to_str), CAN(:to_sym))
253
-
254
- # A getter for a useful condition.
255
- # @return [STRINGABLE] check "looks string family"
256
- def STRINGABLE?
257
- STRINGABLE
258
- end
259
-
260
- alias_method :stringable, :STRINGABLE?
261
- alias_method :STRINGABLE, :STRINGABLE?
262
- module_function :stringable, :STRINGABLE
263
-
264
- class << self
265
- private :_logical_operator
266
- end
267
- end
268
-
269
- end
@@ -1,14 +0,0 @@
1
- autoload :YAML, 'yaml'
2
-
3
- class Striuct; module Containable
4
- # @group for YAML
5
-
6
- # @return [String]
7
- def to_yaml
8
- YAML.__id__ # for autoload
9
- klass = Struct.new(*members)
10
- klass.new(*values).to_yaml
11
- end
12
-
13
- # @endgroup
14
- end; end
@@ -1,83 +0,0 @@
1
- class Striuct
2
-
3
- # Useful Flavor Builders
4
- # @author Kenichi Kamiya
5
- # @example overview
6
- # class MyClass < Striuct
7
- # member :id, &WHEN(String, PARSE(Integer))
8
- module Flavors
9
- module_function
10
-
11
- # true if argument is sufficient for flavor.
12
- # A flavor have to be arity equal 1.
13
- # @param [Object] flavor
14
- def adjustable?(flavor)
15
- case flavor
16
- when Proc
17
- flavor.arity == 1
18
- else
19
- if flavor.respond_to?(:to_proc)
20
- flavor.to_proc.arity == 1
21
- else
22
- false
23
- end
24
- end
25
- end
26
-
27
- alias_method :flavorable?, :adjustable?
28
-
29
- # Apply flavor when passed condition.
30
- # @return [lambda]
31
- def WHEN(condition, flavor)
32
- raise TypeError, 'wrong object for condition' unless conditionable? condition
33
- raise TypeError, 'wrong object for flavor' unless adjustable? flavor
34
-
35
- ->v{pass?(v, condition) ? flavor.call(v) : v}
36
- end
37
-
38
- # Sequencial apply all flavors.
39
- # @return [lambda]
40
- def REDUCE(flavor1, flavor2, *flavors)
41
- flavors = [flavor1, flavor2, *flavors]
42
-
43
- unless flavors.all?{|f|adjustable? f}
44
- raise TypeError, 'wrong object for flavor'
45
- end
46
-
47
- ->v{
48
- flavors.reduce(v){|ret, flavor|flavor.call ret}
49
- }
50
- end
51
-
52
- alias_method :FLAVORS, :REDUCE
53
- alias_method :INJECT, :REDUCE
54
-
55
- # Accept any parser when that resopond to parse method.
56
- # @return [lambda]
57
- def PARSE(parser)
58
- if !::Integer.equal?(parser) and !parser.respond_to?(:parse)
59
- raise TypeError, 'wrong object for parser'
60
- end
61
-
62
- ->v{
63
- if ::Integer.equal? parser
64
- ::Kernel.Integer v
65
- else
66
- parser.parse(
67
- case v
68
- when String
69
- v
70
- when ->_{v.respond_to? :to_str}
71
- v.to_str
72
- when ->_{v.respond_to? :read}
73
- v.read
74
- else
75
- raise TypeError, 'wrong object for parsing source'
76
- end
77
- )
78
- end
79
- }
80
- end
81
- end
82
-
83
- end