striuct 0.3.5.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. data/.travis.yml +3 -0
  2. data/.yardopts +1 -1
  3. data/LICENSE +1 -1
  4. data/README.md +7 -3
  5. data/example/example1.rb +5 -4
  6. data/lib/striuct/classmethods/README.md +9 -4
  7. data/lib/striuct/classmethods/adjustment.rb +4 -15
  8. data/lib/striuct/{attributes.rb → classmethods/attributes.rb} +15 -17
  9. data/lib/striuct/classmethods/{prevent_conflicts.rb → conflict_management.rb} +37 -14
  10. data/lib/striuct/classmethods/constructor.rb +2 -4
  11. data/lib/striuct/classmethods/copy.rb +2 -2
  12. data/lib/striuct/classmethods/default.rb +9 -18
  13. data/lib/striuct/classmethods/enum.rb +3 -5
  14. data/lib/striuct/classmethods/inner.rb +6 -10
  15. data/lib/striuct/classmethods/macro.rb +33 -48
  16. data/lib/striuct/classmethods/names.rb +75 -0
  17. data/lib/striuct/classmethods/predicate.rb +128 -0
  18. data/lib/striuct/classmethods/{requiremnets.rb → requirements.rb} +6 -3
  19. data/lib/striuct/classmethods/validation.rb +5 -43
  20. data/lib/striuct/instancemethods/README.md +4 -4
  21. data/lib/striuct/instancemethods/assign.rb +11 -13
  22. data/lib/striuct/instancemethods/cast.rb +5 -8
  23. data/lib/striuct/instancemethods/compare.rb +6 -6
  24. data/lib/striuct/instancemethods/default.rb +28 -6
  25. data/lib/striuct/instancemethods/delegate_class_methods.rb +21 -21
  26. data/lib/striuct/instancemethods/enum.rb +3 -9
  27. data/lib/striuct/instancemethods/getter.rb +47 -0
  28. data/lib/striuct/instancemethods/hashy.rb +2 -6
  29. data/lib/striuct/instancemethods/keyvalidatable.rb +0 -5
  30. data/lib/striuct/instancemethods/lock.rb +35 -43
  31. data/lib/striuct/instancemethods/object.rb +3 -26
  32. data/lib/striuct/instancemethods/requirements.rb +16 -2
  33. data/lib/striuct/instancemethods/safety.rb +1 -1
  34. data/lib/striuct/instancemethods/setter.rb +58 -0
  35. data/lib/striuct/instancemethods/to_s.rb +2 -2
  36. data/lib/striuct/instancemethods/validation.rb +13 -7
  37. data/lib/striuct/instancemethods/values.rb +8 -7
  38. data/lib/striuct/requirements.rb +2 -5
  39. data/lib/striuct/{hashdeepdupulicatable.rb → singleton_class/hashdeepdupulicatable.rb} +2 -2
  40. data/lib/striuct/singleton_class.rb +3 -4
  41. data/striuct.gemspec +7 -4
  42. data/test/test_sglc-constructor.rb +36 -0
  43. data/test/test_subc-c-constructor.rb +88 -0
  44. data/test/{test_striuct-subclass-class-safety_naming.rb → test_subc-c-safety_naming.rb} +6 -5
  45. data/test/test_subc-f-enum.rb +139 -0
  46. data/test/test_subc-f-predicate.rb +647 -0
  47. data/test/{test_striuct-subclass-feature-validation_util.rb → test_subc-f-validation_util.rb} +10 -8
  48. data/test/test_subc-f_name.rb +525 -0
  49. data/test/{test_striuct-subclass-instance-accessor.rb → test_subc-i-accessor.rb} +3 -2
  50. data/test/test_subc-i-assign.rb +49 -0
  51. data/test/test_subc-i-cast.rb +27 -0
  52. data/test/{test_striuct-subclass-instance-default_value.rb → test_subc-i-default_value.rb} +2 -2
  53. data/test/{test_striuct-subclass-instance-hashlike.rb → test_subc-i-hashlike.rb} +0 -9
  54. data/test/{test_striuct-subclass-instance-lock.rb → test_subc-i-lock.rb} +8 -8
  55. data/test/{test_striuct-subclass-instance-to_s_family.rb → test_subc-i-to_s_family.rb} +1 -1
  56. data/test/{test_striuct-subclass-instance-validation_specific_conditions.rb → test_subc-i-validation_specific_conditions.rb} +50 -0
  57. data/test/test_version.rb +14 -0
  58. metadata +124 -80
  59. data/Manifest.txt +0 -97
  60. data/README.ja.old.rdoc +0 -298
  61. data/example/example.old.rdoc +0 -188
  62. data/lib/striuct/classmethods/hashy.rb +0 -22
  63. data/lib/striuct/classmethods/named.rb +0 -126
  64. data/lib/striuct/classmethods.rb +0 -1
  65. data/lib/striuct/instancemethods/inner_accessor.rb +0 -53
  66. data/lib/striuct/instancemethods/subscript.rb +0 -27
  67. data/lib/striuct/instancemethods.rb +0 -1
  68. data/lib/striuct/version.rb +0 -5
  69. data/test/test_striuct-singleton_class-define.rb +0 -19
  70. data/test/test_striuct-subclass-class-constructor.rb +0 -82
  71. data/test/test_striuct-subclass-class-validation.rb +0 -26
  72. data/test/test_striuct-subclass-instance-assign.rb +0 -30
  73. data/test/test_striuct-subclass-instance-enum.rb +0 -93
  74. data/test/test_striuct-subclass-instance-validation_functional_condition.rb +0 -51
  75. data/test/test_striuct-subclass-instance_names.rb +0 -18
  76. data/test/test_striuct-version.rb +0 -11
  77. /data/{History.rdoc → History.old(~0.3.n).rdoc} +0 -0
  78. /data/test/{test_striuct-subclass-class-macro.rb → test_subc-c-add_members.rb} +0 -0
  79. /data/test/{test_striuct-subclass-class-close.rb → test_subc-c-close_member.rb} +0 -0
  80. /data/test/{test_striuct-subclass-class-copy.rb → test_subc-c-copy.rb} +0 -0
  81. /data/test/{test_striuct-subclass-class-freeze.rb → test_subc-c-freeze.rb} +0 -0
  82. /data/test/{test_striuct-subclass-class-inheritable.rb → test_subc-c-inheritable.rb} +0 -0
  83. /data/test/{test_striuct-subclass-feature-alias_member.rb → test_subc-f-alias_member.rb} +0 -0
  84. /data/test/{test_striuct-subclass-feature-to_struct.rb → test_subc-f-to_struct.rb} +0 -0
  85. /data/test/{test_striuct-subclass-instance-adjuster.rb → test_subc-i-adjuster.rb} +0 -0
  86. /data/test/{test_striuct-subclass-instance-basic.rb → test_subc-i-basic.rb} +0 -0
  87. /data/test/{test_striuct-subclass-instance-compare.rb → test_subc-i-compare.rb} +0 -0
  88. /data/test/{test_striuct-subclass-instance-copy.rb → test_subc-i-copy.rb} +0 -0
  89. /data/test/{test_striuct-subclass-instance-freeze.rb → test_subc-i-freeze.rb} +0 -0
  90. /data/test/{test_striuct-subclass-instance-keyvalidatable.rb → test_subc-i-keyvalidatable.rb} +0 -0
  91. /data/test/{test_striuct-subclass-instance-validation_inference.rb → test_subc-i-validation_inference.rb} +0 -0
  92. /data/test/{test_striuct-subclass-instance-validation_with_getter.rb → test_subc-i-validation_with_getter.rb} +0 -0
