Module: InterProcessAttribute

Included in:
Class
Defined in:
lib/interprocess_attribute.rb

Instance Method Summary collapse

Instance Method Details

#interprocess_attribute(name, opts = {visibility: "public"}) ⇒ Object

Examples:

class Person
  extend InterProcessAttribute
  interprocess_attribute :name, {visibility: :private}

  def initialize
    self.name = "Rob"
  end
end

person = Person.new
person.name = "Rob"
p person.name # => "Rob"

Parameters:

  • name (#to_s)

    The name of the attribute.

  • opts (Hash) (defaults to: {visibility: "public"})

    @option opts [#to_s] :visibility

    "public", "protected", or "private"
    


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/interprocess_attribute.rb', line 25

def interprocess_attribute(name, opts = {visibility: "public"})
  class_eval do
    channel = IChannel.new Marshal
    define_method name do
      while channel.readable?
        instance_variable_set "@#{name}", channel.get
      end
      instance_variable_get "@#{name}"
    end
    send opts[:visibility], name.to_sym

    define_method "#{name}=" do |value|
      channel.put value
      instance_variable_set "@#{name}", value
    end
    send opts[:visibility], "#{name}=".to_sym
  end
end