striuct 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/History.rdoc +6 -0
  2. data/Manifest.txt +13 -7
  3. data/README.md +12 -10
  4. data/example/README.rb +5 -3
  5. data/lib/striuct/attributes.rb +114 -0
  6. data/lib/striuct/classmethods/adjustment.rb +8 -15
  7. data/lib/striuct/classmethods/copy.rb +55 -0
  8. data/lib/striuct/classmethods/default.rb +18 -13
  9. data/lib/striuct/classmethods/enum.rb +1 -3
  10. data/lib/striuct/classmethods/fix.rb +28 -0
  11. data/lib/striuct/classmethods/hashy.rb +22 -0
  12. data/lib/striuct/classmethods/inner.rb +24 -58
  13. data/lib/striuct/classmethods/macro.rb +33 -47
  14. data/lib/striuct/classmethods/named.rb +52 -40
  15. data/lib/striuct/classmethods/prevent_conflicts.rb +1 -1
  16. data/lib/striuct/classmethods/requiremnets.rb +4 -2
  17. data/lib/striuct/classmethods/validation.rb +19 -22
  18. data/lib/striuct/hashdeepdupulicatable.rb +21 -0
  19. data/lib/striuct/instancemethods/assign.rb +9 -6
  20. data/lib/striuct/instancemethods/default.rb +1 -1
  21. data/lib/striuct/instancemethods/delegate_class_methods.rb +11 -5
  22. data/lib/striuct/instancemethods/hashy.rb +1 -1
  23. data/lib/striuct/instancemethods/{inner.rb → inner_accessor.rb} +6 -13
  24. data/lib/striuct/instancemethods/lock.rb +7 -4
  25. data/lib/striuct/instancemethods/object.rb +25 -28
  26. data/lib/striuct/instancemethods/requirements.rb +3 -2
  27. data/lib/striuct/instancemethods/subscript.rb +4 -32
  28. data/lib/striuct/instancemethods/to_s.rb +35 -0
  29. data/lib/striuct/instancemethods/validation.rb +1 -1
  30. data/lib/striuct/instancemethods/values.rb +0 -17
  31. data/lib/striuct/requirements.rb +3 -2
  32. data/lib/striuct/singleton_class.rb +6 -4
  33. data/lib/striuct/version.rb +1 -1
  34. data/test/test_striuct-subclass-class-copy.rb +71 -0
  35. data/test/{test_striuct-subclass-instance-cloning.rb → test_striuct-subclass-instance-copy.rb} +2 -2
  36. metadata +13 -9
  37. data/lib/striuct/classmethods/object.rb +0 -54
  38. data/lib/striuct/specificcontainer.rb +0 -19
  39. data/test/test_striuct-subclass-class-cloning.rb +0 -21
data/History.rdoc CHANGED
@@ -1,3 +1,9 @@
1
+ === 0.3.5 2012-9-9
2
+
3
+ * fix:
4
+ * SubClass.dup, SubClass.clone
5
+ * SubClass#replace_values
6
+
1
7
  === 0.3.4 2012-9-8
2
8
 
3
9
  * fix:
data/Manifest.txt CHANGED
@@ -9,20 +9,26 @@ striuct.gemspec
9
9
  .yardopts
10
10
 
11
11
  lib/striuct.rb
12
+ lib/striuct/requirements.rb
12
13
  lib/striuct/version.rb
13
- lib/striuct/specificcontainer.rb
14
+ lib/striuct/structs.rb
15
+ lib/striuct/attributes.rb
16
+ lib/striuct/hashdeepdupulicatable.rb
14
17
  lib/striuct/singleton_class.rb
18
+ lib/striuct/classmethods.rb
19
+ lib/striuct/instancemethods.rb
15
20
 
16
21
  lib/striuct/classmethods/README.md
17
- lib/striuct/classmethods.rb
18
22
  lib/striuct/classmethods/requirements.rb
19
23
  lib/striuct/classmethods/inner.rb
20
24
  lib/striuct/classmethods/named.rb
21
- lib/striuct/classmethods/object.rb
25
+ lib/striuct/classmethods/copy.rb
26
+ lib/striuct/classmethods/fix.rb
22
27
  lib/striuct/classmethods/length.rb
23
28
  lib/striuct/classmethods/to_struct.rb
24
29
  lib/striuct/classmethods/constructor.rb
25
30
  lib/striuct/classmethods/enum.rb
31
+ lib/striuct/classmethods/hashy.rb
26
32
  lib/striuct/classmethods/default.rb
27
33
  lib/striuct/classmethods/adjustment.rb
