striuct 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/.gitignore +2 -0
  2. data/.yardopts +1 -0
  3. data/History.rdoc +6 -0
  4. data/Manifest.txt +38 -17
  5. data/README.md +6 -4
  6. data/{benchmarks → benchmark}/basics.rb +0 -0
  7. data/{examples → example}/README.rb +0 -0
  8. data/{examples → example}/example.old.rdoc +0 -0
  9. data/{examples → example}/example1.rb +0 -0
  10. data/{examples → example}/example2.rb +0 -0
  11. data/{examples → example}/see_trace.rb +0 -0
  12. data/lib/striuct/classmethods/adjustment.rb +31 -0
  13. data/lib/striuct/classmethods/constructor.rb +8 -8
  14. data/lib/striuct/classmethods/default.rb +28 -0
  15. data/lib/striuct/classmethods/enum.rb +49 -0
  16. data/lib/striuct/classmethods/inner.rb +25 -108
  17. data/lib/striuct/classmethods/length.rb +14 -0
  18. data/lib/striuct/classmethods/macro.rb +38 -41
  19. data/lib/striuct/classmethods/named.rb +94 -0
  20. data/lib/striuct/classmethods/object.rb +24 -0
  21. data/lib/striuct/classmethods/prevent_conflicts.rb +89 -0
  22. data/lib/striuct/classmethods/requiremnets.rb +9 -4
  23. data/lib/striuct/classmethods/to_struct.rb +26 -0
  24. data/lib/striuct/classmethods/validation.rb +56 -0
  25. data/lib/striuct/instancemethods/assign.rb +31 -0
  26. data/lib/striuct/instancemethods/compare.rb +31 -0
  27. data/lib/striuct/instancemethods/default.rb +14 -0
  28. data/lib/striuct/instancemethods/delegate_class_methods.rb +20 -0
  29. data/lib/striuct/instancemethods/enum.rb +104 -0
  30. data/lib/striuct/instancemethods/{hashlike.rb → hashy.rb} +39 -45
  31. data/lib/striuct/instancemethods/inner.rb +16 -77
  32. data/lib/striuct/instancemethods/lock.rb +51 -0
  33. data/lib/striuct/instancemethods/object.rb +47 -0
  34. data/lib/striuct/instancemethods/requirements.rb +12 -3
  35. data/lib/striuct/instancemethods/safety.rb +2 -66
  36. data/lib/striuct/instancemethods/singleton_class.rb +4 -1
  37. data/lib/striuct/instancemethods/subscript.rb +55 -0
  38. data/lib/striuct/instancemethods/to_struct.rb +12 -0
  39. data/lib/striuct/instancemethods/validation.rb +25 -0
  40. data/lib/striuct/instancemethods/values.rb +64 -0
  41. data/lib/striuct/singleton_class.rb +10 -10
  42. data/lib/striuct/specificcontainer.rb +3 -1
  43. data/lib/striuct/version.rb +1 -1
  44. data/striuct.gemspec +1 -1
  45. data/test/test_striuct_subclass_alias_member.rb +3 -3
  46. data/test/test_striuct_subclass_for_pairs.rb +8 -1
  47. metadata +32 -17
  48. data/lib/striuct/classmethods/basic.rb +0 -54
  49. data/lib/striuct/classmethods/constants.rb +0 -23
  50. data/lib/striuct/classmethods/handy.rb +0 -124
  51. data/lib/striuct/classmethods/safety.rb +0 -52
  52. data/lib/striuct/instancemethods/basic.rb +0 -135
  53. data/lib/striuct/instancemethods/handy.rb +0 -91
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.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-01 00:00:00.000000000 Z
12
+ date: 2012-09-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: validation
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: 0.0.2
37
+ version: 0.0.3
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: 0.0.2
45
+ version: 0.0.3
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: yard
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -68,6 +68,7 @@ extra_rdoc_files: []
68
68
  files:
69
69
  - .gitignore
70
70
  - .travis.yml
71
+ - .yardopts
71
72
  - Gemfile
72
73
  - History.rdoc
73
74
  - LICENSE
@@ -75,30 +76,44 @@ files:
75
76
  - README.ja.old.rdoc
