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
data/.travis.yml CHANGED
@@ -12,3 +12,6 @@ matrix:
12
12
  - rvm: rbx-19mode
13
13
  - rvm: jruby-head
14
14
  - rvm: jruby-19mode
15
+
16
+ before_install:
17
+ - gem install bundler
data/.yardopts CHANGED
@@ -1 +1 @@
1
- --readme README.md --private lib/**/*.rb --exclude inner|instancemethods/singleton_class - LICENSE
1
+ --readme README.md --private lib/**/*.rb --exclude attributes|hashdeepdupulicatable - LICENSE
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  (The MIT X11 License)
2
2
 
3
- Copyright (C) 2011 Kenichi Kamiya
3
+ Copyright (c) 2011 Kenichi Kamiya
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining
6
6
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,8 +1,6 @@
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
-
6
4
  Description
7
5
  -----------
8
6
 
@@ -103,6 +101,7 @@ Requirements
103
101
 
104
102
  * Ruby - [1.9.2 or later](http://travis-ci.org/#!/kachick/striuct)
105
103
  * [validation](https://github.com/kachick/validation) - 0.0.3
104
+ * [optionalargument](https://github.com/kachick/optionalargument) - 0.0.1
106
105
  * [keyvalidatable](https://github.com/kachick/keyvalidatable) - 0.0.3
107
106
 
108
107
  Install
@@ -112,6 +111,11 @@ Install
112
111
  $ gem install striuct
113
112
  ```
114
113
 
114
+ Build Status
115
+ -------------
116
+
117
+ [![Build Status](https://secure.travis-ci.org/kachick/striuct.png)](http://travis-ci.org/kachick/striuct)
118
+
115
119
  Link
116
120
  ----
117
121
 
@@ -126,6 +130,6 @@ License
126
130
  --------
127
131
 
128
132
  The MIT X11 License
129
- Copyright (C) 2011 Kenichi Kamiya
133
+ Copyright (c) 2011 Kenichi Kamiya
130
134
  See the file LICENSE for further details.
131
135
 
data/example/example1.rb CHANGED
@@ -135,10 +135,11 @@ class SafetyNaming < Striuct.new
135
135
  debug $!
136
136
  end
137
137
 
138
- # set lower
139
- protect_level :struct
138
+ # tempolary set lower
139
+ conflict_management :struct do
140
+ member :__send__, :'? !'
141
+ end
140
142
 
141
- member :__send__, :'? !'
142
143
  end
143
144
 
144
145
  # 2. Struct+ "Handy"
@@ -231,4 +232,4 @@ p a
231
232
 
232
233
  p MyC.new.to_struct
233
234
  myc = Striuct.new :a, :b
234
- p myc.new.to_struct
235
+ p myc.new.to_struct
@@ -1,5 +1,10 @@
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 only divided by CONSTANT names.
6
+
7
+ CONSTANT
8
+ --------
9
+
10
+ * attributes - Attributes
@@ -2,24 +2,13 @@ class Striuct; module ClassMethods
2
2
 
3
3
  # @group Adjuster
4
4
 
5
- # @param [Symbol, String] name
6
- def has_adjuster?(name)
7
- autonym = autonym_for_name name
8
-
9
- _attributes_for(autonym).has_adjuster?
10
- end
11
-
12
- alias_method :has_flavor?, :has_adjuster? # obsolute
13
-
14
- # @param [Symbol, String] name
15
- def adjuster_for(name)
16
- return nil unless has_adjuster? name
17
- autonym = autonym_for_name name
5
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
6
+ def adjuster_for(key)
7
+ autonym = autonym_for_key key
8
+ raise KeyError unless with_adjuster? autonym
18
9
 
19
10
  _attributes_for(autonym).adjuster
20
11
  end
21
-
22
- alias_method :flavor_for, :adjuster_for # obsolute
23
12
 
24
13
  # @endgroup
25
14
 
@@ -1,25 +1,23 @@
1
- class Striuct
1
+ class Striuct; module ClassMethods
2
2
 
3
3
  # Attributes for each Member(Autonym)
4
4
  class Attributes
5
5
 
6
- VALUES = [:condition, :adjuster].freeze
7
- BOOLEANS = [
8
- :inference,
9
- :validate_with_setter,
10
- :validate_with_getter
11
- ].freeze
6
+ VALUES = [ :condition,
7
+ :adjuster ].freeze
8
+
9
+ BOOLEANS = [ :inference,
10
+ :safety_setter,
11
+ :safety_getter ].freeze
12
12
 
13
13
  def initialize
14
- @hash = {
15
- inference: false,
16
- validate_with_setter: false,
17
- validate_with_getter: false
18
- }
14
+ @hash = { inference: false,
15
+ safety_setter: false,
16
+ safety_getter: false }
19
17
  end
20
18
 
21
19
  VALUES.each do |role|
22
- define_method "has_#{role}?".to_sym do
20
+ define_method :"with_#{role}?" do
23
21
  @hash.has_key? role
24
22
  end
25
23
 
@@ -38,14 +36,14 @@ class Striuct
38
36
 
39
37
  def adjuster=(adjuster)
40
38
  unless ::Validation.adjustable? adjuster
41
- raise ArgumentError, "wrong number of block argument #{arity} for 1"
39
+ raise ArgumentError, 'wrong object for adjuster'
42
40
  end
43
41
 
44
42
  @hash[:adjuster] = adjuster
45
43
  end
46
44
 
47
45
  BOOLEANS.each do |role|
48
- define_method "#{role}?".to_sym do
46
+ define_method :"with_#{role}?" do
49
47
  @hash.fetch role
50
48
  end
51
49
 
@@ -56,7 +54,7 @@ class Striuct
56
54
  end
57
55
  end
58
56
 
59
- def has_default?
57
+ def with_default?
60
58
  @hash.has_key? :default_value
61
59
  end
62
60
 
@@ -111,4 +109,4 @@ class Striuct
111
109
  private_constant :Attributes
112
110
  end
113
111
 
114
- end
112
+ end; end
@@ -1,7 +1,8 @@
1
1
  class Striuct; module ClassMethods
2
2
 
3
- # @group Prevent Naming Conflicts
3
+ # @group Member Conflict Management
4
4
 
5
+ # @return [Hash] Symbol => Fixnum
5
6
  NAMING_RISKS = {
6
7
  conflict: 10,
7
8
  no_identifier: 9,
@@ -10,32 +11,54 @@ class Striuct; module ClassMethods
10
11
  strict: 0
11
12
  }.freeze
12
13
 
13
- PROTECT_LEVELS = {
14
+ # @return [Hash] Symbol => Hash
15
+ CONFLICT_MANAGEMENT_LEVELS = {
14
16
  struct: {error: 99, warn: 99},
15
17
  warning: {error: 99, warn: 5},
16
18
  error: {error: 9, warn: 5},
17
19
  prevent: {error: 5, warn: 1},
18
20
  nervous: {error: 1, warn: 1}
19
21
  }.each(&:freeze).freeze
22
+
23
+ # @return [Symbol]
24
+ DEFAULT_CONFLICT_MANAGEMENT_LEVEL = :prevent
20
25
 
21
26
  # @param [Object] name
22
27
  # accpeptable the name into own member, under protect level of runtime
23
28
  def cname?(name)
24
- _check_safety_naming(_nameable_for name){|r|r}
29
+ _check_safety_naming(name.to_sym){|r|r}
25
30
  rescue Exception
26
31
  false
27
32
  end
33
+
34
+ attr_reader :conflict_management_level
28
35
 
29
36
  private
37
+
38
+ # @param [Symbol, String, #to_sym] level
39
+ # @return [Symbol] level
40
+ # change level of management conflict member names
41
+ def set_conflict_management_level(level)
42
+ level = level.to_sym
43
+ raise NameError unless CONFLICT_MANAGEMENT_LEVELS.has_key? level
30
44
 
31
- # @param [Symbol] level
32
- # @return [nil]
33
- # change protect level for risk of naming members
34
- def protect_level(level)
35
- raise NameError unless PROTECT_LEVELS.has_key? level
45
+ @conflict_management_level = level
46
+ end
47
+
48
+ # @param [Symbol, String, #to_sym] level
49
+ # @see [#set_conflict_management_level]
50
+ # @yieldreturn [self]
51
+ # @return [void]
52
+ # @raise [ArgumentError] if no block given
53
+ # temp scope of a conflict_management_level
54
+ def conflict_management(level=DEFAULT_CONFLICT_MANAGEMENT_LEVEL)
55
+ before = @conflict_management_level
56
+ set_conflict_management_level level
36
57
 
37
- @protect_level = level
38
- nil
58
+ yield
59
+ ensure
60
+ @conflict_management_level = before
61
+ self
39
62
  end
40
63
 
41
64
  # @param [Symbol] name
@@ -43,16 +66,16 @@ class Striuct; module ClassMethods
43
66
  # @yieldreturn [Boolean]
44
67
  def _check_safety_naming(name)
45
68
  estimation = _estimate_naming name
46
- risk = NAMING_RISKS[estimation]
47
- plevels = PROTECT_LEVELS[@protect_level]
69
+ risk = NAMING_RISKS.fetch estimation
70
+ plevels = CONFLICT_MANAGEMENT_LEVELS.fetch @conflict_management_level
48
71
  caution = "undesirable naming '#{name}', because #{estimation}"
49
72
 
50
73
  r = (
51
74
  case
52
- when risk >= plevels[:error]
75
+ when risk >= plevels.fetch(:error)
53
76
  raise NameError, caution unless block_given?
54
77
  false
55
- when risk >= plevels[:warn]
78
+ when risk >= plevels.fetch(:warn)
56
79
  warn caution unless block_given?
57
80
  false
58
81
  else
@@ -9,10 +9,9 @@ class Striuct; module ClassMethods
9
9
  new_instance(*values)
10
10
  end
11
11
 
12
- alias_method :load_values, :for_values
13
12
  alias_method :new, :for_values
14
13
 
15
- # @param [Hash, Struct] pairs
14
+ # @param [Hash, Struct, Striuct, #each_pair] pairs
16
15
  # @return [Striuct]
17
16
  def for_pairs(pairs)
18
17
  raise TypeError, 'no pairs object' unless pairs.respond_to?(:each_pair)
@@ -26,7 +25,6 @@ class Striuct; module ClassMethods
26
25
  }
27
26
  end
28
27
 
29
- alias_method :load_pairs, :for_pairs
30
28
  alias_method :[], :for_pairs
31
29
 
32
30
  # for build the fixed object
@@ -55,7 +53,7 @@ class Striuct; module ClassMethods
55
53
  raise Validation::InvalidWritingError, "invalids members are, yet '#{invalids.inspect} in #{self}'"
56
54
  end
57
55
 
58
- instance.lock if lock
56
+ instance.lock_all if lock
59
57
  }
60
58
  end
61
59
 
@@ -30,7 +30,7 @@ class Striuct; module ClassMethods
30
30
  autonyms = @autonyms.dup
31
31
  aliases = @aliases.dup
32
32
  attributes = @attributes.deep_dup
33
- protect_level = @protect_level
33
+ conflict_management = @conflict_management_level
34
34
 
35
35
  subclass.class_eval do
36
36
  original_inherited subclass
@@ -38,7 +38,7 @@ class Striuct; module ClassMethods
38
38
  @autonyms = autonyms
39
39
  @aliases = aliases
40
40
  @attributes = attributes
41
- @protect_level = protect_level
41
+ @conflict_management_level = conflict_management
42
42
  end
43
43
  end
44
44
 
@@ -1,29 +1,20 @@
1
1
  class Striuct; module ClassMethods
2
2
 
3
3
  # @group With default value
4
-
5
- # @param [Symbol, String] name
6
- def has_default?(name)
7
- autonym = autonym_for_name name
8
-
9
- _attributes_for(autonym).has_default?
10
- end
11
-
12
- # @param [Symbol, String] name
13
- def default_value_for(name)
14
- autonym = autonym_for_name name
15
- raise "#{name} has no default value" unless has_default? autonym
4
+
5
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
6
+ def default_value_for(key)
7
+ autonym = autonym_for_key key
8
+ raise KeyError unless with_default? autonym
16
9
 
17
10
  _attributes_for(autonym).default_value
18
11
  end
19
12
 
20
- alias_method :default_for, :default_value_for
21
-
22
- # @param [Symbol, String] name
13
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
23
14
  # @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
15
+ def default_type_for(key)
16
+ autonym = autonym_for_key key
17
+ raise KeyError unless with_default? autonym
27
18
 
28
19
  _attributes_for(autonym).default_type
29
20
  end
@@ -4,7 +4,7 @@ class Striuct; module ClassMethods
4
4
 
5
5
  # @yield [autonym]
6
6
  # @yieldparam [Symbol] autonym - sequential under defined
7
- # @yieldreturn [self]
7
+ # @yieldreturn [Class] self
8
8
  # @return [Enumerator]
9
9
  def each_autonym
10
10
  return to_enum(__callee__) unless block_given?
@@ -13,12 +13,11 @@ class Striuct; module ClassMethods
13
13
  self
14
14
  end
15
15
 
16
- alias_method :each_name, :each_autonym
17
16
  alias_method :each_member, :each_autonym
18
17
 
19
18
  # @yield [index]
20
19
  # @yieldparam [Integer] Index
21
- # @yieldreturn [self]
20
+ # @yieldreturn [Class] self
22
21
  # @return [Enumerator]
23
22
  def each_index
24
23
  return to_enum(__callee__) unless block_given?
@@ -30,7 +29,7 @@ class Striuct; module ClassMethods
30
29
  # @yield [autonym, index]
31
30
  # @yieldparam [Symbol] autonym
32
31
  # @yieldparam [Integer] index
33
- # @yieldreturn [self]
32
+ # @yieldreturn [Class] self
34
33
  # @return [Enumerator]
35
34
  def each_autonym_with_index
36
35
  return to_enum(__callee__) unless block_given?
@@ -39,7 +38,6 @@ class Striuct; module ClassMethods
39
38
  self
40
39
  end
41
40
 
42
- alias_method :each_name_with_index, :each_autonym_with_index
43
41
  alias_method :each_member_with_index, :each_autonym_with_index
44
42
 
45
43
  # @endgroup
@@ -5,7 +5,7 @@ class Striuct; module ClassMethods
5
5
  private
6
6
 
7
7
  def _add_autonym(autonym)
8
- autonym = _nameable_for autonym
8
+ autonym = autonym.to_sym
9
9
  raise NameError, 'already defined' if member? autonym
10
10
 
11
11
  @attributes[autonym] = Attributes.new
@@ -16,7 +16,7 @@ class Striuct; module ClassMethods
16
16
  @attributes.fetch autonym
17
17
  end
18
18
 
19
- def _def_getter!(autonym)
19
+ def _def_getter(autonym)
20
20
  define_method autonym do
21
21
  _get autonym
22
22
  end
@@ -24,8 +24,8 @@ class Striuct; module ClassMethods
24
24
  nil
25
25
  end
26
26
 
27
- def _def_setter!(autonym, condition, &adjuster)
28
- unless Validation::Condition::ANYTHING.equal? condition
27
+ def _def_setter(autonym, condition, &adjuster)
28
+ unless ANYTHING.equal? condition
29
29
  _attributes_for(autonym).condition = condition
30
30
  end
31
31
 
@@ -40,12 +40,8 @@ class Striuct; module ClassMethods
40
40
  nil
41
41
  end
42
42
 
43
- def _found_family!(_caller, autonym, our)
44
- family = our.class
45
-
46
- raise 'must not happen' unless inference?(autonym) and
47
- autonym?(autonym) and
48
- _caller.instance_of?(self)
43
+ def _found_family(autonym, other)
44
+ family = other.class
49
45
 
50
46
  _attributes_for(autonym).condition = family
51
47
  _attributes_for(autonym).inference = false
@@ -1,26 +1,21 @@
1
- require 'keyvalidatable'
2
-
3
1
  class Striuct; module ClassMethods
4
2
 
5
3
  # @group Macro for Member Definition
6
4
 
7
- VALID_MEMBER_OPTIONS = [
8
- :default,
9
- :default_proc,
10
- :inference,
11
- :reader_validation,
12
- :getter_validation,
13
- :writer_validation,
14
- :setter_validation
15
- ].freeze
16
-
17
- DEFAULT_MEMBER_OPTIONS = {
18
- setter_validation: true
19
- }.freeze
5
+ ANYTHING = ::Validation::Condition::ANYTHING
20
6
 
7
+ ADD_MEMBER_OPTIONS = OptionalArgument.define {
8
+ opt :default_value, aliases: [:default]
9
+ opt :default_proc, aliases: [:lazy_default]
10
+ conflict :default_value, :default_proc
11
+ opt :inference, default: false
12
+ opt :setter_validation, aliases: [:writer_validation], default: true
13
+ opt :getter_validation, aliases: [:reader_validation], default: false
14
+ }
15
+
21
16
  private
22
17
 
23
- # @param [Symbol, String] autonym
18
+ # @param [Symbol, String, #to_sym] autonym
24
19
  # @param [#===, Proc, Method, ANYTHING] condition
25
20
  # @param [Hash] options
26
21
  # @option options [BasicObject] :default
@@ -31,42 +26,32 @@ class Striuct; module ClassMethods
31
26
  # @option options [Boolean] :writer_validation
32
27
  # @option options [Boolean] :setter_validation
33
28
  # @return [nil]
34
- def add_member(autonym, condition=Validation::Condition::ANYTHING, options=DEFAULT_MEMBER_OPTIONS, &adjuster)
29
+ def add_member(autonym, condition=ANYTHING, options={}, &adjuster)
35
30
  raise "can't modify frozen Class" if frozen?
36
31
  raise "already closed to add member in #{self}" if closed?
37
- options = DEFAULT_MEMBER_OPTIONS.merge(options).extend(KeyValidatable)
38
- options.validate_keys let: VALID_MEMBER_OPTIONS
39
- if options.has_key?(:default) and options.has_key?(:default_proc)
40
- raise ArgumentError, 'It is not able to choose "default" with "default_proc" in options'
41
- end
42
32
 
43
- autonym = _nameable_for autonym # First difinition for an autonym
33
+ options = ADD_MEMBER_OPTIONS.parse options
34
+ autonym = autonym.to_sym # First difinition for an autonym
44
35
 
45
36
  raise ArgumentError, %Q!already exist name "#{autonym}"! if member? autonym
46
37
  _check_safety_naming autonym
47
38
  _add_autonym autonym
48
39
 
49
- if options[:setter_validation] or options[:writer_validation]
50
- _attributes_for(autonym).validate_with_setter = true
51
- end
40
+ _attributes_for(autonym).safety_setter = !!options.setter_validation
41
+ _attributes_for(autonym).safety_getter = !!options.getter_validation
52
42
 
53
- if options[:getter_validation] or options[:reader_validation]
54
- _attributes_for(autonym).validate_with_getter = true
55
- end
56
-
57
- if options[:inference]
43
+ if options.inference
58
44
  _attributes_for(autonym).inference = true
59
45
  end
60
46
 
61
- _def_getter! autonym
62
- _def_setter! autonym, condition, &adjuster
63
-
64
- if options.has_key?(:default)
65
- set_default_value autonym, options.fetch(:default)
66
- end
47
+ _def_getter autonym
48
+ _def_setter autonym, condition, &adjuster
67
49
 
68
- if options.has_key?(:default_proc)
69
- set_default_value autonym, &options.fetch(:default_proc)
50
+ case
51
+ when options.default_value?
52
+ set_default_value autonym, options.default_value
53
+ when options.default_proc?
54
+ set_default_value autonym, &options.default_proc
70
55
  end
71
56
 
72
57
  nil
@@ -74,8 +59,8 @@ class Striuct; module ClassMethods
74
59
 
75
60
  alias_method :member, :add_member
76
61
 
77
- # @param [Symbol, String] autonym
78
- # @param [Symbol, String] autonyms
62
+ # @param [Symbol, String, #to_sym] autonym
63
+ # @param [Symbol, String, #to_sym] autonyms
79
64
  # @return [nil]
80
65
  def add_members(autonym, *autonyms)
81
66
  raise "can't modify frozen Class" if frozen?
@@ -85,14 +70,14 @@ class Striuct; module ClassMethods
85
70
  nil
86
71
  end
87
72
 
88
- # @param [Symbol, String] aliased
89
- # @param [Symbol, String] autonym
73
+ # @param [Symbol, String, #to_sym] aliased
74
+ # @param [Symbol, String, #to_sym] autonym
90
75
  # @return [nil]
91
76
  def alias_member(aliased, autonym)
92
77
  raise "can't modify frozen Class" if frozen?
93
78
  raise "already closed to add members in #{self}" if closed?
94
- autonym = autonym_for_name autonym
95
- aliased = _nameable_for aliased
79
+ autonym = autonym_for_member autonym
80
+ aliased = aliased.to_sym
96
81
  raise ArgumentError, %Q!already exist name "#{aliased}"! if member? aliased
97
82
  _check_safety_naming aliased
98
83
 
@@ -102,13 +87,13 @@ class Striuct; module ClassMethods
102
87
  nil
103
88
  end
104
89
 
105
- # @param [Symbol, String] name
90
+ # @param [Symbol, String, #to_sym] name
106
91
  # @return [nil]
107
92
  def set_default_value(name, value=nil, &block)
108
93
  raise "can't modify frozen Class" if frozen?
109
94
  raise "already closed to modify member attributes in #{self}" if closed?
110
- autonym = autonym_for_name(name)
111
- raise "already settled default value for #{name}" if has_default? autonym
95
+ autonym = autonym_for_member name
96
+ raise "already settled default value for #{name}" if with_default? autonym
112
97
 
113
98
  if block_given?
114
99
  unless value.nil?
@@ -0,0 +1,75 @@
1
+ class Striuct; module ClassMethods
2
+
3
+ # @group Named
4
+
5
+ # @return [Array<Symbol>]
6
+ def autonyms
7
+ @autonyms.dup
8
+ end
9
+
10
+ alias_method :members, :autonyms
11
+
12
+ # @return [Array<Symbol>]
13
+ def all_members
14
+ @autonyms + @aliases.keys
15
+ end
16
+
17
+ # @param [Symbol, String, #to_sym] als
18
+ # @return [Symbol]
19
+ def autonym_for_alias(als)
20
+ @aliases.fetch als.to_sym
21
+ rescue NoMethodError
22
+ raise TypeError
23
+ rescue KeyError
24
+ raise NameError
25
+ end
26
+
27
+ # @param [Symbol, String, #to_sym] name - autonym / aliased
28
+ # @return [Symbol]
29
+ def autonym_for_member(name)
30
+ raise TypeError unless name.respond_to? :to_sym
31
+ name = name.to_sym
32
+
33
+ @autonyms.include?(name) ? name : autonym_for_alias(name)
34
+ end
35
+
36
+ # @param [Index, #to_int] index
37
+ # @return [Symbol] autonym
38
+ def autonym_for_index(index)
39
+ @autonyms.fetch index
40
+ end
41
+
42
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
43
+ # @return [Symbol] autonym
44
+ def autonym_for_key(key)
45
+ key.respond_to?(:to_sym) ? autonym_for_member(key) : autonym_for_index(key)
46
+ rescue NameError, IndexError, TypeError
47
+ raise KeyError
48
+ end
49
+
50
+ # @param [Symbol, String, #to_sym] autonym
51
+ # @return [Array<Symbol>]
52
+ def aliases_for_autonym(autonym)
53
+ raise TypeError unless autonym.respond_to?(:to_sym)
54
+ autonym = autonym.to_sym
55
+ raise NameError unless with_aliases? autonym
56
+
57
+ @aliases.select{|als, aut|autonym == aut}.keys
58
+ end
59
+
60
+ # @return [Hash] alias => autonym
61
+ def aliases
62
+ @aliases.dup
63
+ end
64
+
65
+ private
66
+
67
+ # for direct access inner data from own instance
68
+ # @return [Hash] alias => autonym
69
+ def _autonyms
70
+ @autonyms
71
+ end
72
+
73
+ # @endgroup
74
+
75
+ end; end