Class: Verifly::DependentCallbacks::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/verifly/dependent_callbacks/service.rb

Overview

A service to store all callbacks info and delegate methods fom DSLs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*parents) ⇒ Service

Returns a new instance of Service.

Parameters:

  • parents ([Service])

    is filled in by the parent



12
13
14
15
# File 'lib/verifly/dependent_callbacks/service.rb', line 12

def initialize(*parents)
  self.parents = parents
  self.groups = Hash.new { |h, k| h[k] = CallbackGroup.new(k) }
end

Instance Attribute Details

#groupsSymbol => CallbackGroup

groups index

Returns:



8
9
10
# File 'lib/verifly/dependent_callbacks/service.rb', line 8

def groups
  @groups
end

#parents[Service]

parents in service inheritance system

Returns:

  • ([Service])

    the current value of parents



8
9
10
# File 'lib/verifly/dependent_callbacks/service.rb', line 8

def parents
  @parents
end

Instance Method Details

#add_callback(position, group, *args, &block) ⇒ Object

Adds callback into matching group

Parameters:

  • position (:before, :after, :around)
  • group (Symbol)

    group name

  • args

    callback args

See Also:



28
29
30
# File 'lib/verifly/dependent_callbacks/service.rb', line 28

def add_callback(position, group, *args, &block)
  groups[group].add_callback(Callback.new(position, *args, &block))
end

#compiled_group(group_name) ⇒ CallbackGroup

Compiles callback group from itself and parents callback groups. If nothing changed, cached value taken

Parameters:

  • group_name (Symbol)

    group name

Returns:

  • (CallbackGroup)

    callback group joined from all relative callback groups



46
47
48
49
50
51
52
53
54
# File 'lib/verifly/dependent_callbacks/service.rb', line 46

def compiled_group(group_name)
  @compiled_groups_cache ||= Hash.new { |h, k| h[k] = {} }
  cache_entry = @compiled_groups_cache[group_name]
  return cache_entry[:group] if cache_entry[:digest] == digest

  cache_entry[:digest] = digest
  cache_entry[:group] = parents.map { |parent| parent.compiled_group(group_name) }
                               .reduce(groups[group_name], &:merge)
end

#digestNumeric

Digest change forces recompilation of compiled_group

Returns:

  • (Numeric)


58
59
60
61
62
63
# File 'lib/verifly/dependent_callbacks/service.rb', line 58

def digest
  [
    *parents.map(&:digest),
    *groups.map { |k, v| [k, v.digest].join },
  ].hash
end

#group_names[Symbol]

Returns names of all groups stored inside itself or parents.

Returns:

  • ([Symbol])

    names of all groups stored inside itself or parents



38
39
40
# File 'lib/verifly/dependent_callbacks/service.rb', line 38

def group_names
  [groups.keys, *parents.map(&:group_names)].flatten.uniq
end

#invoke(group_name) {|invoker| ... } ⇒ Object

Yields:

  • (invoker)


32
33
34
35
# File 'lib/verifly/dependent_callbacks/service.rb', line 32

def invoke(group_name)
  invoker = Invoker.new(compiled_group(group_name))
  yield(invoker)
end

#merge!(other) ⇒ Object

Merges another service into this

Parameters:



19
20
21
# File 'lib/verifly/dependent_callbacks/service.rb', line 19

def merge!(other)
  parents << other
end

#to_dot(group, binding_) ⇒ Object

Exprorts selected group to graphiz .dot format



66
67
68
# File 'lib/verifly/dependent_callbacks/service.rb', line 66

def to_dot(group, binding_)
  compiled_group(group).to_dot(binding_)
end