Class: Culpa::Envelope

Inherits:
Object
  • Object
show all
Defined in:
lib/culpa/envelope.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Envelope

Returns a new instance of Envelope.



11
12
13
14
15
16
# File 'lib/culpa/envelope.rb', line 11

def initialize(attributes = {})
  @semaphore = Mutex.new
  attributes.each do |name, value|
    instance_variable_set "@#{name}", value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/culpa/envelope.rb', line 18

def method_missing(sym, *args)
  sym = sym.to_s
  @semaphore.synchronize do
    if sym.end_with? '='
      instance_variable_set "@#{sym.delete('=')}", args[0]
    else
      return instance_variable_get "@#{sym.delete('=')}"
    end
  end
end

Instance Method Details

#==(other_envelope) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/culpa/envelope.rb', line 29

def ==(other_envelope)
  return false unless other_envelope.is_a? Envelope
  return false unless other_envelope.instance_variables.count == instance_variables.count
  instance_variables.select{ |iv| iv != :@semaphore }.each do |iv|
    return false unless instance_variable_get(iv) == other_envelope.instance_variable_get(iv)
  end
  true
end

#must_have!(*args) ⇒ Object



5
6
7
8
9
# File 'lib/culpa/envelope.rb', line 5

def must_have!(*args)
  args.each do |arg|
    raise MustHaveFailed, "Envelope must_have! failed on #{arg}" unless instance_variable_defined? "@#{arg}"
  end
end