@@ -0,0 +1,128 @@
1
+ class Striuct; module ClassMethods
2
+
3
+ # @group Basic Predicate
4
+
5
+ # @param [Symbol, String, #to_sym] name
6
+ def has_autonym?(name)
7
+ name = name.to_sym
8
+ rescue NoMethodError
9
+ false
10
+ else
11
+ @autonyms.include? name
12
+ end
13
+
14
+ alias_method :autonym?, :has_autonym?
15
+
16
+ # @param [Symbol, String, #to_sym] als
17
+ def has_alias?(als)
18
+ als = als.to_sym
19
+ rescue NoMethodError
20
+ false
21
+ else
22
+ @aliases.has_key? als
23
+ end
24
+
25
+ alias_method :alias?, :has_alias?
26
+ alias_method :aliased?, :has_alias? # obsolute
27
+
28
+ # @param [Symbol, String, #to_sym] name
29
+ def has_member?(name)
30
+ autonym_for_member name
31
+ rescue Exception
32
+ false
33
+ else
34
+ true
35
+ end
36
+
37
+ alias_method :member?, :has_member?
38
+
39
+ # @param [Integer, #to_int] index
40
+ def has_index?(index)
41
+ @autonyms.fetch index
42
+ rescue Exception
43
+ false
44
+ else
45
+ true
46
+ end
47
+
48
+ alias_method :index?, :has_index?
49
+
50
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
51
+ def has_key?(key)
52
+ has_member?(key) || has_index?(key)
53
+ end
54
+
55
+ alias_method :key?, :has_key?
56
+
57
+ # @param [Symbol, String, #to_sym] autonym
58
+ def with_aliases?(autonym)
59
+ autonym = autonym.to_sym
60
+ rescue NoMethodError
61
+ false
62
+ else
63
+ @aliases.has_value? autonym
64
+ end
65
+
66
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
67
+ def with_default?(key)
68
+ autonym = autonym_for_key key
69
+ rescue Exception
70
+ false
71
+ else
72
+ _attributes_for(autonym).with_default?
73
+ end
74
+
75
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
76
+ def with_condition?(key)
77
+ autonym = autonym_for_key key
78
+ rescue Exception
79
+ false
80
+ else
81
+ _attributes_for(autonym).with_condition?
82
+ end
83
+
84
+ alias_method :restrict?, :with_condition?
85
+
86
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
87
+ def with_inference?(key)
88
+ autonym = autonym_for_key key
89
+ rescue Exception
90
+ false
91
+ else
92
+ _attributes_for(autonym).with_inference?
93
+ end
94
+
95
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
96
+ def with_safety_getter?(key)
97
+ autonym = autonym_for_key key
98
+ rescue Exception
99
+ false
100
+ else
101
+ _attributes_for(autonym).with_safety_getter?
102
+ end
103
+
104
+ alias_method :with_safety_reader?, :with_safety_getter?
105
+
106
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
107
+ def with_safety_setter?(key)
108
+ autonym = autonym_for_key key
109
+ rescue Exception
110
+ false
111
+ else
112
+ _attributes_for(autonym).with_safety_setter?
113
+ end
114
+
115
+ alias_method :with_safety_writer?, :with_safety_setter?
116
+
117
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
118
+ def with_adjuster?(key)
119
+ autonym = autonym_for_key key
120
+ rescue Exception
121
+ false
122
+ else
123
+ _attributes_for(autonym).with_adjuster?
124
+ end
125
+
126
+ # @endgroup
127
+
128
+ end; end
@@ -1,14 +1,17 @@
1
+ require 'optionalargument'
2
+
3
+ require_relative 'attributes'
1
4
  require_relative 'inner'