28
34
  lib/striuct/classmethods/macro.rb
@@ -30,13 +36,13 @@ lib/striuct/classmethods/prevent_conflicts.rb
30
36
  lib/striuct/classmethods/validation.rb
31
37
 
32
38
  lib/striuct/instancemethods/README.md
33
- lib/striuct/instancemethods.rb
34
39
  lib/striuct/instancemethods/requirements.rb
35
40
  lib/striuct/instancemethods/delegate_class_methods.rb
36
41
  lib/striuct/instancemethods/keyvalidatable.rb
37
- lib/striuct/instancemethods/inner.rb
42
+ lib/striuct/instancemethods/inner_accessor.rb
38
43
  lib/striuct/instancemethods/object.rb
39
44
  lib/striuct/instancemethods/compare.rb
45
+ lib/striuct/instancemethods/to_s.rb
40
46
  lib/striuct/instancemethods/values.rb
41
47
  lib/striuct/instancemethods/cast.rb
42
48
  lib/striuct/instancemethods/default.rb
@@ -51,7 +57,7 @@ lib/striuct/instancemethods/safety.rb
51
57
  test/helper.rb
52
58
  test/test_striuct-version.rb
53
59
  test/test_striuct-singleton_class-define.rb
54
- test/test_striuct-subclass-class-cloning.rb
60
+ test/test_striuct-subclass-class-copy.rb
55
61
  test/test_striuct-subclass-class-close.rb
56
62
  test/test_striuct-subclass-class-freeze.rb
57
63
  test/test_striuct-subclass-class-constructor.rb
@@ -66,7 +72,7 @@ test/test_striuct-subclass-instance-accessor.rb
66
72
  test/test_striuct-subclass-instance-adjuster.rb
67
73
  test/test_striuct-subclass-instance-assign.rb
68
74
  test/test_striuct-subclass-instance-basic.rb
69
- test/test_striuct-subclass-instance-cloning.rb
75
+ test/test_striuct-subclass-instance-copy.rb
70
76
  test/test_striuct-subclass-instance-compare.rb
71
77
  test/test_striuct-subclass-instance-default_value.rb
72
78
  test/test_striuct-subclass-instance-enum.rb
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  striuct
2
2
  =======
3
3
 
