Class: MagicUserstamp::Event

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, actor, default_attribute_compatible, options = nil) ⇒ Event

Returns a new instance of Event.



10
11
12
13
14
15
16
17
18
19
# File 'lib/magic_userstamp/event.rb', line 10

def initialize(name, actor, default_attribute_compatible, options = nil)
  @name, @actor = name.to_s, actor.to_s
  @default_attribute = "#{@actor}_id"
  @default_attribute_compatible = default_attribute_compatible
  options = {
    :actual_hook => "before_#{@name.to_s}"
  }.update(options || {})
  @actual_hook = options[:actual_hook]
  @after_callback = options[:after_callback]
end

Instance Attribute Details

#actorObject (readonly)

Returns the value of attribute actor.



7
8
9
# File 'lib/magic_userstamp/event.rb', line 7

def actor
  @actor
end

#actual_hookObject (readonly)

Returns the value of attribute actual_hook.



7
8
9
# File 'lib/magic_userstamp/event.rb', line 7

def actual_hook
  @actual_hook
end

#after_callbackObject (readonly)

Returns the value of attribute after_callback.



7
8
9
# File 'lib/magic_userstamp/event.rb', line 7

def after_callback
  @after_callback
end

#default_attributeObject (readonly)

Returns the value of attribute default_attribute.



8
9
10
# File 'lib/magic_userstamp/event.rb', line 8

def default_attribute
  @default_attribute
end

#default_attribute_compatibleObject (readonly)

Returns the value of attribute default_attribute_compatible.



8
9
10
# File 'lib/magic_userstamp/event.rb', line 8

def default_attribute_compatible
  @default_attribute_compatible
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/magic_userstamp/event.rb', line 7

def name
  @name
end

Class Method Details

.[](event_name) ⇒ Object



31
32
33
34
# File 'lib/magic_userstamp/event.rb', line 31

def [](event_name)
  raise_unless_valid_name(event_name)
  @name_hash[event_name]
end

.actor_name(event_name) ⇒ Object



41
42
43
# File 'lib/magic_userstamp/event.rb', line 41

def actor_name(event_name)
  self[event_name].actor
end

.create(name, actor, default_attribute_compatible, options = nil, &block) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/magic_userstamp/event.rb', line 22

def create(name, actor, default_attribute_compatible, options = nil, &block)
  result = self.new(name, actor, default_attribute_compatible, options, &block)
  @name_hash ||= HashWithIndifferentAccess.new
  @name_hash[name] = result
  @instances ||= []
  @instances << result
  result
end

.each(&block) ⇒ Object



36
37
38
39
# File 'lib/magic_userstamp/event.rb', line 36

def each(&block)
  return unless block
  (@instances || []).each(&block)
end

.raise_unless_valid_name(event_name) ⇒ Object



53
54
55
56
# File 'lib/magic_userstamp/event.rb', line 53

def raise_unless_valid_name(event_name)
  return if valid_name?(event_name)
  raise MagicUserstampError, "Invalid event name '#{event_name.inspect}'. Event name must be one of #{valid_names.inspect}"
end

.valid_name?(event_name) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/magic_userstamp/event.rb', line 49

def valid_name?(event_name)
  valid_names.include?(event_name.to_s)
end

.valid_namesObject



45
46
47
# File 'lib/magic_userstamp/event.rb', line 45

def valid_names
  (@instances || []).map(&:name)
end