2
- require_relative 'named'
5
+ require_relative 'names'
6
+ require_relative 'predicate'
3
7
  require_relative 'copy'
4
8
  require_relative 'fix'
5
9
  require_relative 'length'
6
10
  require_relative 'to_struct'
7
11
  require_relative 'constructor'
8
12
  require_relative 'enum'
9
- require_relative 'hashy'
10
13
  require_relative 'default'
11
14
  require_relative 'adjustment'
12
15
  require_relative 'macro'
13
- require_relative 'prevent_conflicts'
16
+ require_relative 'conflict_management'
14
17
  require_relative 'validation'
@@ -2,51 +2,13 @@ class Striuct; module ClassMethods
2
2
 
3
3
  # @group Validation
4
4
 
5
- # @param [Symbol, String] name
6
- # inference checker is waiting yet
7
- def inference?(name)
8
- autonym = autonym_for_name name
9
-
10
- _attributes_for(autonym).inference?
11
- end
12
-
13
- # @param [Symbol, String] name
14
- def has_condition?(name)
15
- autonym = autonym_for_name name
16
-
17
- _attributes_for(autonym).has_condition?
18
- end
19
-
20
- alias_method :restrict?, :has_condition?
21
- alias_method :has_validator?, :has_condition? # obsolute
22
-
23
- # @param [Symbol, String] name
24
- def condition_for(name)
25
- return nil unless has_condition? name
26
- autonym = autonym_for_name name
27
-
5
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
6
+ def condition_for(key)
7
+ autonym = autonym_for_key key
8
+ raise KeyError unless with_condition? autonym
9
+
28
10
  _attributes_for(autonym).condition