4
+ [![Build Status](https://secure.travis-ci.org/kachick/striuct.png)](http://travis-ci.org/kachick/striuct)
5
+
4
6
  Description
5
7
  -----------
6
8
 
@@ -46,8 +48,8 @@ class User < Person # Inheritable
46
48
  member :id, Integer, # Looks typed validation
47
49
  default_proc: ->{User.next_id} # With default value
48
50
 
51
+ @id = 0
49
52
  def self.next_id
50
- @id ||= 0
51
53
  @id += 1
52
54
  end
53
55
  end
@@ -64,8 +66,10 @@ ken.id #=> 2
64
66
  ```ruby
65
67
  class Foo < Striuct
66
68
  member :foo
67
- member :bar, Numeric, inference: true
68
- member :with_adjuster, Integer, &->v{Integer v}
69
+ member :bar, Numeric, # First validation under Numeric
70
+ inference: true # And use inference Validation
71
+ member :with_adjuster, Integer,
72
+ &->v{Integer v} # Use adjuster before a setter
69
73
  end
70
74
 
71
75
  foo = Foo.new
@@ -89,19 +93,17 @@ foo.with_adjuster = '5'
89
93
  foo.with_adjuster #=> 5 # Casted via adjuster
90
94
  ```
91
95
 
96
+ ### How to build a flexible condition
92
97
 
93
- ### More Examples
94
-
95
- #### Basics
96
-
97
- example/*
98
+ * That from validation library.
99
+ See the [validation-API](http://kachick.github.com/docs/validation/api/frames.html)
98
100
 
99
101
  Requirements
100
102
  -------------
101
103
 
102
104
  * Ruby 1.9.2 or later [MRI/YARV, Rubinius](http://travis-ci.org/#!/kachick/striuct)
103
- * validation - 0.0.3
104
- * keyvalidatable - 0.0.3
105
+ * [validation](https://github.com/kachick/validation) - 0.0.3
106
+ * [keyvalidatable](https://github.com/kachick/keyvalidatable) - 0.0.3
105
107
 
106
108
  Install
107
109
  -------
data/example/README.rb CHANGED
@@ -11,8 +11,8 @@ class User < Person # Inheritable
11
11
  member :id, Integer, # Looks typed validation
12
12
  default_proc: ->{User.next_id} # With default value
13
13
 
14
+ @id = 0
14
15
  def self.next_id
15
- @id ||= 0
16
16
  @id += 1
17
17
  end
18
18
  end
@@ -26,8 +26,10 @@ p ken.id #=> 2
26
26
 
27
27
  class Foo < Striuct
28
28
  member :foo
29
- member :bar, Numeric, inference: true
30
- member :with_adjuster, Integer, &->v{Integer v}
29
+ member :bar, Numeric,
30
+ inference: true
31
+ member :with_adjuster, Integer,
32
+ &->v{Integer v}
31
33
  end
32
34
 
33
35
  foo = Foo.new
@@ -0,0 +1,114 @@
1
+ class Striuct
2
+
3
+ # Attributes for each Member(Autonym)
4
+ class Attributes
5
+
6
+ VALUES = [:condition, :adjuster].freeze
7
+ BOOLEANS = [
8
+ :inference,
9
+ :validate_with_setter,
10
+ :validate_with_getter
11
+ ].freeze
12
+
13
+ def initialize
14
+ @hash = {
15
+ inference: false,
16
+ validate_with_setter: false,
17
+ validate_with_getter: false
18
+ }
19
+ end
20
+
21
+ VALUES.each do |role|
22
+ define_method "has_#{role}?".to_sym do
23
+ @hash.has_key? role
24
+ end
25
+
26
+ define_method role do
27
+ @hash.fetch role
28
+ end
29
+ end
30
+
31
+ def condition=(condition)
32
+ unless ::Validation.conditionable? condition
33
+ raise TypeError, 'wrong object for condition'
34
+ end
35
+
36
+ @hash[:condition] = condition
37
+ end
38
+
39
+ def adjuster=(adjuster)
40
+ unless ::Validation.adjustable? adjuster
41
+ raise ArgumentError, "wrong number of block argument #{arity} for 1"
42
+ end
43
+
44
+ @hash[:adjuster] = adjuster
45
+ end
46
+
47
+ BOOLEANS.each do |role|
48
+ define_method "#{role}?".to_sym do
49
+ @hash.fetch role
50
+ end
51
+
52
+ define_method "#{role}=".to_sym do |arg|
53
+ raise TypeError unless arg.equal?(true) or arg.equal?(false)
54
+
55
+ @hash[role] = arg
56
+ end
57
+ end
58
+
59
+ def has_default?
60
+ @hash.has_key? :default_value
61
+ end
62
+
63
+ def default_value
64
+ @hash.fetch :default_value
65
+ end
66
+
67
+ def default_type
68
+ @hash.fetch :default_type
69
+ end
70
+
71
+ # @param [Symbol] type - :value / :lazy
72
+ def set_default(value, type)
73
+ raise TypeError unless type.equal?(:value) or type.equal?(:lazy)
74
+ check_default_lazy_proc value if type.equal?(:lazy)
75
+
76
+ @hash[:default_type] = type
77
+ @hash[:default_value] = value
78
+ end
79
+
80
+ def check_default_lazy_proc(_proc)
81
+ raise TypeError unless _proc.respond_to? :call
82
+ arity = _proc.arity
83
+ unless arity <= 2
84
+ raise ArgumentError, "wrong number of block parameter #{arity} for 0..2"
85
+ end
86
+ end
87
+
88
+ def freeze
89
+ ret = super
90
+ @hash.freeze
91
+ ret
92
+ end
93
+
94
+ def dup
95
+ ret = super
96
+ @hash = @hash.dup
97
+ ret
98
+ end
99
+
100
+ private
101
+
102
+ def initialize_copy(original)
103
+ ret = super original
104
+ @hash = @hash.dup
105
+ ret
106
+ end
107
+
108
+ end
109
+
110
+ if respond_to? :private_constant
111
+ private_constant :Attributes
112
+ end
113
+
114
+ end
@@ -4,30 +4,23 @@ class Striuct; module ClassMethods
4
4
 
5
5
  # @param [Symbol, String] name
6
6
  def has_adjuster?(name)
7
- autonym = autonym_for name
7
+ autonym = autonym_for_name name
8
8
 
9
- ! adjuster_for(autonym).nil?
9
+ _attributes_for(autonym).has_adjuster?
10
10
  end
11
11
 
12
- alias_method :has_flavor?, :has_adjuster?
12
+ alias_method :has_flavor?, :has_adjuster? # obsolute
13
13
 
14
14
  # @param [Symbol, String] name
15
15
  def adjuster_for(name)
16
- autonym = autonym_for name
16
+ return nil unless has_adjuster? name
17
+ autonym = autonym_for_name name
17
18
 
18
- _adjuster_for autonym
19
+ _attributes_for(autonym).adjuster
19
20
  end
20
21
 
21
- alias_method :flavor_for, :adjuster_for
22
-
23
- private
24
-
25
- def _adjuster_for(autonym)
26
- @adjusters[autonym]
27
- end
28
-
29
- alias_method :_flavor_for, :_adjuster_for
22
+ alias_method :flavor_for, :adjuster_for # obsolute
30
23
 
31
24
  # @endgroup
32
25
 
33
- end; end
26
+ end; end
@@ -0,0 +1,55 @@
1
+ class Striuct; module ClassMethods
2
+
3
+ # @group Basic Methods for Ruby's Object
4
+
5
+ # @return [Module]
6
+ def dup
7
+ autonyms = @autonyms.dup
8
+ aliases = @aliases.dup
9
+ attributes = @attributes.deep_dup
10
+
11
+ super.tap {|ret|
12
+ ret.instance_eval do
13
+ @autonyms = autonyms
14
+ @aliases = aliases
15
+ @attributes = attributes
16
+ end
17
+ }
18
+ end
19
+
20
+ # @return [Module]
21
+ def clone
22
+ ret = super
23
+ ret.__send__ :close if closed?
24
+ ret
25
+ end
26
+
27
+ private
28
+
29
+ def inherited(subclass)
30
+ autonyms = @autonyms.dup
31
+ aliases = @aliases.dup
32
+ attributes = @attributes.deep_dup
33
+ protect_level = @protect_level
34
+
35
+ subclass.class_eval do
36
+ original_inherited subclass
37
+
38
+ @autonyms = autonyms
39
+ @aliases = aliases
40
+ @attributes = attributes
41
+ @protect_level = protect_level
42
+ end
43
+ end
44
+
45
+ def initialize_copy(original)
46
+ ret = super original
47
+ @autonyms = @autonyms.dup
48
+ @aliases = @aliases.dup
49
+ @attributes = @attributes.deep_dup
50
+ ret
51
+ end
52
+
53
+ # @endgroup
54
+
55
+ end; end
@@ -4,25 +4,30 @@ class Striuct; module ClassMethods
4
4
 
5
5
  # @param [Symbol, String] name
6
6
  def has_default?(name)
7
- autonym = autonym_for name
7
+ autonym = autonym_for_name name
8
8
 
9
- @defaults.has_key? autonym
9
+ _attributes_for(autonym).has_default?
10
10
  end
11
11
 
12
12
  # @param [Symbol, String] name
13
- def default_for(name)
14
- autonym = autonym_for name
13
+ def default_value_for(name)
14
+ autonym = autonym_for_name name
15
15
  raise "#{name} has no default value" unless has_default? autonym
16
-
17
- _default_for autonym
18
- end
19
-
20
- private
21
-
22
- def _default_for(autonym)
23
- @defaults[autonym]
16
+
17
+ _attributes_for(autonym).default_value
24
18
  end
25
19
 
20
+ alias_method :default_for, :default_value_for
21
+
22
+ # @param [Symbol, String] name
23
+ # @return [Symbol] :value / :proc
24
+ def default_type_for(name)
25
+ autonym = autonym_for_name name
26
+ raise "#{name} has no default value" unless has_default? autonym
27
+
28
+ _attributes_for(autonym).default_type
29
+ end
30
+
26
31
  # @endgroup
27
32
 
28
- end; end
33
+ end; end
@@ -15,7 +15,6 @@ class Striuct; module ClassMethods
15
15
 
16
16
  alias_method :each_name, :each_autonym
17
17
  alias_method :each_member, :each_autonym
18
- alias_method :each_key, :each_autonym
19
18
 
20
19
  # @yield [index]
21
20
  # @yieldparam [Integer] Index
@@ -42,8 +41,7 @@ class Striuct; module ClassMethods
42
41
 
43
42
  alias_method :each_name_with_index, :each_autonym_with_index
44
43
  alias_method :each_member_with_index, :each_autonym_with_index
45
- alias_method :each_key_with_index, :each_autonym_with_index
46
44
 
47
45
  # @endgroup
48
46
 
49
- end; end
47
+ end; end
@@ -0,0 +1,28 @@
1
+ class Striuct; module ClassMethods
2
+
3
+ # @group Fix inner data structures
4
+
5
+ # @return [self]
6
+ def freeze
7
+ [@autonyms, @attributes, @aliases].each(&:freeze)
8
+ super
9
+ end
10
+
11
+ def closed?
12
+ [@autonyms, @attributes, @aliases].any?(&:frozen?)
13
+ end
14
+
15
+ private
16
+
17
+ # @return [self]
18
+ def close_member
19
+ [@autonyms, @attributes, @aliases].each(&:freeze)
20
+ self
21
+ end
22
+
23
+ alias_method :fix_structural, :close_member
24
+ alias_method :close, :close_member
25
+
26
+ # @endgroup
27
+
28
+ end; end
@@ -0,0 +1,22 @@
1
+ require_relative 'named'
2
+ require_relative 'enum'
3
+
4
+ class Striuct; module ClassMethods
5
+
6
+ # @group Like Ruby's Hash
7
+
8
+ alias_method :keys, :autonyms
9
+ alias_method :has_key?, :has_member?
10
+ alias_method :key?, :has_key?
11
+ alias_method :each_key, :each_autonym
12
+
13
+ # @endgroup
14
+
15
+
16
+ # @group Like Ruby's Hash +
17
+
18
+ alias_method :each_key_with_index, :each_autonym_with_index
19
+
20
+ # @endgroup
21
+
22
+ end; end
@@ -4,89 +4,55 @@ class Striuct; module ClassMethods
4
4
 
5
5
  private
6
6
 
7
- def __getter__!(name)
8
- define_method name do
9
- __get__ name
10
- end
11
-
12
- nil
13
- end
7
+ def _add_autonym(autonym)
8
+ autonym = _nameable_for autonym
9
+ raise NameError, 'already defined' if member? autonym
14
10
 
15
- def _remove_inference(autonym)
16
- @inferences.delete autonym
17
- end
18
-
19
- def _mark_setter_validation(autonym)
20
- @setter_validations[autonym] = true
11
+ @attributes[autonym] = Attributes.new
12
+ @autonyms << autonym
21
13
  end
22
14
 
23
- def _mark_getter_validation(autonym)
24
- @getter_validations[autonym] = true
15
+ def _attributes_for(autonym)
16
+ @attributes.fetch autonym
25
17
  end
26
18
 
27
- def _mark_inference(autonym)
28
- @inferences[autonym] = true
29
- end
30
-
31
- def _set_adjuster(autonym, adjuster)
32
- @adjusters[autonym] = adjuster
33
- end
34
-
35
- def _set_condition(autonym, condition)
36
- @conditions[autonym] = condition
37
- end
38
-
39
- def _set_default_value(autonym, value)
40
- @defaults[autonym] = value
19
+ def _def_getter!(autonym)
20
+ define_method autonym do
21
+ _get autonym
22
+ end
23
+
24
+ nil
41
25
  end
42
26
 
43
- def __setter__!(autonym, condition, &adjuster)
44
- __set_condition__! autonym, condition unless Validation::Condition::ANYTHING.equal? condition
45
- __set_adjuster__! autonym, &adjuster if block_given?
46
-
47
- define_method :"#{autonym}=" do |value|
48
- __set__ autonym, value
27
+ def _def_setter!(autonym, condition, &adjuster)
28
+ unless Validation::Condition::ANYTHING.equal? condition
29
+ _attributes_for(autonym).condition = condition
49
30
  end
50
-
51
- nil
52
31
 
53
- end
54
-
55
- def __set_condition__!(autonym, condition)
56
- if ::Validation.conditionable? condition
57
- _set_condition autonym, condition
58
- else
59
- raise TypeError, 'wrong object for condition'
32
+ if block_given?
33
+ _attributes_for(autonym).adjuster = adjuster
60
34
  end
61
-
62
- nil
63
- end
64
35
 
65
- def __set_adjuster__!(autonym, &adjuster)
66
- if ::Validation.adjustable? adjuster
67
- _set_adjuster autonym, adjuster
68
- else
69
- raise ArgumentError, "wrong number of block argument #{arity} for 1"
36
+ define_method :"#{autonym}=" do |value|
37
+ _set autonym, value
70
38
  end
71
39
 
72
40
  nil
73
41
  end
74
42
 
75
- def __found_family__!(_caller, autonym, our)
43
+ def _found_family!(_caller, autonym, our)
76
44
  family = our.class
77
45
 
78
46
  raise 'must not happen' unless inference?(autonym) and
79
47
  autonym?(autonym) and
80
48
  _caller.instance_of?(self)
81
49
 
82
- raise ArgumentError unless Validation.conditionable? family
83
-
84
- _set_condition autonym, family
85
- _remove_inference autonym
50
+ _attributes_for(autonym).condition = family
51
+ _attributes_for(autonym).inference = false
86
52
 
87
53
  nil
88
54
  end
89
55
 
90
56
  # @endgroup
91
57
 
92
- end; end
58
+ end; end