Module: Concurrent::Synchronization::AbstractStruct
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb |
Overview
**Private Implementation:** This abstraction is a private, internal implementation detail. It should never be used directly.
Class Method Summary
Instance Method Summary
-
#length ⇒ Fixnum
(also: #size)
Returns the number of struct members.
-
#members ⇒ Array
Returns the struct members as an array of symbols.
-
#size
Alias for #length.
- #initialize(*values) private
-
#ns_each {|value| ... }
private
Yields the value of each struct member in order.
-
#ns_each_pair {|member, value| ... }
private
Yields the name and value of each struct member in order.
-
#ns_equality(other) ⇒ Boolean
private
Equality.
-
#ns_get(member) ⇒ Object
private
Attribute Reference.
- #ns_initialize_copy private
-
#ns_inspect ⇒ String
private
Describe the contents of this struct in a string.
-
#ns_merge(other) {|member, selfvalue, othervalue| ... } ⇒ Synchronization::AbstractStruct
private
Returns a new struct containing the contents of
other
and the contents ofself
. -
#ns_select {|value| ... } ⇒ Array
private
Yields each member value from the struct to the block and returns an
Array
containing the member values from the struct for which the given block returns a true value (equivalent toEnumerable#select
). -
#ns_to_h ⇒ Hash
private
Returns a hash containing the names and values for the struct’s members.
-
#ns_values ⇒ Array
private
Returns the values for this struct as an
Array
. -
#ns_values_at(indexes)
private
Returns the struct member values for each selector as an
Array
. - #pr_underscore(clazz) private
Class Method Details
.define_struct_class(parent, base, name, members, &block) (private)
[ GitHub ]# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 141
def self.define_struct_class(parent, base, name, members, &block) clazz = Class.new(base || Object) do include parent self.const_set(:MEMBERS, members.collect{|member| member.to_s.to_sym}.freeze) def ns_initialize(*values) raise ArgumentError.new('struct size differs') if values.length > length @values = values.fill(nil, values.length..length-1) end end unless name.nil? begin parent.send :remove_const, name if parent.const_defined?(name, false) parent.const_set(name, clazz) clazz rescue NameError raise NameError.new("identifier #{name} needs to be constant") end end members.each_with_index do |member, index| clazz.send :remove_method, member if clazz.instance_methods(false).include? member clazz.send(:define_method, member) do @values[index] end end clazz.class_exec(&block) unless block.nil? clazz.singleton_class.send :alias_method, :[], :new clazz end
Instance Method Details
#initialize(*values) (private)
[ GitHub ]# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 9
def initialize(*values) super() ns_initialize(*values) end
#length ⇒ Fixnum
Also known as: #size
Returns the number of struct members.
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 19
def length self.class::MEMBERS.length end
#members ⇒ Array
Returns the struct members as an array of symbols.
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 29
def members self.class::MEMBERS.dup end
#ns_each {|value| ... } (private)
Yields the value of each struct member in order. If no block is given an enumerator is returned.
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 82
def ns_each values.each{|value| yield value } end
#ns_each_pair {|member, value| ... } (private)
Yields the name and value of each struct member in order. If no block is given an enumerator is returned.
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 89
def ns_each_pair @values.length.times do |index| yield self.class::MEMBERS[index], @values[index] end end
#ns_equality(other) ⇒ Boolean
(private)
Equality
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 75
def ns_equality(other) self.class == other.class && self.values == other.values end
#ns_get(member) ⇒ Object (private)
Attribute Reference
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 59
def ns_get(member) if member.is_a? Integer if member >= @values.length raise IndexError.new("offset #{member} too large for struct(size:#{@values.length})") end @values[member] else send(member) end rescue NoMethodError raise NameError.new("no member '#{member}' in struct") end
#ns_initialize_copy (private)
[ GitHub ]# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 119
def ns_initialize_copy @values = @values.map do |val| begin val.clone rescue TypeError val end end end
#ns_inspect ⇒ String
(private)
Describe the contents of this struct in a string.
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 105
def ns_inspect struct = pr_underscore(self.class.ancestors[1]) clazz = ((self.class.to_s =~ /^#<Class:/) == 0) ? '' : " #{self.class}" "#<#{struct}#{clazz} #{ns_to_h}>" end
#ns_merge(other) {|member, selfvalue, othervalue| ... } ⇒ Synchronization::AbstractStruct (private)
Returns a new struct containing the contents of other
and the contents of self
. If no block is specified, the value for entries with duplicate keys will be that of other
. Otherwise the value for each duplicate key is determined by calling the block with the key, its value in self
and its value in other
.
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 114
def ns_merge(other, &block) self.class.new(*self.to_h.merge(other, &block).values) end
#ns_select {|value| ... } ⇒ Array (private)
Yields each member value from the struct to the block and returns an Array
containing the member values from the struct for which the given block returns a true value (equivalent to Enumerable#select
).
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 98
def ns_select values.select{|value| yield value } end
#ns_to_h ⇒ Hash (private)
Returns a hash containing the names and values for the struct’s members.
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 52
def ns_to_h length.times.reduce({}){|memo, i| memo[self.class::MEMBERS[i]] = @values[i]; memo} end
#ns_values ⇒ Array (private)
Returns the values for this struct as an Array
.
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 38
def ns_values @values.dup end
#ns_values_at(indexes) (private)
Returns the struct member values for each selector as an Array
.
A selector may be either an Integer offset or a Range of offsets (as in Array#values_at
).
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 45
def ns_values_at(indexes) @values.values_at(*indexes) end
#pr_underscore(clazz) (private)
[ GitHub ]# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 130
def pr_underscore(clazz) word = clazz.to_s.dup # dup string to workaround JRuby 9.2.0.0 bug https://github.com/jruby/jruby/issues/5229 word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end
#size
Alias for #length.
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 22
alias_method :size, :length