29
11
  end
30
-
31
- alias_method :validator_for, :condition_for # obsolute
32
-
33
- # @param [Symbol, String] name
34
- def safety_getter?(name)
35
- autonym = autonym_for_name name
36
-
37
- _attributes_for(autonym).validate_with_getter?
38
- end
39
-
40
- alias_method :safety_reader?, :safety_getter?
41
-
42
- # @param [Symbol, String] name
43
- def safety_setter?(name)
44
- autonym = autonym_for_name name
45
-
46
- _attributes_for(autonym).validate_with_setter?
47
- end
48
-
49
- alias_method :safety_writer?, :safety_setter?
50
12
 
51
13
  # @endgroup
52
14
 
@@ -1,5 +1,5 @@
1
- For developer
2
- ==============
1
+ Note
2
+ =====
3
3
 
4
- * This dir's tree is diveded under roles/features.
5
- Not under CONSTANT names.
4
+ This dir's tree is diveded under roles/features.
5
+ Not under CONSTANT names.
@@ -1,32 +1,30 @@
1
- require_relative 'subscript'
2
-
3
1
  class Striuct; module InstanceMethods
4
2
 
5
- # @group Assign
6
-
7
- alias_method :assign, :[]=
3
+ # @group Assign / Unassign
8
4
 
9
- # @param [Symbol, String] name
10
- def assign?(name)
11
- autonym = autonym_for_name name
5
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
6
+ def assign?(key)
7
+ autonym = autonym_for_key key
12
8
 
13
9
  @db.has_key? autonym
14
10
  end
15
11
 
16
- # @param [Symbol, String, Fixnum] key
12
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
13
+ # @return value / nil - value assigned under the key
17
14
  def unassign(key)
18
15
  raise "can't modify frozen #{self.class}" if frozen?
16
+
19
17
  autonym = autonym_for_key key
18
+ raise "can't modify locked member #{autonym}" if locked? autonym
20
19
 
21
20
  @db.delete autonym
22
21
  end
23
22
 
24
- alias_method :clear_at, :unassign # obsolute
25
- alias_method :reset_at, :unassign
23
+ alias_method :delete_at, :unassign
26
24
 
27
- # all members aren't assigned
25
+ # true if all members are not yet assigned
28
26
  def empty?
29
- _autonyms.none?{|autonym|assign? autonym}
27
+ _autonyms.none?{|autonym|@db.has_key? autonym}
30
28
  end
31
29
 
32
30
  # @endgroup
@@ -12,15 +12,12 @@ class Striuct; module InstanceMethods
12
12
  self.class.to_struct_class.new(*values)
13
13
  end
14
14
 
15
+ # @param [Boolean] include_no_assign
15
16
  # @return [Hash]
16
- def to_h(reject_no_assign=false)
17
- return @db.dup if reject_no_assign
17
+ def to_h(include_no_assign=true)
18
+ return @db.dup unless include_no_assign
18
19
 
19
- {}.tap {|h|
20
- each_pair do |autonym, val|
21
- h[autonym] = val
22
- end
23
- }
20
+ Hash[each_pair.to_a]
24
21
  end
25
22
 
26
23
  # @return [Array]
@@ -32,4 +29,4 @@ class Striuct; module InstanceMethods
32
29
 
33
30
  # @endgroup
34
31
 
