123456789_123456789_123456789_123456789_123456789_

Module: Concurrent::Synchronization::AbstractStruct

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb

Overview

Note:

**Private Implementation:** This abstraction is a private, internal implementation detail. It should never be used directly.

Class Method Summary

Instance Method Summary

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.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

#lengthFixnum Also known as: #size

Returns the number of struct members.

Returns:

  • (Fixnum)

    the number of struct members

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 19

def length
  self.class::MEMBERS.length
end

#membersArray

Returns the struct members as an array of symbols.

Returns:

  • (Array)

    the struct members as an array of symbols

[ GitHub ]

  
# 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.

Yields:

  • the operation to be performed on each struct member

Yield Parameters:

  • value (Object)

    each struct value (in order)

[ GitHub ]

  
# 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.

Yields:

  • the operation to be performed on each struct member/value pair

Yield Parameters:

  • member (Object)

    each struct member (in order)

  • value (Object)

    each struct value (in order)

[ GitHub ]

  
# 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

Returns:

  • (Boolean)

    true if other has the same struct subclass and has equal member values (according to Object#==)

[ GitHub ]

  
# 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

Parameters:

  • member (Symbol, String, Integer)

    the string or symbol name of the member for which to obtain the value or the member’s index

Returns:

  • (Object)

    the value of the given struct member or the member at the given index.

Raises:

  • (NameError)

    if the member does not exist

  • (IndexError)

    if the index is out of range.

[ GitHub ]

  
# 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_inspectString (private)

Describe the contents of this struct in a string.

Returns:

  • (String)

    the contents of this struct in a string

[ GitHub ]

  
# 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.

Parameters:

  • other (Hash)

    the hash from which to set the new values

Yields:

  • an options block for resolving duplicate keys

Yield Parameters:

  • member (String, Symbol)

    the name of the member which is duplicated

  • selfvalue (Object)

    the value of the member in self

  • othervalue (Object)

    the value of the member in other

Returns:

Raises:

  • (ArgumentError)

    of given a member that is not defined in the struct

[ GitHub ]

  
# 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).

Yields:

  • the operation to be performed on each struct member

Yield Parameters:

  • value (Object)

    each struct value (in order)

Returns:

  • (Array)

    an array containing each value for which the block returns true

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 98

def ns_select
  values.select{|value| yield value }
end

#ns_to_hHash (private)

Returns a hash containing the names and values for the struct’s members.

Returns:

  • (Hash)

    the names and values for the struct’s members

[ GitHub ]

  
# 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_valuesArray (private)

Returns the values for this struct as an Array.

Returns:

  • (Array)

    the values for this struct

[ GitHub ]

  
# 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).

Parameters:

  • indexes (Fixnum, Range)

    the index(es) from which to obatin the values (in order)

[ GitHub ]

  
# 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.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/synchronization/abstract_struct.rb', line 22

alias_method :size, :length