Module: Esse::Hooks::Mixin::ClassMethods

Defined in:
lib/esse/hooks/mixin.rb

Instance Method Summary collapse

Instance Method Details

#disable!(*repos) ⇒ void

This method returns an undefined value.

Global disable indexing callbacks. If no repository is specified, all repositories will be disabled.

Parameters:

  • repos (Array<String, Esse::Index, Esse::Repo>)


46
47
48
49
50
# File 'lib/esse/hooks/mixin.rb', line 46

def disable!(*repos)
  filter_repositories(*repos).each do |repo|
    state[:repos][repo] = false
  end
end

#disable_model!(model_class, *repos) ⇒ void

This method returns an undefined value.

Disable model indexing callbacks for the given model. If no repository is specified, all repositories will be disabled.

Parameters:

  • model_class (Class)
  • repos (Array<String, Esse::Index, Esse::Repo>)

Raises:

  • (ArgumentError)

    if model repository is not registered for the given model



87
88
89
90
91
92
# File 'lib/esse/hooks/mixin.rb', line 87

def disable_model!(model_class, *repos)
  ensure_registered_model_class!(model_class)
  filter_model_repositories(model_class, *repos).each do |repo|
    state[:models][model_class][repo] = false
  end
end

#disabled?(*repos) ⇒ Boolean

Check if the given repository is enabled for indexing. If no repository is specified, all repositories will be checked.

Parameters:

  • repos (Array<String, Esse::Index, Esse::Repo>)

Returns:

  • (Boolean)


56
57
58
# File 'lib/esse/hooks/mixin.rb', line 56

def disabled?(*repos)
  filter_repositories(*repos).all? { |repo| !state[:repos][repo] }
end

#enable!(*repos) ⇒ void

This method returns an undefined value.

Global enable indexing callbacks. If no repository is specified, all repositories will be enabled.

Parameters:

  • repos (Array<String, Esse::Index, Esse::Repo>)


37
38
39
40
41
# File 'lib/esse/hooks/mixin.rb', line 37

def enable!(*repos)
  filter_repositories(*repos).each do |repo|
    state[:repos][repo] = true
  end
end

#enable_model!(model_class, *repos) ⇒ void

This method returns an undefined value.

Enable model indexing callbacks for the given model. If no repository is specified, all repositories will be enabled.

Parameters:

  • model_class (Class)
  • repos (Array<String, Esse::Index, Esse::Repo>)

Raises:

  • (ArgumentError)

    if model repository is not registered for the given model



74
75
76
77
78
79
# File 'lib/esse/hooks/mixin.rb', line 74

def enable_model!(model_class, *repos)
  ensure_registered_model_class!(model_class)
  filter_model_repositories(model_class, *repos).each do |repo|
    state[:models][model_class][repo] = true
  end
end

#enabled?(*repos) ⇒ Boolean

Check if the given repository is enabled for indexing. If no repository is specified, all repositories will be checked.

Parameters:

  • repos (Array<String, Esse::Index, Esse::Repo>)

Returns:

  • (Boolean)


64
65
66
# File 'lib/esse/hooks/mixin.rb', line 64

def enabled?(*repos)
  filter_repositories(*repos).all? { |repo| state[:repos][repo] }
end

#enabled_for_model?(model_class, *repos) ⇒ Boolean

Check if the given model is enabled for indexing. If no repository is specified, all repositories will be checked.

Parameters:

  • model_class (Class)
  • repos (Array<String, Esse::Index, Esse::Repo>)

Returns:

  • (Boolean)


105
106
107
108
109
110
111
# File 'lib/esse/hooks/mixin.rb', line 105

def enabled_for_model?(model_class, *repos)
  return false unless registered_model_class?(model_class)

  filter_model_repositories(model_class, *repos).all? do |repo|
    state.dig(:models, model_class, repo) != false
  end
end

#ensure_registered_model_class!(model_class) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
# File 'lib/esse/hooks/mixin.rb', line 94