35
- end; end
32
+ end; end
@@ -4,13 +4,14 @@ class Striuct; module InstanceMethods
4
4
 
5
5
  # @return [Boolean]
6
6
  def ==(other)
7
- __compare_all__? other, :==
7
+ other.instance_of?(self.class) &&
8
+ each_pair.all?{|autonym, val|other._get(autonym) == val}
8
9
  end
9
10
 
10
11
  alias_method :===, :==
11
12
 
12
13
  def eql?(other)
13
- __compare_all__? other, :eql?
14
+ other.instance_of?(self.class) && other._db.eql?(@db)
14
15
  end
15
16
 
16
17
  # @return [Integer]
@@ -18,11 +19,10 @@ class Striuct; module InstanceMethods
18
19
  @db.hash
19
20
  end
20
21
 
21
- private
22
+ protected
22
23
 
23
- # @param [Symbol] method
24
- def __compare_all__?(other, method)
25
- other.instance_of?(self.class) && each_pair.all?{|k, v|v.__send__ method, other[k]}
24
+ def _db
25
+ @db
26
26
  end
27
27
 
28
28
  # @endgroup
@@ -1,14 +1,36 @@
1
1
  class Striuct; module InstanceMethods
2
2
 
3
3
  # @group Default Value
4
-
5
- # @param [Symbol, String] name
6
- def default?(name)
7
- autonym = autonym_for_name name
4
+
5
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
6
+ def default?(key)
7
+ autonym = autonym_for_key key
8
8
 
9
- default_for(autonym) == self[autonym]
9
+ default_value_for(autonym) == fetch_by_autonym(autonym)
10
+ end
11
+
12
+ private
13
+
14
+ # @param [Symbol] target_autonyms - MUST already converted to native autonym
15
+ # @return [self]
16
+ def _set_defaults(*target_autonyms)
17
+ target_autonyms.each do |autonym|
18
+ if with_default? autonym
19
+ default = default_value_for autonym
20
+ _set autonym, (
21
+ if default_type_for(autonym) == :lazy
22
+ args = [self, autonym][0, default.arity]
23
+ default.call(*args)
24
+ else
25
+ default
26
+ end
27
+ )
28
+ end
29
+ end
30
+
31
+ self
10
32
  end
11
33
 
12
34
  # @endgroup
13
35
 
14
- end; end
36
+ end; end
@@ -1,33 +1,33 @@
1
- require 'forwardable'
2
-
3
1
  class Striuct; module InstanceMethods
4
-
5
- extend Forwardable
6
-
7
- # Forwardable has public/protected class_macro
8
- private_class_method(*Forwardable.instance_methods)
9
2
 
10
3
  # @group Delegate Class Methods
11
4
 
12
5
  def_delegators :'self.class',
13
- :_autonyms, :_nameable_for,
14
- :autonym_for, :autonym_for_name, :autonym_for_key,
15
- :aliases_for,
16
- :validator_for, :condition_for,
17
- :adjuster_for, :flavor_for,
18
- :members, :keys, :names, :autonyms, :all_members, :aliases,
19
- :has_member?, :member?, :has_key?, :key?,
6
+ :_autonyms,
7
+ :autonym_for_alias,
8
+ :autonym_for_member,
9
+ :autonym_for_index,
10
+ :autonym_for_key,
11
+ :autonyms, :members, :all_members, :aliases,
20
12
  :has_autonym?, :autonym?,
21
13
  :has_alias?, :alias?, :aliased?,
22
- :has_aliases?,
14
+ :has_member?, :member?,
15
+ :has_index?, :index?,
16
+ :has_key?, :key?,
17
+ :with_aliases?,
18
+ :aliases_for_autonym,
23
19
  :length, :size,
24
- :restrict?, :has_validator?, :has_condition?,
25
- :safety_getter?, :safety_reader?, :safety_setter?, :safty_writer?,
26
- :inference?,
27
- :has_default?, :default_value_for, :default_for, :default_type_for,
28
- :has_adjuster?, :has_flavor?
20
+ :with_condition?, :restrict?,
21
+ :condition_for,
22
+ :with_safety_getter?, :with_safety_reader?,
23
+ :with_safety_setter?, :with_safety_writer?,
24
+ :with_inference?,
25
+ :with_default?,
26
+ :default_value_for, :default_type_for,
27
+ :with_adjuster?,
28
+ :adjuster_for
29
29
 
