module Concurrent::SettableStruct

An thread-safe, write-once variation of Ruby's standard `Struct`. Each member can have its value set at most once, either at construction or any time thereafter. Attempting to assign a value to a member that has already been set will result in a `Concurrent::ImmutabilityError`.

@see ruby-doc.org/core/Struct.html Ruby standard library `Struct` @see en.wikipedia.org/wiki/Final_(Java) Java `final` keyword

Constants

FACTORY

Private Class Methods

new(*args, &block) click to toggle source

@!macro struct_new

# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 105
def self.new(*args, &block)
  clazz_name = nil
  if args.length == 0
    raise ArgumentError.new('wrong number of arguments (0 for 1+)')
  elsif args.length > 0 && args.first.is_a?(String)
    clazz_name = args.shift
  end
  FACTORY.define_struct(clazz_name, args, &block)
end

Public Instance Methods

==(other) click to toggle source

@!macro struct_equality

# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 50
def ==(other)
  synchronize { ns_equality(other) }
end
[](member) click to toggle source

@!macro struct_get

# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 45
def [](member)
  synchronize { ns_get(member) }
end
[]=(member, value) click to toggle source

@!macro struct_set

@raise [Concurrent::ImmutabilityError] if the given member has already been set

# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 75
def []=(member, value)
  if member.is_a? Integer
    length = synchronize { @values.length }
    if member >= length
      raise IndexError.new("offset #{member} too large for struct(size:#{length})")
    end
    synchronize do
      unless @values[member].nil?
        raise Concurrent::ImmutabilityError.new('struct member has already been set')
      end
      @values[member] = value
    end
  else
    send("#{member}=", value)
  end
rescue NoMethodError
  raise NameError.new("no member '#{member}' in struct")
end
each(&block) click to toggle source

@!macro struct_each

# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 55
def each(&block)
  return enum_for(:each) unless block_given?
  synchronize { ns_each(&block) }
end
each_pair(&block) click to toggle source

@!macro struct_each_pair

# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 61
def each_pair(&block)
  return enum_for(:each_pair) unless block_given?
  synchronize { ns_each_pair(&block) }
end
inspect() click to toggle source

@!macro struct_inspect

# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 29
def inspect
  synchronize { ns_inspect }
end
Also aliased as: to_s
merge(other, &block) click to toggle source

@!macro struct_merge

# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 35
def merge(other, &block)
  synchronize { ns_merge(other, &block) }
end
select(&block) click to toggle source

@!macro struct_select

# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 67
def select(&block)
  return enum_for(:select) unless block_given?
  synchronize { ns_select(&block) }
end
to_a()
Alias for: values
to_h() click to toggle source

@!macro struct_to_h

# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 40
def to_h
  synchronize { ns_to_h }
end
to_s()
Alias for: inspect
values() click to toggle source

@!macro struct_values

# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 18
def values
  synchronize { ns_values }
end
Also aliased as: to_a
values_at(*indexes) click to toggle source

@!macro struct_values_at

# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 24
def values_at(*indexes)
  synchronize { ns_values_at(indexes) }
end

Private Instance Methods

initialize_copy(original) click to toggle source

@!visibility private

Calls superclass method
# File lib/concurrent-ruby/concurrent/settable_struct.rb, line 97
def initialize_copy(original)
  synchronize do
    super(original)
    ns_initialize_copy
  end
end