Class: SchrodingersCat

Inherits:
Object show all
Defined in:
lib/schrodinger/schrodingers_cat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original, attrs = {}) ⇒ SchrodingersCat

Returns a new instance of SchrodingersCat.



29
30
31
32
# File 'lib/schrodinger/schrodingers_cat.rb', line 29

def initialize(original, attrs={})
  self.original, self.method_chain  = original, Array.wrap(attrs[:method_chain])
  self.else, @_else_set             = attrs[:else], true if attrs.key?(:else)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object (private)



50
51
52
53
54
# File 'lib/schrodinger/schrodingers_cat.rb', line 50

def method_missing(method_sym, *arguments, &block)
  return self.else if @_else_set
  self.method_chain << { method_sym => arguments.push(&block) }
  self
end

Instance Attribute Details

#elseObject

Note: ##

These can’t be overriden as they are not methods but low-level operators… ):

define_method(:"&&") {|obj| false }; define_method(:"||") {|obj| obj }

which means I can’t reliably do something like record.assuming_a(User).authentications.first || raise “You didn’t supply a user with authentications!” because if sc.is_a?(SchrodingersCat), we ideally want: sc || false # => false true && sc # => false but instead, as || and && are low-level operators for short-circuiting, we get: sc || false # => sc true && sc # => sc However, though normally nil is the only object which should respond to #nil? with a true, so does SchrodingersCat whose MODEL_OBJECT is nil.



24
25
26
# File 'lib/schrodinger/schrodingers_cat.rb', line 24

def else
  @else
end

#method_chainObject

Note: ##

These can’t be overriden as they are not methods but low-level operators… ):

define_method(:"&&") {|obj| false }; define_method(:"||") {|obj| obj }

which means I can’t reliably do something like record.assuming_a(User).authentications.first || raise “You didn’t supply a user with authentications!” because if sc.is_a?(SchrodingersCat), we ideally want: sc || false # => false true && sc # => false but instead, as || and && are low-level operators for short-circuiting, we get: sc || false # => sc true && sc # => sc However, though normally nil is the only object which should respond to #nil? with a true, so does SchrodingersCat whose MODEL_OBJECT is nil.



24
25
26
# File 'lib/schrodinger/schrodingers_cat.rb', line 24

def method_chain
  @method_chain
end

#originalObject

Note: ##

These can’t be overriden as they are not methods but low-level operators… ):

define_method(:"&&") {|obj| false }; define_method(:"||") {|obj| obj }

which means I can’t reliably do something like record.assuming_a(User).authentications.first || raise “You didn’t supply a user with authentications!” because if sc.is_a?(SchrodingersCat), we ideally want: sc || false # => false true && sc # => false but instead, as || and && are low-level operators for short-circuiting, we get: sc || false # => sc true && sc # => sc However, though normally nil is the only object which should respond to #nil? with a true, so does SchrodingersCat whose MODEL_OBJECT is nil.



24
25
26
# File 'lib/schrodinger/schrodingers_cat.rb', line 24

def original
  @original
end

Instance Method Details

#dupObject

delegate *[ “==”, “eql?”, “&”, “^”, “|” ].map(&:intern), :to => :MODEL_OBJECT



48
# File 'lib/schrodinger/schrodingers_cat.rb', line 48

def dup; nil; end