Class: Wazowski::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, klass, block) ⇒ Node

Returns a new instance of Node.



41
42
43
44
45
# File 'lib/wazowski.rb', line 41

def initialize(name, klass, block)
  @name = name
  @observer_klass = klass
  instance_eval(&block)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



39
40
41
# File 'lib/wazowski.rb', line 39

def name
  @name
end

Instance Method Details

#after_commit_on_create(klass, object) ⇒ Object

Raises:



109
110
111
112
113
114
115
# File 'lib/wazowski.rb', line 109

def after_commit_on_create(klass, object)
  raise ConfigurationError, "Needs to be called within a #wrapping call!" if @context.nil?
  handler = lookup_handler(klass)

  return unless handler[:opts][:only].nil? || [handler[:opts][:only]].flatten.include?(:insert)
  @context.instance_exec(object, :insert, {}, &handler[:block])
end

#after_commit_on_delete(klass, object) ⇒ Object

Raises:



101
102
103
104
105
106
107
# File 'lib/wazowski.rb', line 101

def after_commit_on_delete(klass, object)
  raise ConfigurationError, "Needs to be called within a #wrapping call!" if @context.nil?
  handler = lookup_handler(klass)

  return unless handler[:opts][:only].nil? || [handler[:opts][:only]].flatten.include?(:delete)
  @context.instance_exec(object, :delete, {}, &handler[:block])
end

#after_commit_on_update(klass, object, dirty_changes) ⇒ Object

Raises:



93
94
95
96
97
98
99
# File 'lib/wazowski.rb', line 93

def after_commit_on_update(klass, object, dirty_changes)
  raise ConfigurationError, "Needs to be called within a #wrapping call!" if @context.nil?
  handler = lookup_handler(klass)

  return unless handler[:opts][:only].nil? || [handler[:opts][:only]].flatten.include?(:update)
  @context.instance_exec(object, :update, dirty_changes, &handler[:block])
end

#dependantsObject



64
65
66
# File 'lib/wazowski.rb', line 64

def dependants
  @depends_on
end

#depends_on(klass, *attrs) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/wazowski.rb', line 47

def depends_on(klass, *attrs)
  if attrs.empty?
    raise ConfigurationError, 'Must depend on some attributes. You can also use `:none` and `:any`'
  end

  @depends_on ||= {}
  @depends_on[klass] ||= []
  @depends_on[klass] += attrs
end

#handler(klass, opts = {}, &block) ⇒ Object

Raises:



57
58
59
60
61
62
# File 'lib/wazowski.rb', line 57

def handler(klass, opts = {}, &block)
  @handlers ||= {}
  raise ConfigurationError, "Already defined handler for #{klass}" if @handlers[klass]

  @handlers[klass] = { block: block, opts: opts }
end

#inspectObject



68
69
70
# File 'lib/wazowski.rb', line 68

def inspect
  "<Wazowski::Node #{@name}>"
end

#lookup_handler(klass) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/wazowski.rb', line 72

def lookup_handler(klass)
  handler = if @handlers[klass]
              @handlers[klass]
            else
              lookup_handler(klass.superclass) unless klass.superclass == Object
            end

  if handler.nil?
    raise(ConfigurationError, "Cannot run handler for klass #{klass}, it has been never "\
                              'registered! Check your definitions.')
  end

  handler
end

#wrappingObject



87
88
89
90
91
# File 'lib/wazowski.rb', line 87

def wrapping
  @context = @observer_klass.new

  yield
end