striuct 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +12 -0
- data/LICENSE +2 -2
- data/Manifest.txt +34 -24
- data/README.md +34 -5
- data/example/README.rb +20 -1
- data/lib/striuct/classmethods/README.md +5 -0
- data/lib/striuct/classmethods/adjustment.rb +5 -3
- data/lib/striuct/classmethods/default.rb +2 -2
- data/lib/striuct/classmethods/enum.rb +3 -3
- data/lib/striuct/classmethods/inner.rb +28 -49
- data/lib/striuct/classmethods/named.rb +22 -2
- data/lib/striuct/classmethods/object.rb +35 -5
- data/lib/striuct/instancemethods/README.md +5 -0
- data/lib/striuct/instancemethods/assign.rb +1 -1
- data/lib/striuct/instancemethods/cast.rb +35 -0
- data/lib/striuct/instancemethods/compare.rb +4 -5
- data/lib/striuct/instancemethods/delegate_class_methods.rb +12 -4
- data/lib/striuct/instancemethods/hashy.rb +0 -11
- data/lib/striuct/instancemethods/keyvalidatable.rb +14 -0
- data/lib/striuct/instancemethods/lock.rb +15 -4
- data/lib/striuct/instancemethods/object.rb +11 -6
- data/lib/striuct/instancemethods/requirements.rb +2 -2
- data/lib/striuct/instancemethods/subscript.rb +1 -1
- data/lib/striuct/instancemethods/values.rb +1 -8
- data/lib/striuct/singleton_class.rb +9 -36
- data/lib/striuct/version.rb +1 -1
- data/striuct.gemspec +4 -4
- data/test/{test_striuct_define.rb → test_striuct-singleton_class-define.rb} +1 -1
- data/test/test_striuct-subclass-class-cloning.rb +21 -0
- data/test/{test_striuct_subclass_classlock.rb → test_striuct-subclass-class-close.rb} +2 -2
- data/test/{test_striuct_subclass_classmethods.rb → test_striuct-subclass-class-constructor.rb} +15 -20
- data/test/test_striuct-subclass-class-freeze.rb +36 -0
- data/test/{test_striuct_subclass_inheritable.rb → test_striuct-subclass-class-inheritable.rb} +1 -1
- data/test/test_striuct-subclass-class-macro.rb +13 -0
- data/test/{test_striuct_subclass_safetynaming.rb → test_striuct-subclass-class-safety_naming.rb} +1 -1
- data/test/test_striuct-subclass-class-validation.rb +26 -0
- data/test/{test_striuct_subclass_alias_member.rb → test_striuct-subclass-feature-alias_member.rb} +1 -1
- data/test/{test_striuct_subclass_to_struct.rb → test_striuct-subclass-feature-to_struct.rb} +1 -1
- data/test/test_striuct-subclass-instance-accessor.rb +137 -0
- data/test/{test_striuct_subclass_adjuster.rb → test_striuct-subclass-instance-adjuster.rb} +28 -28
- data/test/{test_striuct_subclass_assign.rb → test_striuct-subclass-instance-assign.rb} +1 -1
- data/test/test_striuct-subclass-instance-basic.rb +32 -0
- data/test/test_striuct-subclass-instance-cloning.rb +23 -0
- data/test/test_striuct-subclass-instance-compare.rb +52 -0
- data/test/{test_striuct_subclass_default_value.rb → test_striuct-subclass-instance-default_value.rb} +2 -2
- data/test/test_striuct-subclass-instance-enum.rb +93 -0
- data/test/{test_striuct_subclass_freeze.rb → test_striuct-subclass-instance-freeze.rb} +1 -1
- data/test/{test_striuct_subclass_hashlike.rb → test_striuct-subclass-instance-hashlike.rb} +1 -1
- data/test/test_striuct-subclass-instance-keyvalidatable.rb +25 -0
- data/test/{test_striuct_subclass_lock.rb → test_striuct-subclass-instance-lock.rb} +1 -1
- data/test/test_striuct-subclass-instance-to_s_family.rb +25 -0
- data/test/{test_striuct_subclass_functional_condition.rb → test_striuct-subclass-instance-validation_functional_condition.rb} +0 -0
- data/test/{test_striuct_subclass_inference.rb → test_striuct-subclass-instance-validation_inference.rb} +1 -1
- data/test/{test_striuct_subclass_specific_conditions.rb → test_striuct-subclass-instance-validation_specific_conditions.rb} +1 -1
- data/test/{test_striuct_subclass_getter_validation.rb → test_striuct-subclass-instance-validation_with_getter.rb} +1 -1
- data/test/test_striuct-subclass-instance_names.rb +18 -0
- data/test/test_striuct-version.rb +11 -0
- metadata +68 -50
- data/lib/striuct/instancemethods/singleton_class.rb +0 -22
- data/lib/striuct/instancemethods/to_struct.rb +0 -12
- data/test/test_striuct.rb +0 -24
- data/test/test_striuct_subclass_cloning.rb +0 -36
- data/test/test_striuct_subclass_for_pairs.rb +0 -20
- data/test/test_striuct_subclass_instance.rb +0 -289
- data/test/test_striuct_version.rb +0 -9
data/History.rdoc
CHANGED
data/LICENSE
CHANGED
data/Manifest.txt
CHANGED
@@ -13,6 +13,7 @@ lib/striuct/version.rb
|
|
13
13
|
lib/striuct/specificcontainer.rb
|
14
14
|
lib/striuct/singleton_class.rb
|
15
15
|
|
16
|
+
lib/striuct/classmethods/README.md
|
16
17
|
lib/striuct/classmethods.rb
|
17
18
|
lib/striuct/classmethods/requirements.rb
|
18
19
|
lib/striuct/classmethods/inner.rb
|
@@ -28,15 +29,16 @@ lib/striuct/classmethods/macro.rb
|
|
28
29
|
lib/striuct/classmethods/prevent_conflicts.rb
|
29
30
|
lib/striuct/classmethods/validation.rb
|
30
31
|
|
32
|
+
lib/striuct/instancemethods/README.md
|
31
33
|
lib/striuct/instancemethods.rb
|
32
34
|
lib/striuct/instancemethods/requirements.rb
|
33
|
-
lib/striuct/instancemethods/singleton_class.rb
|
34
35
|
lib/striuct/instancemethods/delegate_class_methods.rb
|
36
|
+
lib/striuct/instancemethods/keyvalidatable.rb
|
35
37
|
lib/striuct/instancemethods/inner.rb
|
36
38
|
lib/striuct/instancemethods/object.rb
|
37
39
|
lib/striuct/instancemethods/compare.rb
|
38
40
|
lib/striuct/instancemethods/values.rb
|
39
|
-
lib/striuct/instancemethods/
|
41
|
+
lib/striuct/instancemethods/cast.rb
|
40
42
|
lib/striuct/instancemethods/default.rb
|
41
43
|
lib/striuct/instancemethods/enum.rb
|
42
44
|
lib/striuct/instancemethods/hashy.rb
|
@@ -47,28 +49,36 @@ lib/striuct/instancemethods/validation.rb
|
|
47
49
|
lib/striuct/instancemethods/safety.rb
|
48
50
|
|
49
51
|
test/helper.rb
|
50
|
-
test/test_striuct.rb
|
51
|
-
test/
|
52
|
-
test/
|
53
|
-
test/
|
54
|
-
test/
|
55
|
-
test/
|
56
|
-
test/
|
57
|
-
test/
|
58
|
-
test/
|
59
|
-
test/
|
60
|
-
test/
|
61
|
-
test/
|
62
|
-
test/
|
63
|
-
test/
|
64
|
-
test/
|
65
|
-
test/
|
66
|
-
test/
|
67
|
-
test/
|
68
|
-
test/
|
69
|
-
test/
|
70
|
-
test/
|
71
|
-
test/
|
52
|
+
test/test_striuct-version.rb
|
53
|
+
test/test_striuct-singleton_class-define.rb
|
54
|
+
test/test_striuct-subclass-class-cloning.rb
|
55
|
+
test/test_striuct-subclass-class-close.rb
|
56
|
+
test/test_striuct-subclass-class-freeze.rb
|
57
|
+
test/test_striuct-subclass-class-constructor.rb
|
58
|
+
test/test_striuct-subclass-class-inheritable.rb
|
59
|
+
test/test_striuct-subclass-class-macro.rb
|
60
|
+
test/test_striuct-subclass-class-safety_naming.rb
|
61
|
+
test/test_striuct-subclass-class-validation.rb
|
62
|
+
test/test_striuct-subclass-feature-alias_member.rb
|
63
|
+
test/test_striuct-subclass-feature-to_struct.rb
|
64
|
+
test/test_striuct-subclass-instance-accessor.rb
|
65
|
+
test/test_striuct-subclass-instance-adjuster.rb
|
66
|
+
test/test_striuct-subclass-instance-assign.rb
|
67
|
+
test/test_striuct-subclass-instance-basic.rb
|
68
|
+
test/test_striuct-subclass-instance-cloning.rb
|
69
|
+
test/test_striuct-subclass-instance-compare.rb
|
70
|
+
test/test_striuct-subclass-instance-default_value.rb
|
71
|
+
test/test_striuct-subclass-instance-enum.rb
|
72
|
+
test/test_striuct-subclass-instance-freeze.rb
|
73
|
+
test/test_striuct-subclass-instance-hashlike.rb
|
74
|
+
test/test_striuct-subclass-instance-keyvalidatable.rb
|
75
|
+
test/test_striuct-subclass-instance-lock.rb
|
76
|
+
test/test_striuct-subclass-instance-to_s_family.rb
|
77
|
+
test/test_striuct-subclass-instance-validation_functional_condition.rb
|
78
|
+
test/test_striuct-subclass-instance-validation_inference.rb
|
79
|
+
test/test_striuct-subclass-instance-validation_specific_conditions.rb
|
80
|
+
test/test_striuct-subclass-instance-validation_with_getter.rb
|
81
|
+
test/test_striuct-subclass-instance_names.rb
|
72
82
|
|
73
83
|
example/README.rb
|
74
84
|
example/example1.rb
|
data/README.md
CHANGED
@@ -21,7 +21,7 @@ Features
|
|
21
21
|
* Default value
|
22
22
|
* Member aliasing
|
23
23
|
* Inheritable
|
24
|
-
*
|
24
|
+
* Handling between nil <-> unassigned
|
25
25
|
* More flendly API for Hash
|
26
26
|
|
27
27
|
### Onepoint
|
@@ -32,7 +32,7 @@ Features
|
|
32
32
|
Usage
|
33
33
|
-----
|
34
34
|
|
35
|
-
### Overview
|
35
|
+
### Overview - Case 1
|
36
36
|
|
37
37
|
```ruby
|
38
38
|
require 'striuct'
|
@@ -59,6 +59,36 @@ john.id #=> 1
|
|
59
59
|
ken = User[name: 'ken'] # Construct from hash
|
60
60
|
ken.id #=> 2
|
61
61
|
```
|
62
|
+
### Overview - Case 2
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class Foo < Striuct
|
66
|
+
member :foo
|
67
|
+
member :bar, Numeric, inference: true
|
68
|
+
member :with_adjuster, Integer, &->v{Integer v}
|
69
|
+
end
|
70
|
+
|
71
|
+
foo = Foo.new
|
72
|
+
|
73
|
+
# nil <-> unaasigned
|
74
|
+
foo.foo #=> nil
|
75
|
+
foo.assign?(:foo) #=> false
|
76
|
+
foo.foo = nil
|
77
|
+
foo.assign?(:foo) #=> true
|
78
|
+
|
79
|
+
# Lock to a member
|
80
|
+
foo.lock(:foo)
|
81
|
+
foo.foo = nil #=> error
|
82
|
+
|
83
|
+
# Inference Validation
|
84
|
+
foo.bar = 1.2 #=> pass # memorize 1.2's class is Float
|
85
|
+
foo.bar = 1 #=> error # 1 is not Float
|
86
|
+
|
87
|
+
# With adjuster
|
88
|
+
foo.with_adjuster = '5'
|
89
|
+
foo.with_adjuster #=> 5 # Casted via adjuster
|
90
|
+
```
|
91
|
+
|
62
92
|
|
63
93
|
### More Examples
|
64
94
|
|
@@ -71,7 +101,7 @@ Requirements
|
|
71
101
|
|
72
102
|
* Ruby 1.9.2 or later [MRI/YARV, Rubinius](http://travis-ci.org/#!/kachick/striuct)
|
73
103
|
* validation - 0.0.3
|
74
|
-
* keyvalidatable - 0.0.
|
104
|
+
* keyvalidatable - 0.0.3
|
75
105
|
|
76
106
|
Install
|
77
107
|
-------
|
@@ -88,12 +118,11 @@ Link
|
|
88
118
|
* [issues](https://github.com/kachick/striuct/issues)
|
89
119
|
* [CI](http://travis-ci.org/#!/kachick/striuct)
|
90
120
|
* [gem](https://rubygems.org/gems/striuct)
|
91
|
-
* [gem+](http://metagem.info/gems/striuct)
|
92
121
|
|
93
122
|
License
|
94
123
|
--------
|
95
124
|
|
96
|
-
The MIT
|
125
|
+
The MIT X11 License
|
97
126
|
Copyright (c) 2011 Kenichi Kamiya
|
98
127
|
See the file LICENSE for further details.
|
99
128
|
|
data/example/README.rb
CHANGED
@@ -22,4 +22,23 @@ p john[:name] #=> 'john'
|
|
22
22
|
#~ p john.name = '' #=> error
|
23
23
|
p john.id #=> 1
|
24
24
|
ken = User[name: 'ken'] # Construct from hash
|
25
|
-
p ken.id #=> 2
|
25
|
+
p ken.id #=> 2
|
26
|
+
|
27
|
+
class Foo < Striuct
|
28
|
+
member :foo
|
29
|
+
member :bar, Numeric, inference: true
|
30
|
+
member :with_adjuster, Integer, &->v{Integer v}
|
31
|
+
end
|
32
|
+
|
33
|
+
foo = Foo.new
|
34
|
+
p foo.foo #=> nil
|
35
|
+
p foo.assign?(:foo) #=> false
|
36
|
+
foo.foo = nil
|
37
|
+
p foo.assign?(:foo) #=> true
|
38
|
+
foo.lock(:foo)
|
39
|
+
#~ foo.foo = nil #=> error
|
40
|
+
foo.bar = 1.2
|
41
|
+
#~ foo.bar = 1 #=> error
|
42
|
+
|
43
|
+
foo.with_adjuster = '5'
|
44
|
+
p foo.with_adjuster
|
@@ -13,15 +13,17 @@ class Striuct; module ClassMethods
|
|
13
13
|
|
14
14
|
# @param [Symbol, String] name
|
15
15
|
def adjuster_for(name)
|
16
|
-
|
16
|
+
autonym = autonym_for name
|
17
|
+
|
18
|
+
_adjuster_for autonym
|
17
19
|
end
|
18
20
|
|
19
21
|
alias_method :flavor_for, :adjuster_for
|
20
22
|
|
21
23
|
private
|
22
24
|
|
23
|
-
def _adjuster_for(
|
24
|
-
@adjusters[
|
25
|
+
def _adjuster_for(autonym)
|
26
|
+
@adjusters[autonym]
|
25
27
|
end
|
26
28
|
|
27
29
|
alias_method :_flavor_for, :_adjuster_for
|
@@ -9,7 +9,7 @@ class Striuct; module ClassMethods
|
|
9
9
|
def each_autonym
|
10
10
|
return to_enum(__callee__) unless block_given?
|
11
11
|
|
12
|
-
|
12
|
+
@autonyms.each{|autonym|yield autonym}
|
13
13
|
self
|
14
14
|
end
|
15
15
|
|
@@ -24,7 +24,7 @@ class Striuct; module ClassMethods
|
|
24
24
|
def each_index
|
25
25
|
return to_enum(__callee__) unless block_given?
|
26
26
|
|
27
|
-
|
27
|
+
@autonyms.each_index{|index|yield index}
|
28
28
|
self
|
29
29
|
end
|
30
30
|
|
@@ -36,7 +36,7 @@ class Striuct; module ClassMethods
|
|
36
36
|
def each_autonym_with_index
|
37
37
|
return to_enum(__callee__) unless block_given?
|
38
38
|
|
39
|
-
|
39
|
+
@autonyms.each_with_index{|autonym, index|yield autonym, index}
|
40
40
|
self
|
41
41
|
end
|
42
42
|
|
@@ -12,69 +12,49 @@ class Striuct; module ClassMethods
|
|
12
12
|
nil
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
16
|
-
@
|
15
|
+
def _remove_inference(autonym)
|
16
|
+
@inferences.delete autonym
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
def _alias_member(aliased, original)
|
22
|
-
@aliases[aliased] = original
|
23
|
-
end
|
24
|
-
|
25
|
-
def _autonym_for(aliased)
|
26
|
-
@aliases[aliased]
|
27
|
-
end
|
28
|
-
|
29
|
-
alias_method :_original_for, :_autonym_for
|
30
|
-
|
31
|
-
def _aliases_for(original)
|
32
|
-
@aliases.group_by{|aliased, org|org}[original]
|
33
|
-
end
|
34
|
-
|
35
|
-
def _remove_inference(name)
|
36
|
-
@inferences.delete name
|
37
|
-
end
|
38
|
-
|
39
|
-
def _mark_setter_validation(name)
|
40
|
-
@setter_validations[name] = true
|
19
|
+
def _mark_setter_validation(autonym)
|
20
|
+
@setter_validations[autonym] = true
|
41
21
|
end
|
42
22
|
|
43
|
-
def _mark_getter_validation(
|
44
|
-
@getter_validations[
|
23
|
+
def _mark_getter_validation(autonym)
|
24
|
+
@getter_validations[autonym] = true
|
45
25
|
end
|
46
26
|
|
47
|
-
def _mark_inference(
|
48
|
-
@inferences[
|
27
|
+
def _mark_inference(autonym)
|
28
|
+
@inferences[autonym] = true
|
49
29
|
end
|
50
30
|
|
51
|
-
def _set_adjuster(
|
52
|
-
@adjusters[
|
31
|
+
def _set_adjuster(autonym, adjuster)
|
32
|
+
@adjusters[autonym] = adjuster
|
53
33
|
end
|
54
34
|
|
55
|
-
def _set_condition(
|
56
|
-
@conditions[
|
35
|
+
def _set_condition(autonym, condition)
|
36
|
+
@conditions[autonym] = condition
|
57
37
|
end
|
58
38
|
|
59
|
-
def _set_default_value(
|
60
|
-
@defaults[
|
39
|
+
def _set_default_value(autonym, value)
|
40
|
+
@defaults[autonym] = value
|
61
41
|
end
|
62
42
|
|
63
|
-
def __setter__!(
|
64
|
-
__set_condition__!
|
65
|
-
__set_adjuster__!
|
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?
|
66
46
|
|
67
|
-
define_method :"#{
|
68
|
-
__set__
|
47
|
+
define_method :"#{autonym}=" do |value|
|
48
|
+
__set__ autonym, value
|
69
49
|
end
|
70
50
|
|
71
51
|
nil
|
72
52
|
|
73
53
|
end
|
74
54
|
|
75
|
-
def __set_condition__!(
|
55
|
+
def __set_condition__!(autonym, condition)
|
76
56
|
if ::Validation.conditionable? condition
|
77
|
-
_set_condition
|
57
|
+
_set_condition autonym, condition
|
78
58
|
else
|
79
59
|
raise TypeError, 'wrong object for condition'
|
80
60
|
end
|
@@ -82,9 +62,9 @@ class Striuct; module ClassMethods
|
|
82
62
|
nil
|
83
63
|
end
|
84
64
|
|
85
|
-
def __set_adjuster__!(
|
65
|
+
def __set_adjuster__!(autonym, &adjuster)
|
86
66
|
if ::Validation.adjustable? adjuster
|
87
|
-
_set_adjuster
|
67
|
+
_set_adjuster autonym, adjuster
|
88
68
|
else
|
89
69
|
raise ArgumentError, "wrong number of block argument #{arity} for 1"
|
90
70
|
end
|
@@ -92,18 +72,17 @@ class Striuct; module ClassMethods
|
|
92
72
|
nil
|
93
73
|
end
|
94
74
|
|
95
|
-
def __found_family__!(_caller,
|
75
|
+
def __found_family__!(_caller, autonym, our)
|
96
76
|
family = our.class
|
97
77
|
|
98
|
-
raise 'must not happen' unless
|
99
|
-
|
100
|
-
member?(name) and
|
78
|
+
raise 'must not happen' unless inference?(autonym) and
|
79
|
+
autonym?(autonym) and
|
101
80
|
_caller.instance_of?(self)
|
102
81
|
|
103
82
|
raise ArgumentError unless Validation.conditionable? family
|
104
83
|
|
105
|
-
_set_condition
|
106
|
-
_remove_inference
|
84
|
+
_set_condition autonym, family
|
85
|
+
_remove_inference autonym
|
107
86
|
|
108
87
|
nil
|
109
88
|
end
|
@@ -39,7 +39,7 @@ class Striuct; module ClassMethods
|
|
39
39
|
def autonym_for(name)
|
40
40
|
name = keyable_for name
|
41
41
|
|
42
|
-
if
|
42
|
+
if @autonyms.include? name
|
43
43
|
name
|
44
44
|
else
|
45
45
|
if autonym = _autonym_for(name)
|
@@ -61,13 +61,15 @@ class Striuct; module ClassMethods
|
|
61
61
|
alias_method :original?, :autonym?
|
62
62
|
|
63
63
|
# @param [Symbol, String] name
|
64
|
-
def
|
64
|
+
def alias?(name)
|
65
65
|
name = keyable_for name
|
66
66
|
raise NameError unless member? name
|
67
67
|
|
68
68
|
@aliases.has_key? name
|
69
69
|
end
|
70
70
|
|
71
|
+
alias_method :aliased?, :alias?
|
72
|
+
|
71
73
|
# @param [Symbol, String] autonym
|
72
74
|
def has_aliases?(autonym)
|
73
75
|
raise NameError unless autonym? autonym
|
@@ -88,6 +90,24 @@ class Striuct; module ClassMethods
|
|
88
90
|
def aliases
|
89
91
|
@aliases.dup
|
90
92
|
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
def _autonyms
|
97
|
+
@autonyms
|
98
|
+
end
|
99
|
+
|
100
|
+
def _alias_member(aliased, autonym)
|
101
|
+
@aliases[aliased] = autonym
|
102
|
+
end
|
103
|
+
|
104
|
+
def _autonym_for(aliased)
|
105
|
+
@aliases[aliased]
|
106
|
+
end
|
107
|
+
|
108
|
+
def _aliases_for(autonym)
|
109
|
+
@aliases.group_by{|aliased, an|an}.fetch(autonym)
|
110
|
+
end
|
91
111
|
|
92
112
|
# @endgroup
|
93
113
|
|
@@ -4,7 +4,7 @@ class Striuct; module ClassMethods
|
|
4
4
|
|
5
5
|
# @return [self]
|
6
6
|
def freeze
|
7
|
-
|
7
|
+
__floating_attributes__.each(&:freeze)
|
8
8
|
super
|
9
9
|
end
|
10
10
|
|
@@ -12,12 +12,42 @@ class Striuct; module ClassMethods
|
|
12
12
|
def dup
|
13
13
|
r = super
|
14
14
|
@autonyms, @adjusters, @defaults, @aliases,
|
15
|
-
@setter_validations, @getter_validations
|
16
|
-
|
17
|
-
|
18
|
-
@conditions, @inferences = @conditions.dup, @inferences.dup
|
15
|
+
@setter_validations, @getter_validations,
|
16
|
+
@conditions, @inferences =
|
17
|
+
*__floating_attributes__.map(&:dup)
|
19
18
|
r
|
20
19
|
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def inherited(subclass)
|
24
|
+
autonyms = @autonyms.dup
|
25
|
+
attributes = [@conditions, @adjusters, @defaults, @inferences, @aliases, @setter_validations, @getter_validations].map(&:dup)
|
26
|
+
protect_level = @protect_level
|
27
|
+
|
28
|
+
subclass.class_eval do
|
29
|
+
original_inherited subclass
|
30
|
+
|
31
|
+
@autonyms = autonyms
|
32
|
+
@conditions, @adjusters, @defaults, @inferences, @aliases, @setter_validations, @getter_validations = *attributes
|
33
|
+
@protect_level = protect_level
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize_copy(original)
|
38
|
+
ret = super original
|
39
|
+
@autonyms, @adjusters, @defaults, @aliases,
|
40
|
+
@setter_validations, @getter_validations,
|
41
|
+
@conditions, @inferences =
|
42
|
+
*__floating_attributes__.map(&:dup)
|
43
|
+
ret
|
44
|
+
end
|
45
|
+
|
46
|
+
def __floating_attributes__
|
47
|
+
[@autonyms, @adjusters, @defaults, @aliases,
|
48
|
+
@setter_validations, @getter_validations,
|
49
|
+
@conditions, @inferences]
|
50
|
+
end
|
21
51
|
|
22
52
|
# @endgroup
|
23
53
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class Striuct; module InstanceMethods
|
2
|
+
|
3
|
+
# @group Cast
|
4
|
+
|
5
|
+
# @return [self]
|
6
|
+
def to_striuct
|
7
|
+
self
|
8
|
+
end
|
9
|
+
|
10
|
+
# @return [Struct]
|
11
|
+
def to_struct
|
12
|
+
self.class.to_struct_class.new(*values)
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [Hash]
|
16
|
+
def to_h(reject_no_assign=false)
|
17
|
+
return @db.dup if reject_no_assign
|
18
|
+
|
19
|
+
{}.tap {|h|
|
20
|
+
each_pair do |autonym, val|
|
21
|
+
h[autonym] = val
|
22
|
+
end
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [Array]
|
27
|
+
def values
|
28
|
+
each_value.to_a
|
29
|
+
end
|
30
|
+
|
31
|
+
alias_method :to_a, :values
|
32
|
+
|
33
|
+
# @endgroup
|
34
|
+
|
35
|
+
end; end
|
@@ -4,13 +4,13 @@ class Striuct; module InstanceMethods
|
|
4
4
|
|
5
5
|
# @return [Boolean]
|
6
6
|
def ==(other)
|
7
|
-
__compare_all__ other, :==
|
7
|
+
__compare_all__? other, :==
|
8
8
|
end
|
9
9
|
|
10
10
|
alias_method :===, :==
|
11
11
|
|
12
12
|
def eql?(other)
|
13
|
-
__compare_all__ other, :eql?
|
13
|
+
__compare_all__? other, :eql?
|
14
14
|
end
|
15
15
|
|
16
16
|
# @return [Integer]
|
@@ -21,9 +21,8 @@ class Striuct; module InstanceMethods
|
|
21
21
|
private
|
22
22
|
|
23
23
|
# @param [Symbol] method
|
24
|
-
def __compare_all__(other, method)
|
25
|
-
instance_of?(
|
26
|
-
each_pair.all?{|k, v|v.__send__ method, other[k]}
|
24
|
+
def __compare_all__?(other, method)
|
25
|
+
other.instance_of?(self.class) && each_pair.all?{|k, v|v.__send__ method, other[k]}
|
27
26
|
end
|
28
27
|
|
29
28
|
# @endgroup
|
@@ -1,9 +1,16 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
1
3
|
class Striuct; module InstanceMethods
|
4
|
+
|
5
|
+
extend Forwardable
|
6
|
+
|
7
|
+
# Forwardable has public/protected class_macro
|
8
|
+
private_class_method(*Forwardable.instance_methods)
|
2
9
|
|
3
10
|
# @group Delegate Class Methods
|
4
11
|
|
5
|
-
|
6
|
-
|
12
|
+
def_delegators :'self.class',
|
13
|
+
:_autonyms,
|
7
14
|
:keyable_for, :autonym_for, :aliases_for,
|
8
15
|
:validator_for, :condition_for,
|
9
16
|
:adjuster_for, :flavor_for,
|
@@ -13,8 +20,9 @@ class Striuct; module InstanceMethods
|
|
13
20
|
:restrict?, :has_validator?, :has_condition?,
|
14
21
|
:safety_getter?, :safety_reader?, :safety_setter?, :safty_writer?, :inference?,
|
15
22
|
:has_default?, :default_for, :has_adjuster?, :has_flavor?
|
16
|
-
)
|
17
23
|
|
18
|
-
|
24
|
+
private :_autonyms
|
19
25
|
|
26
|
+
# @endgroup
|
27
|
+
|
20
28
|
end; end
|
@@ -5,17 +5,6 @@ class Striuct; module InstanceMethods
|
|
5
5
|
# @group Like Ruby's Hash
|
6
6
|
|
7
7
|
alias_method :fetch, :[]
|
8
|
-
|
9
|
-
# @return [Hash]
|
10
|
-
def to_h(reject_no_assign=false)
|
11
|
-
return @db.dup if reject_no_assign
|
12
|
-
|
13
|
-
{}.tap {|h|
|
14
|
-
each_pair do |autonym, val|
|
15
|
-
h[autonym] = val
|
16
|
-
end
|
17
|
-
}
|
18
|
-
end
|
19
8
|
|
20
9
|
def has_value?(value)
|
21
10
|
@db.has_value? value
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'keyvalidatable'
|
2
|
+
|
3
|
+
class Striuct; module InstanceMethods
|
4
|
+
|
5
|
+
extend Forwardable
|
6
|
+
include KeyValidatable
|
7
|
+
|
8
|
+
KeyValidatable.instance_methods.each do |feature|
|
9
|
+
if specific_feature = feature.to_s.sub!('members'){'autonyms'}
|
10
|
+
def_delegator :self, feature, specific_feature.to_sym
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end; end
|