76
77
  - README.md
77
78
  - Rakefile
78
- - benchmarks/basics.rb
79
- - examples/README.rb
80
- - examples/example.old.rdoc
81
- - examples/example1.rb
82
- - examples/example2.rb
83
- - examples/see_trace.rb
79
+ - benchmark/basics.rb
80
+ - example/README.rb
81
+ - example/example.old.rdoc
82
+ - example/example1.rb
83
+ - example/example2.rb
84
+ - example/see_trace.rb
84
85
  - lib/striuct.rb
85
86
  - lib/striuct/classmethods.rb
86
- - lib/striuct/classmethods/basic.rb
87
- - lib/striuct/classmethods/constants.rb
87
+ - lib/striuct/classmethods/adjustment.rb
88
88
  - lib/striuct/classmethods/constructor.rb
89
- - lib/striuct/classmethods/handy.rb
89
+ - lib/striuct/classmethods/default.rb
90
+ - lib/striuct/classmethods/enum.rb
90
91
  - lib/striuct/classmethods/inner.rb
92
+ - lib/striuct/classmethods/length.rb
91
93
  - lib/striuct/classmethods/macro.rb
94
+ - lib/striuct/classmethods/named.rb
95
+ - lib/striuct/classmethods/object.rb
96
+ - lib/striuct/classmethods/prevent_conflicts.rb
92
97
  - lib/striuct/classmethods/requiremnets.rb
93
- - lib/striuct/classmethods/safety.rb
98
+ - lib/striuct/classmethods/to_struct.rb
99
+ - lib/striuct/classmethods/validation.rb
94
100
  - lib/striuct/instancemethods.rb
95
- - lib/striuct/instancemethods/basic.rb
96
- - lib/striuct/instancemethods/handy.rb
97
- - lib/striuct/instancemethods/hashlike.rb
101
+ - lib/striuct/instancemethods/assign.rb
102
+ - lib/striuct/instancemethods/compare.rb
103
+ - lib/striuct/instancemethods/default.rb
104
+ - lib/striuct/instancemethods/delegate_class_methods.rb
105
+ - lib/striuct/instancemethods/enum.rb
106
+ - lib/striuct/instancemethods/hashy.rb
98
107
  - lib/striuct/instancemethods/inner.rb
108
+ - lib/striuct/instancemethods/lock.rb
109
+ - lib/striuct/instancemethods/object.rb
99
110
  - lib/striuct/instancemethods/requirements.rb
100
111
  - lib/striuct/instancemethods/safety.rb
101
112
  - lib/striuct/instancemethods/singleton_class.rb
113
+ - lib/striuct/instancemethods/subscript.rb
114
+ - lib/striuct/instancemethods/to_struct.rb
115
+ - lib/striuct/instancemethods/validation.rb
116
+ - lib/striuct/instancemethods/values.rb
102
117
  - lib/striuct/requirements.rb
103
118
  - lib/striuct/singleton_class.rb
104
119
  - lib/striuct/specificcontainer.rb