30
- private :_autonyms, :_nameable_for
30
+ private :_autonyms
31
31
 
32
32
  # @endgroup
33
33
 
@@ -13,9 +13,7 @@ class Striuct; module InstanceMethods
13
13
  self
14
14
  end
15
15
 
16
- alias_method :each_name, :each_autonym
17
16
  alias_method :each_member, :each_autonym
18
- alias_method :each_key, :each_autonym
19
17
 
20
18
  # @yield [value]
21
19
  # @yieldparam [Object] value - sequential under defined
@@ -25,7 +23,7 @@ class Striuct; module InstanceMethods
25
23
  def each_value
26
24
  return to_enum(__callee__) unless block_given?
27
25
 
28
- each_autonym{|autonym|yield self[autonym]}
26
+ each_autonym{|autonym|yield _get(autonym)}
29
27
  end
30
28
 
31
29
  alias_method :each, :each_value
@@ -40,7 +38,7 @@ class Striuct; module InstanceMethods
40
38
  def each_pair
41
39
  return to_enum(__callee__) unless block_given?
42
40
 
43
- each_autonym{|autonym|yield autonym, self[autonym]}
41
+ each_autonym{|autonym|yield autonym, _get(autonym)}
44
42
  end
45
43
 
46
44
  # @yield [index]
@@ -66,9 +64,7 @@ class Striuct; module InstanceMethods
66
64
  self
67
65
  end
68
66
 
69
- alias_method :each_name_with_index, :each_autonym_with_index
70
67
  alias_method :each_member_with_index, :each_autonym_with_index
71
- alias_method :each_key_with_index, :each_autonym_with_index
72
68
 
73
69
  # @yield [value, index]
74
70
  # @yieldparam [Integer] index
@@ -78,7 +74,6 @@ class Striuct; module InstanceMethods
78
74
  return to_enum(__callee__) unless block_given?
79
75
 
80
76
  each_value.with_index{|value, index|yield value, index}
81
- self
82
77
  end
83
78
 
84
79
  alias_method :each_with_index, :each_value_with_index
@@ -96,9 +91,8 @@ class Striuct; module InstanceMethods
96
91
  yield autonym, value, index
97
92
  index += 1
98
93
  end
99
- self
100
94
  end
101
95
 
102
96
  # @endgroup
103
97
 
104
- end; end
98
+ end; end
@@ -0,0 +1,47 @@
1
+ class Striuct; module InstanceMethods
2
+
3
+ # @group Getter
4
+
5
+ # @param [Symbol, String, #to_sym] autonym
6
+ def fetch_by_autonym(autonym)
7
+ autonym = autonym.to_sym
8
+ raise NameError unless autonym? autonym
9
+
10
+ _get autonym
11
+ end
12
+
13
+ # @param [Symbol, String, #to_sym] member
14
+ def fetch_by_member(member)
15
+ _get autonym_for_member(member)
16
+ end
17
+
18
+ # @param [Integer, #to_int] index
19
+ def fetch_by_index(index)
20
+ _get autonym_for_index(index)
21
+ end
22
+
23
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
24
+ def fetch_by_key(key)
25
+ _get autonym_for_key(key)
26
+ end
27
+
28
+ alias_method :[], :fetch_by_key
29
+ alias_method :fetch, :fetch_by_key
30
+
31
+ protected
32
+
33
+ # @param [Symbol] autonym - MUST already converted to native autonym
34
+ def _get(autonym)
35
+ value = @db[autonym]
36
+
37
+ if with_safety_getter?(autonym) and !accept?(autonym, value)
38
+ raise ::Validation::InvalidReadingError,
39
+ "#{value.inspect} is deficient for #{autonym} in #{self.class}"
40
+ end
41
+
42
+ value
43
+ end
44
+
45
+ # @endgroup
46
+
47
+ end; end
@@ -1,10 +1,6 @@
1
- require_relative 'subscript'
2
-
3
1
  class Striuct; module InstanceMethods