def ensure_registered_model_class!(model_class)
  return if registered_model_class?(model_class)

  raise ArgumentError, "Model class #{model_class} is not registered. The model should inherit from Esse::ActiveRecord::Model and have a `index_callback' callback defined"
end

#model_namesObject



30
31
32
# File 'lib/esse/hooks/mixin.rb', line 30

def model_names
  models.map(&:to_s)
end

#modelsObject



26
27
28
# File 'lib/esse/hooks/mixin.rb', line 26

def models
  @models || []
end

#register_model(model_class) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/esse/hooks/mixin.rb', line 17

def register_model(model_class)
  unless model_class.respond_to?(:esse_callbacks)
    raise ArgumentError, "Model class #{model_class} should have a `esse_callbacks' method"
  end

  @models ||= []
  @models |= [model_class]
end

#resolve_index_repository(name) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/esse/hooks/mixin.rb', line 171

def resolve_index_repository(name)
  index_name, repo_name = Esse::Hooks::Primitives::String.new(name).underscore.split("::").join("/").split(":", 2)
  if index_name !~ /(I|_i)ndex$/ && index_name !~ /_index\/([\w_]+)$/
    index_name = format("%<index_name>s_index", index_name: index_name)
  end
  klass = Esse::Hooks::Primitives::String.new(index_name).classify.constantize
  return klass if klass <= Esse::Repository

  repo_name ? klass.repo(repo_name) : klass.repo
end

#with_indexing(*repos) ⇒ Object

Enable the indexing callbacks execution for the block execution. Example:

Esse::ActiveRecord::Hooks.with_indexing { User.create! }
Esse::ActiveRecord::Hooks.with_indexing(UsersIndex, AccountsIndex.repo(:user)) { User.create! }


130
131
132
133
134
135
136
137
# File 'lib/esse/hooks/mixin.rb', line 130

def with_indexing(*repos)
  state_before_enable = state[:repos].dup
  enable!(*repos)

  yield
ensure
  state[:repos] = state_before_enable
end

#with_indexing_for_model(model_class, *repos) ⇒ Object

Enable model indexing callbacks execution for the block execution for the given model. Example:

BroadcastChanges.with_indexing_for_model(User) { }
BroadcastChanges.with_indexing_for_model(User, :datasync, :other) { }


159
160
161
162
163
164
165
166
167
168
169
# File 'lib/esse/hooks/mixin.rb', line 159

def with_indexing_for_model(model_class, *repos)
  state_before_enable = state[:models].dig(model_class).dup
  enable_model!(model_class, *repos)
  yield
ensure
  if state_before_enable.nil?
    state[:models].delete(model_class)
  else
    state[:models][model_class] = state_before_enable
  end
end

#without_indexing(*repos) ⇒ Object

Disable indexing callbacks execution for the block execution. Example:

Esse::ActiveRecord::Hooks.without_indexing { User.create! }
Esse::ActiveRecord::Hooks.without_indexing(UsersIndex, AccountsIndex.repo(:user)) { User.create! }


117
118
119
120
121
122
123
124
# File 'lib/esse/hooks/mixin.rb', line 117

def without_indexing(*repos)
  state_before_disable = state[:repos].dup
  disable!(*repos)

  yield
ensure
  state[:repos] = state_before_disable
end

#without_indexing_for_model(model_class, *repos) ⇒ Object

Disable model indexing callbacks execution for the block execution for the given model. Example:

BroadcastChanges.without_indexing_for_model(User) { }
BroadcastChanges.without_indexing_for_model(User, :datasync, :other) { }


143
144
145
146
147
148
149
150
151
152
153
# File 'lib/esse/hooks/mixin.rb', line 143

def without_indexing_for_model(model_class, *repos)
  state_before_disable = state[:models].dig(model_class).dup
  disable_model!(model_class, *repos)
  yield
ensure
  if state_before_disable.nil?
    state[:models].delete(model_class)
  else
    state[:models][model_class] = state_before_disable
  end
end