Module: Striuct::InstanceMethods
- Extended by:
- Forwardable
- Defined in:
- lib/striuct/instancemethods/cast.rb,
lib/striuct/instancemethods/enum.rb,
lib/striuct/instancemethods/lock.rb,
lib/striuct/instancemethods/to_s.rb,
lib/striuct/instancemethods/hashy.rb,
lib/striuct/instancemethods/assign.rb,
lib/striuct/instancemethods/getter.rb,
lib/striuct/instancemethods/object.rb,
lib/striuct/instancemethods/safety.rb,
lib/striuct/instancemethods/setter.rb,
lib/striuct/instancemethods/values.rb,
lib/striuct/instancemethods/compare.rb,
lib/striuct/instancemethods/default.rb,
lib/striuct/instancemethods/validation.rb,
lib/striuct/instancemethods/requirements.rb,
lib/striuct/instancemethods/delegate_class_methods.rb
Cast collapse
- #to_h(include_no_assign: true) ⇒ Hash
- #to_striuct ⇒ self
- #to_struct ⇒ Struct
- #values ⇒ Array (also: #to_a)
Enumerative collapse
- #each_autonym {|autonym| ... } ⇒ Enumerator (also: #each_member)
- #each_autonym_with_index {|autonym, index| ... } ⇒ Enumerator (also: #each_member_with_index)
- #each_index {|index| ... } ⇒ Enumerator
- #each_pair {|autonym, value| ... } ⇒ Enumerator
- #each_pair_with_index {|autonym, value, index| ... } ⇒ Enumerator
- #each_value {|value| ... } ⇒ Enumerator (also: #each)
- #each_value_with_index {|value, index| ... } ⇒ Enumerator (also: #each_with_index)
Lock / Unlock collapse
To Strings collapse
Like Ruby's Hash collapse
-
#assoc(name) ⇒ Array
E.g [autonym, value].
- #delete_if {|autonym, value| ... } ⇒ Enumerator
- #flatten(level = 1) ⇒ Array
- #has_value?(value) ⇒ Boolean (also: #value?)
- #keep_if {|autonym, value| ... } ⇒ Enumerator
-
#rassoc(value) ⇒ Array
[autonym, value].
- #reject {|autonym, value| ... } ⇒ Striuct
-
#reject! {|autonym, value| ... } ⇒ Enumerator
keep falsy only (unassign truthy member).
- #select {|autonym, value| ... } ⇒ Striuct
-
#select! {|autonym, value| ... } ⇒ Enumerator
keep truthy only (unassign falsy member).
Assign / Unassign collapse
- #assigned?(key) ⇒ Boolean (also: #assign?)
-
#empty? ⇒ Boolean
true if all members are not yet assigned.
-
#unassign(key) ⇒ Object
(also: #delete_at)
Value / nil - value assigned under the key.
Getter collapse
- #fetch_by_autonym(autonym) ⇒ Object
- #fetch_by_index(index) ⇒ Object
- #fetch_by_key(key) ⇒ Object (also: #[], #fetch)
- #fetch_by_member(member) ⇒ Object
Basic Methods for Ruby's Object collapse
Safety collapse
-
#secure? ⇒ Boolean
freezed, fixed familiar members, all members passed any condition.
Setter collapse
-
#[]=(key, value) ⇒ Object
(also: #assign)
Value.
Behavior under Values collapse
Compare with other collapse
- #==(other) ⇒ Boolean (also: #===)
- #eql?(other) ⇒ Boolean
- #hash ⇒ Integer
Default Value collapse
Validation collapse
- #_valid?(pattern, value) ⇒ Boolean
-
#accept?(key, value) ⇒ Boolean
true if passed under any condition.
-
#strict? ⇒ Boolean
all members passed under any condition.
-
#sufficient?(key, value = self[key]) ⇒ Boolean
(also: #valid?)
true if passed under any condition.
Instance Method Details
#==(other) ⇒ Boolean Also known as: ===
8 9 10 11 |
# File 'lib/striuct/instancemethods/compare.rb', line 8 def ==(other) other.instance_of?(self.class) && each_pair.all? { |autonym, val| other._get(autonym) == val } end |
#[]=(key, value) ⇒ Object Also known as: assign
Returns value.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/striuct/instancemethods/setter.rb', line 9 def []=(key, value) autonym = autonym_for_key(key) _set(autonym, value) rescue InvalidWritingError $!.set_backtrace( ["#{$!.backtrace[-1].sub(/[^:]+\z/) { '' }}in `[#{key.inspect}(#{autonym})]=': #{$!.}", $!.backtrace[-1]] ) raise end |
#_valid?(pattern, value) ⇒ Boolean
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/striuct/instancemethods/validation.rb', line 9 def _valid?(pattern, value) !!( case pattern when Proc instance_exec(value, &pattern) when Method pattern.call(value) else pattern === value end ) end |
#accept?(key, value) ⇒ Boolean
true if passed under any condition
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/striuct/instancemethods/validation.rb', line 25 def accept?(key, value) autonym = autonym_for_key(key) return true unless restrict?(autonym) begin _valid?(condition_for(autonym), value) rescue Exception false end end |
#all_locked? ⇒ Boolean
30 31 32 |
# File 'lib/striuct/instancemethods/lock.rb', line 30 def all_locked? _autonyms.all? { |autonym| @locks.key?(autonym) } end |
#assigned?(key) ⇒ Boolean Also known as: assign?
8 9 10 |
# File 'lib/striuct/instancemethods/assign.rb', line 8 def assigned?(key) @db.key?(autonym_for_key(key)) end |
#assoc(name) ⇒ Array
Returns e.g [autonym, value].
81 82 83 84 85 |
# File 'lib/striuct/instancemethods/hashy.rb', line 81 def assoc(name) autonym = autonym_for_member(name) [autonym, self[name]] end |
#default?(key) ⇒ Boolean
8 9 10 11 12 |
# File 'lib/striuct/instancemethods/default.rb', line 8 def default?(key) autonym = autonym_for_key(key) default_value_for(autonym) == fetch_by_autonym(autonym) end |
#delete_if {|autonym, value| ... } ⇒ Enumerator
71 72 73 74 75 76 77 |
# File 'lib/striuct/instancemethods/hashy.rb', line 71 def delete_if(&block) _check_frozen return to_enum(__callee__) unless block reject!(&block) self end |
#each_autonym {|autonym| ... } ⇒ Enumerator Also known as: each_member
11 12 13 14 15 16 |
# File 'lib/striuct/instancemethods/enum.rb', line 11 def each_autonym(&block) return to_enum(__callee__) { self.class.size } unless block self.class.each_autonym(&block) self end |
#each_autonym_with_index {|autonym, index| ... } ⇒ Enumerator Also known as: each_member_with_index
62 63 64 65 66 67 |
# File 'lib/striuct/instancemethods/enum.rb', line 62 def each_autonym_with_index return to_enum(__callee__) { self.class.size } unless block_given? self.class.each_autonym_with_index { |autonym, index| yield autonym, index } self end |
#each_index {|index| ... } ⇒ Enumerator
50 51 52 53 54 55 |
# File 'lib/striuct/instancemethods/enum.rb', line 50 def each_index return to_enum(__callee__) { self.class.size } unless block_given? self.class.each_index { |index| yield index } self end |
#each_pair {|autonym, value| ... } ⇒ Enumerator
40 41 42 43 44 |
# File 'lib/striuct/instancemethods/enum.rb', line 40 def each_pair return to_enum(__callee__) { self.class.size } unless block_given? each_autonym { |autonym| yield autonym, _get(autonym) } end |
#each_pair_with_index {|autonym, value, index| ... } ⇒ Enumerator
88 89 90 91 92 93 94 95 96 |
# File 'lib/striuct/instancemethods/enum.rb', line 88 def each_pair_with_index return to_enum(__callee__) { self.class.size } unless block_given? index = 0 each_pair do |autonym, value| yield autonym, value, index index += 1 end end |
#each_value {|value| ... } ⇒ Enumerator Also known as: each
25 26 27 28 29 |
# File 'lib/striuct/instancemethods/enum.rb', line 25 def each_value return to_enum(__callee__) { self.class.size } unless block_given? each_autonym { |autonym| yield _get(autonym) } end |
#each_value_with_index {|value, index| ... } ⇒ Enumerator Also known as: each_with_index
75 76 77 78 79 |
# File 'lib/striuct/instancemethods/enum.rb', line 75 def each_value_with_index return to_enum(__callee__) { self.class.size } unless block_given? each_value.with_index { |value, index| yield value, index } end |
#empty? ⇒ Boolean
true if all members are not yet assigned
29 30 31 |
# File 'lib/striuct/instancemethods/assign.rb', line 29 def empty? _autonyms.none? { |autonym| @db.key?(autonym) } end |
#eql?(other) ⇒ Boolean
15 16 17 |
# File 'lib/striuct/instancemethods/compare.rb', line 15 def eql?(other) other.instance_of?(self.class) && other._db.eql?(@db) end |
#fetch_by_autonym(autonym) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/striuct/instancemethods/getter.rb', line 8 def fetch_by_autonym(autonym) autonym = autonym.to_sym raise NameError unless autonym?(autonym) _get(autonym) end |
#fetch_by_index(index) ⇒ Object
21 22 23 |
# File 'lib/striuct/instancemethods/getter.rb', line 21 def fetch_by_index(index) _get(autonym_for_index(index)) end |
#fetch_by_key(key) ⇒ Object Also known as: [], fetch
26 27 28 |
# File 'lib/striuct/instancemethods/getter.rb', line 26 def fetch_by_key(key) _get(autonym_for_key(key)) end |
#fetch_by_member(member) ⇒ Object
16 17 18 |
# File 'lib/striuct/instancemethods/getter.rb', line 16 def fetch_by_member(member) _get(autonym_for_member(member)) end |
#fetch_values(*keys, &block) ⇒ Array
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/striuct/instancemethods/values.rb', line 31 def fetch_values(*keys, &block) keys.map { |key| if key?(key) fetch_by_key(key) else if block yield else raise ArgumentError, "`#{key}' is not matched" end end } end |
#flatten(level = 1) ⇒ Array
94 95 96 |
# File 'lib/striuct/instancemethods/hashy.rb', line 94 def flatten(level=1) each_pair.to_a.flatten(level) end |
#freeze ⇒ self
8 9 10 11 |
# File 'lib/striuct/instancemethods/object.rb', line 8 def freeze @db.freeze; @locks.freeze super end |
#has_value?(value) ⇒ Boolean Also known as: value?
7 8 9 |
# File 'lib/striuct/instancemethods/hashy.rb', line 7 def has_value?(value) @db.value?(value) end |
#hash ⇒ Integer
20 21 22 |
# File 'lib/striuct/instancemethods/compare.rb', line 20 def hash @db.hash end |
#inspect ⇒ String
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/striuct/instancemethods/to_s.rb', line 8 def inspect (+"#<struct' #{self.class}").tap { |s| each_pair do |autonym, value| suffix = (with_default?(autonym) && default?(autonym)) ? '/default' : nil label_valid = valid?(autonym) ? nil : :invalid label_lock = locked?(autonym) ? :locked : nil label_must = must?(autonym) ? :must : nil labels = [label_valid, label_lock, label_must].select { |elm| elm } s << " #{autonym}=#{value.inspect}#{suffix}" unless labels.empty? s << '(' s << labels.join(', ') s << ')' end s << ',' end s.chop! s << '>' } end |
#keep_if {|autonym, value| ... } ⇒ Enumerator
39 40 41 42 43 44 45 |
# File 'lib/striuct/instancemethods/hashy.rb', line 39 def keep_if(&block) _check_frozen return to_enum(__callee__) unless block select!(&block) self end |
#lock(key) ⇒ self
9 10 11 12 13 14 |
# File 'lib/striuct/instancemethods/lock.rb', line 9 def lock(key) _check_frozen @locks[autonym_for_key(key)] = true self end |
#lock_all ⇒ self
17 18 19 20 21 22 23 |
# File 'lib/striuct/instancemethods/lock.rb', line 17 def lock_all _check_frozen each_autonym do |autonym| @locks[autonym] = true end end |
#locked?(key) ⇒ Boolean
26 27 28 |
# File 'lib/striuct/instancemethods/lock.rb', line 26 def locked?(key) @locks.key?(autonym_for_key(key)) end |
#rassoc(value) ⇒ Array
Returns [autonym, value].
88 89 90 |
# File 'lib/striuct/instancemethods/hashy.rb', line 88 def rassoc(value) each_pair.find { |_, val| val == value } end |
#reject {|autonym, value| ... } ⇒ Striuct
112 113 114 115 116 |
# File 'lib/striuct/instancemethods/hashy.rb', line 112 def reject(&block) return to_enum(__callee__) unless block dup.tap { |r| r.reject!(&block) } end |
#reject! {|autonym, value| ... } ⇒ Enumerator
keep falsy only (unassign truthy member)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/striuct/instancemethods/hashy.rb', line 52 def reject! _check_frozen return to_enum(__callee__) unless block_given? modified = false each_pair do |autonym, value| if yield autonym, value unassign(autonym) modified = true end end modified ? self : nil end |
#replace_values(*values) ⇒ self
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/striuct/instancemethods/values.rb', line 46 def replace_values(*values) unless values.size <= size raise ArgumentError, "struct size differs (max: #{size})" end values.each_with_index do |value, index| _set(autonym_for_index(index), value) end self end |
#secure? ⇒ Boolean
freezed, fixed familiar members, all members passed any condition
8 9 10 |
# File 'lib/striuct/instancemethods/safety.rb', line 8 def secure? (frozen? || all_locked?) && self.class.closed? && strict? end |
#select {|autonym, value| ... } ⇒ Striuct
102 103 104 105 106 |
# File 'lib/striuct/instancemethods/hashy.rb', line 102 def select(&block) return to_enum(__callee__) unless block dup.tap { |r| r.select!(&block) } end |
#select! {|autonym, value| ... } ⇒ Enumerator
keep truthy only (unassign falsy member)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/striuct/instancemethods/hashy.rb', line 20 def select! _check_frozen return to_enum(__callee__) unless block_given? modified = false each_pair do |autonym, value| unless yield autonym, value unassign(autonym) modified = true end end modified ? self : nil end |
#strict? ⇒ Boolean
all members passed under any condition
46 47 48 |
# File 'lib/striuct/instancemethods/validation.rb', line 46 def strict? each_pair.all? { |autonym, value| sufficient?(autonym, value) } end |
#sufficient?(key, value = self[key]) ⇒ Boolean Also known as: valid?
true if passed under any condition
39 40 41 |
# File 'lib/striuct/instancemethods/validation.rb', line 39 def sufficient?(key, value=self[key]) accept?(key, value) end |
#to_h(include_no_assign: true) ⇒ Hash
19 20 21 22 23 |
# File 'lib/striuct/instancemethods/cast.rb', line 19 def to_h(include_no_assign: true) return @db.dup unless include_no_assign each_pair.to_a.to_h end |
#to_s ⇒ String
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/striuct/instancemethods/to_s.rb', line 32 def to_s (+"#<struct' #{self.class}").tap { |s| each_pair do |autonym, value| s << " #{autonym}=#{value.inspect}," end s.chop! s << '>' } end |
#to_striuct ⇒ self
8 9 10 |
# File 'lib/striuct/instancemethods/cast.rb', line 8 def to_striuct self end |
#to_struct ⇒ Struct
13 14 15 |
# File 'lib/striuct/instancemethods/cast.rb', line 13 def to_struct self.class.to_struct_class.new(*values) end |
#unassign(key) ⇒ Object Also known as: delete_at
Returns value / nil - value assigned under the key.
16 17 18 19 20 21 22 23 24 |
# File 'lib/striuct/instancemethods/assign.rb', line 16 def unassign(key) _check_frozen _check_locked(key) if must?(key) raise InvalidOperationError, "`#{key}` require a value under `must` option" end @db.delete(autonym_for_key(key)) end |
#values ⇒ Array Also known as: to_a
26 27 28 |
# File 'lib/striuct/instancemethods/cast.rb', line 26 def values each_value.to_a end |
#values_at(*keys) ⇒ Array
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/striuct/instancemethods/values.rb', line 9 def values_at(*keys) [].tap { |r| keys.each do |key| case key when ->v { v.respond_to?(:to_int) } r << fetch_by_index(key) when Range key.each do |idx| raise TypeError unless idx.respond_to?(:to_int) r << fetch_by_index(idx) end else raise TypeError end end } end |