4
2
 
5
3
  # @group Like Ruby's Hash
6
-
7
- alias_method :fetch, :[]
8
4
 
9
5
  def has_value?(value)
10
6
  @db.has_value? value
@@ -81,7 +77,7 @@ class Striuct; module InstanceMethods
81
77
  # @param [Symbol, String] name
82
78
  # @return [Array] e.g [autonym, value]
83
79
  def assoc(name)
84
- autonym = autonym_for_name name
80
+ autonym = autonym_for_member name
85
81
 
86
82
  [autonym, self[name]]
87
83
  end
@@ -119,4 +115,4 @@ class Striuct; module InstanceMethods
119
115
 
120
116
  # @endgroup
121
117
 
122
- end; end
118
+ end; end
@@ -1,10 +1,5 @@
1
- require 'keyvalidatable'
2
-
3
1
  class Striuct; module InstanceMethods
4
2
 
5
- extend Forwardable
6
- include KeyValidatable
7
-
8
3
  KeyValidatable.instance_methods.each do |feature|
9
4
  if specific_feature = feature.to_s.sub!('members'){'autonyms'}
10
5
  def_delegator :self, feature, specific_feature.to_sym
@@ -1,62 +1,54 @@
1
1
  class Striuct; module InstanceMethods
2
2
 
3
- # @group Lock
3
+ # @group Lock / Unlock
4
4
 
5
- # @overload lock(key)
6
- # lock a setter for key
7
- # @param [Symbol, String, Fixnum] key
8
- # @overload lock(true)
9
- # lock setters for all members
10
- # @param [true] true
5
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
11
6
  # @return [self]
12
- def lock(key=true)
7
+ def lock(key)
8
+ raise "can't modify frozen #{self.class}" if frozen?
9
+ autonym = autonym_for_key key
10
+
11
+ @locks[autonym] = true
12
+ self
13
+ end
14
+
15
+ # @return [self]
16
+ def lock_all
13
17
  raise "can't modify frozen #{self.class}" if frozen?
14
18
 
15
- if key.equal? true
16
- _autonyms.each do |autonym|
17
- @locks[autonym] = true
18
- end
19
- else
20
- autonym = autonym_for_key key
19
+ each_autonym do |autonym|
21
20
  @locks[autonym] = true
22
21
  end
23
-
24
- self
25
22
  end
26
23
 
27
- # @overload lock?(key)
28
- # predicate locking a setter for key
29
- # @param [Symbol, String, Fixnum] key
30
- # @overload lock?(true)
31
- # predicate locking for all members
32
- # @param [true] true
33
- def lock?(key=true)
34
- if key.equal? true
35
- _autonyms.all?{|autonym|@locks[autonym]}
36
- else
37
- autonym = autonym_for_key key
38
- @locks[autonym] || false
39
- end
24
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
25
+ def locked?(key)
26
+ autonym = autonym_for_key key
27
+
28
+ @locks.has_key? autonym
40
29
  end
41
30
 
42
- private
31
+ def all_locked?
32
+ _autonyms.all?{|autonym|@locks.has_key? autonym}
33
+ end
43
34
 
44
- # @overload unlock(key)
45
- # unlock a setter for key
46
- # @param [Symbol, String, Fixnum] key
47
- # @overload unlock(true)
48
- # unlock setters for all members
49
- # @param [true] true
50
- def unlock(key=true)
35
+ private
36
+
37
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
38
+ # @return [self]
39
+ def unlock(key)
51
40
  raise "can't modify frozen #{self.class}" if frozen?
41
+ autonym = autonym_for_key key
52
42
 
53
- if key.equal? true
54
- @locks.clear
55
- else
56
- autonym = autonym_for_key key
57
- @locks.delete autonym
58
- end
43
+ @locks.delete autonym
44
+ self
45
+ end
46
+
47
+ # @return [self]
48
+ def unlock_all
49
+ raise "can't modify frozen #{self.class}" if frozen?
59
50
 
51
+ @locks.clear
60
52
  self
61
53
  end
62
54