Class: Verifly::DependentCallbacks::Service
- Inherits:
-
Object
- Object
- Verifly::DependentCallbacks::Service
- 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
-
#groups ⇒ Symbol => CallbackGroup
groups index.
-
#parents ⇒ [Service]
parents in service inheritance system.
Instance Method Summary collapse
-
#add_callback(position, group, *args, &block) ⇒ Object
Adds callback into matching group.
-
#compiled_group(group_name) ⇒ CallbackGroup
Compiles callback group from itself and parents callback groups.
-
#digest ⇒ Numeric
Digest change forces recompilation of compiled_group.
-
#group_names ⇒ [Symbol]
Names of all groups stored inside itself or parents.
-
#initialize(*parents) ⇒ Service
constructor
A new instance of Service.
- #invoke(group_name) {|invoker| ... } ⇒ Object
-
#merge!(other) ⇒ Object
Merges another service into this.
-
#to_dot(group, binding_) ⇒ Object
Exprorts selected group to graphiz .dot format.
Constructor Details
#initialize(*parents) ⇒ Service
Returns a new instance of Service.
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
#groups ⇒ Symbol => CallbackGroup
groups index
8 9 10 |
# File 'lib/verifly/dependent_callbacks/service.rb', line 8 def groups @groups end |
#parents ⇒ [Service]
parents in service inheritance system
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
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
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 |
#digest ⇒ Numeric
Digest change forces recompilation of compiled_group
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.
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
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
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 |