@@ -1,54 +0,0 @@
1
- class Striuct; module ClassMethods
2
- # @group Basic
3
-
4
- # @return [Array<Symbol>]
5
- def names
6
- @names.dup
7
- end
8
-
9
- alias_method :autonyms, :names
10
- alias_method :members, :names
11
- alias_method :keys, :names
12
-
13
- # @return [Array<Symbol>]
14
- def all_members
15
- @names + @aliases.keys
16
- end
17
-
18
- def has_member?(name)
19
- autonym_for name
20
- rescue Exception
21
- false
22
- else
23
- true
24
- end
25
-
26
- alias_method :member?, :has_member?
27
- alias_method :has_key?, :has_member?
28
- alias_method :key?, :has_key?
29
-
30
- # @return [Integer]
31
- def length
32
- @names.length
33
- end
34
-
35
- alias_method :size, :length
36
-
37
- # @return [self]
38
- def freeze
39
- __stores__.each(&:freeze)
40
- super
41
- end
42
-
43
- def dup
44
- r = super
45
- @names, @flavors, @defaults, @aliases,
46
- @setter_validations, @getter_validations =
47
- *[@names, @flavors, @defaults, @aliases,
48
- @setter_validations, @getter_validations].map(&:dup)
49
- @conditions, @inferences = @conditions.dup, @inferences.dup
50
- r
51
- end
52
-
53
- # @endgroup
54
- end; end
@@ -1,23 +0,0 @@
1
- class Striuct
2
-
3
- module ClassMethods
4
-
5
- NAMING_RISKS = {
6
- conflict: 10,
7
- no_identifier: 9,
8
- bad_manners: 5,
9
- no_ascii: 3,
10
- strict: 0
11
- }.freeze
12
-
13
- PROTECT_LEVELS = {
14
- struct: {error: 99, warn: 99},
15
- warning: {error: 99, warn: 5},
16
- error: {error: 9, warn: 5},
17
- prevent: {error: 5, warn: 1},
18
- nervous: {error: 1, warn: 1}
19
- }.each(&:freeze).freeze
20
-
21
- end
22
-
23
- end
@@ -1,124 +0,0 @@
1
- class Striuct; module ClassMethods
2
- # @group Struct+ Handy
3
-
4
- # @yield [name]
5
- # @yieldparam [Symbol] name - sequential under defined
6
- # @yieldreturn [self]
7
- # @return [Enumerator]
8
- def each_name
9
- return to_enum(__method__) unless block_given?
10
- _names.each{|name|yield name}
11
- self
12
- end
13
-
14
- alias_method :each_member, :each_name
15
- alias_method :each_key, :each_name
16
-
17
- # @yield [index]
18
- # @yieldparam [Integer] Index
19
- # @yieldreturn [self]
20
- # @return [Enumerator]
21
- def each_index
22
- return to_enum(__method__) unless block_given?
23
- _names.each_index{|index|yield index}
24
- self
25
- end
26
-
27
- # @yield [name, index]
28
- # @yieldparam [Symbol] name
29
- # @yieldparam [Integer] index
30
- # @yieldreturn [self]
31
- # @return [Enumerator]
32
- def each_name_with_index
33
- return to_enum(__method__) unless block_given?
34
- _names.each_with_index{|name, index|yield name, index}
35
- self
36
- end
37
-
38
- alias_method :each_member_with_index, :each_name_with_index
39
- alias_method :each_key_with_index, :each_name_with_index
40
-
41
- # @param [Symbol, String] name
42
- def original?(name)
43
- raise NameError unless member? name
44
-
45
- @names.include? name
46
- end
47
-
48
- # @param [Symbol, String] name
49
- def aliased?(name)
50
- raise NameError unless member? name
51
-
52
- @aliases.has_key? name
53
- end
54
-
55
- # @param [Symbol, String] original
56
- def has_aliases?(original)
57
- raise NameError unless original? original
58
-
59
- @aliases.has_value? original
60
- end
61
-
62
- # @param [Symbol, String] original
63
- # @return [Array<Symbol>]
64
- def aliases_for(original)
65
- original = keyable_for original
66
-
67
- if has_aliases? original
68
- _aliases_for original
69
- else
70
- raise NameError
71
- end
72
- end
73
-
74
- # @return [Hash] alias => autonym
75
- def aliases
76
- @aliases.dup
77
- end
78
-
79
- # @param [Symbol, String] name
80
- def has_flavor?(name)
81
- name = autonym_for name
82
-
83
- ! flavor_for(name).nil?
84
- end
85
-
86
- # @param [Symbol, String] name
87
- def has_default?(name)
88
- name = autonym_for name
89
-
90
- @defaults.has_key? name
91
- end
92
-
93
- # @param [Symbol, String] name
94
- def default_for(name)
95
- name = autonym_for name
96
-
97
- if has_default? name
98
- _default_for name
99
- else
100
- raise "#{name} has no default value"
101
- end
102
- end
103
-
104
- # @return [Class]
105
- def to_struct_class
106
- raise 'No defined members' if names.empty?
107
-
108
- struct_klass = Struct.new(*names)
109
-
110
- if name
111
- tail_name = name.slice(/[^:]+\z/)
112
- if ::Striuct::Structs.const_defined?(tail_name) &&
113
- ((already = ::Striuct::Structs.const_get(tail_name)).members == members)
114
- already
115
- else
116
- ::Striuct::Structs.const_set tail_name, struct_klass
117
- end
118
- else
119
- struct_klass
120
- end
121
- end
122
-
123
- # @endgroup
124
- end; end
@@ -1,52 +0,0 @@
1
- class Striuct; module ClassMethods
2
- # @group Struct+ Safety
3
-
4
- # @param [Symbol, String] name
5
- # inference checker is waiting yet
6
- def inference?(name)
7
- name = autonym_for name
8
-
9
- @inferences.has_key? name
10
- end
11
-
12
- # @param [Symbol, String] name
13
- def has_condition?(name)
14
- name = autonym_for name
15
-
16
- @conditions.has_key?(name)
17
- end
18
-
19
- alias_method :restrict?, :has_condition?
20
-
21
- # @param [Symbol, String] name
22
- def safety_getter?(name)
23
- name = autonym_for name
24
-
25
- @getter_validations.has_key?(name)
26
- end
27
-
28
- alias_method :safety_reader?, :safety_getter?
29
-
30
- # @param [Symbol, String] name
31
- def safety_setter?(name)
32
- name = autonym_for name
33
-
34
- @setter_validations.has_key?(name)
35
- end
36
-
37
- alias_method :safety_writer?, :safety_setter?
38
-
39
- # @param [Object] name
40
- # accpeptable the name into own member, under protect level of runtime
41
- def cname?(name)
42
- _check_safety_naming(keyable_for name){|r|r}
43
- rescue Exception
44
- false
45
- end
46
-
47
- def closed?
48
- [@names, @flavors, @defaults, @aliases].any?(&:frozen?)
49
- end
50
-
51
- # @endgroup
52
- end; end
@@ -1,135 +0,0 @@
1
- class Striuct; module InstanceMethods
2
- # @group Basic
3
-
4
- def initialize(*values)
5
- @db, @locks = {}, {}
6
- replace_values(*values)
7
- end
8
-
9
- # see self.class.*args
10
- delegate_class_methods(
11
- :members, :keys, :names, :autonyms, :all_members, :aliases,
12
- :has_member?, :member?, :has_key?, :key?,
13
- :length, :size
14
- )
15
-
16
- # @return [Boolean]
17
- def ==(other)
18
- __compare_all__ other, :==
19
- end
20
-
21
- alias_method :===, :==
22
-
23
- def eql?(other)
24
- __compare_all__ other, :eql?
25
- end
26
-
27
- # @return [Integer]
28
- def hash
29
- @db.hash
30
- end
31
-
32
- # @return [String]
33
- def inspect
34
- "#<#{self.class} (Striuct)".tap {|s|
35
- each_pair do |name, value|
36
- suffix = (has_default?(name) && default?(name)) ? '(default)' : nil
37
- s << " #{name}=#{value.inspect}#{suffix}"
38
- end
39
-
40
- s << ">"
41
- }
42
- end
43
-
44
- # @return [String]
45
- def to_s
46
- "#<struct #{self.class}".tap {|s|
47
- each_pair do |name, value|
48
- s << " #{name}=#{value.inspect}"
49
- end
50
-
51
- s << '>'
52
- }
53
- end
54
-
55
- # @param [Symbol, String, Fixnum] key
56
- def [](key)
57
- __subscript__(key){|name|__get__ name}
58
- end
59
-
60
- # @param [Symbol, String, Fixnum] key
61
- # @param [Object] value
62
- # @return [value]
63
- def []=(key, value)
64
- true_name = nil
65
- __subscript__(key){|name|true_name = name; __set__ name, value}
66
- rescue Validation::InvalidWritingError
67
- $!.set_backtrace([
68
- "#{$!.backtrace[-1].sub(/[^:]+\z/){''}}in `[#{key.inspect}(#{true_name})]=': #{$!.message}",
69
- $!.backtrace[-1]
70
- ])
71
-
72
- raise
73
- end
74
-
75
- alias_method :assign, :[]
76
-
77
- # @yield [value]
78
- # @yieldparam [Object] value - sequential under defined
79
- # @see #each_name
80
- # @yieldreturn [self]
81
- # @return [Enumerator]
82
- def each_value
83
- return to_enum(__method__) unless block_given?
84
- each_member{|member|yield self[member]}
85
- end
86
-
87
- alias_method :each, :each_value
88
-
89
- # @yield [name, value]
90
- # @yieldparam [Symbol] name
91
- # @yieldparam [Object] value
92
- # @yieldreturn [self]
93
- # @return [Enumerator]
94
- # @see #each_name
95
- # @see #each_value
96
- def each_pair
97
- return to_enum(__method__) unless block_given?
98
- each_name{|name|yield name, self[name]}
99
- end
100
-
101
- # @return [Array]
102
- def values
103
- each_value.to_a
104
- end
105
-
106
- alias_method :to_a, :values
107
-
108
- # @param [Fixnum, Range] *keys
109
- # @return [Array]
110
- def values_at(*_keys)
111
- [].tap {|r|
112
- _keys.each do |key|
113
- case key
114
- when Fixnum
115
- r << self[key]
116
- when Range
117
- key.each do |n|
118
- raise TypeError unless n.instance_of? Fixnum
119
- r << self[n]
120
- end
121
- else
122
- raise TypeError
123
- end
124
- end
125
- }
126
- end
127
-
128
- # @return [self]
129
- def freeze
130
- [@db, @locks].each(&:freeze)
131
- super
132
- end
133
-
134
- # @endgroup
135
- end; end
@@ -1,91 +0,0 @@
1
- class Striuct; module InstanceMethods
2
- # @group Struct + Handy
3
-
4
- # see self.class.*args
5
- delegate_class_methods :has_default?, :default_for, :has_flavor?
6
-
7
- # @yield [index]
8
- # @yieldparam [Integer] index
9
- # @yieldreturn [self]
10
- # @return [Enumerator]
11
- def each_index
12
- return to_enum(__method__) unless block_given?
13
- self.class.each_index{|index|yield index}
14
- self
15
- end
16
-
17
- # @yield [name, index]
18
- # @yieldparam [Symbol] name
19
- # @yieldparam [Integer] index
20
- # @yieldreturn [self]
21
- # @return [Enumerator]
22
- def each_name_with_index
23
- return to_enum(__method__) unless block_given?
24
- self.class.each_name_with_index{|name, index|yield name, index}
25
- self
26
- end
27
-
28
- alias_method :each_member_with_index, :each_name_with_index
29
- alias_method :each_key_with_index, :each_name_with_index
30
-
31
- # @yield [value, index]
32
- # @yieldparam [Integer] index
33
- # @yieldreturn [self]
34
- # @return [Enumerator]
35
- def each_value_with_index
36
- return to_enum(__method__) unless block_given?
37
- each_value.with_index{|value, index|yield value, index}
38
- self
39
- end
40
-
41
- alias_method :each_with_index, :each_value_with_index
42
-
43
- # @yield [name, value, index]
44
- # @yieldparam [Symbol] name
45
- # @yieldparam [Integer] index
46
- # @yieldreturn [self]
47
- # @return [Enumerator]
48
- def each_pair_with_index
49
- return to_enum(__method__) unless block_given?
50
- index = 0
51
- each_pair do |name, value|
52
- yield name, value, index
53
- index += 1
54
- end
55
- self
56
- end
57
-
58
- # @param [Symbol, String] name
59
- def assign?(name)
60
- name = autonym_for name
61
-
62
- @db.has_key? name
63
- end
64
-
65
- # @param [Symbol, String, Fixnum] key
66
- def clear_at(key)
67
- __subscript__(key){|name|__clear__ name}
68
- end
69
-
70
- alias_method :unassign, :clear_at
71
- alias_method :reset_at, :clear_at
72
-
73
- # @param [Symbol, String] name
74
- def default?(name)
75
- name = autonym_for name
76
-
77
- default_for(name) == self[name]
78
- end
79
-
80
- # all members aren't assigned
81
- def empty?
82
- each_name.none?{|name|assign? name}
83
- end
84
-
85
- # @return [Struct]
86
- def to_struct
87
- self.class.to_struct_class.new(*values)
88
- end
89
-
90
- # @endgroup
91
- end; end