Class: Serega::SeregaPlugins::Batch::BatchConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/serega/plugins/batch/lib/batch_config.rb

Overview

Batch plugin config

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ BatchConfig

Returns a new instance of BatchConfig.



12
13
14
# File 'lib/serega/plugins/batch/lib/batch_config.rb', line 12

def initialize(opts)
  @opts = opts
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



10
11
12
# File 'lib/serega/plugins/batch/lib/batch_config.rb', line 10

def opts
  @opts
end

Instance Method Details

#auto_hideBoolean?

Shows option to auto hide attributes with :batch specified

Returns:

  • (Boolean, nil)

    option value



59
60
61
# File 'lib/serega/plugins/batch/lib/batch_config.rb', line 59

def auto_hide
  opts[:auto_hide]
end

#auto_hide=(value) ⇒ Boolean

Returns New option value.

Parameters:

  • value (Boolean)

    New :auto_hide option value

Returns:

  • (Boolean)

    New option value

Raises:



65
66
67
68
# File 'lib/serega/plugins/batch/lib/batch_config.rb', line 65

def auto_hide=(value)
  raise SeregaError, "Must have boolean value, #{value.inspect} provided" if (value != true) && (value != false)
  opts[:auto_hide] = value
end

#default_keySymbol?

Shows default key for :batch option

Returns:

  • (Symbol, nil)

    default key for :batch option



72
73
74
# File 'lib/serega/plugins/batch/lib/batch_config.rb', line 72

def default_key
  opts[:default_key]
end

#default_key=(value) ⇒ Boolean

Returns New option value.

Parameters:

  • value (Symbol)

    New :default_key option value

Returns:

  • (Boolean)

    New option value

Raises:



78
79
80
81
# File 'lib/serega/plugins/batch/lib/batch_config.rb', line 78

def default_key=(value)
  raise SeregaError, "Must be a Symbol, #{value.inspect} provided" unless value.is_a?(Symbol)
  opts[:default_key] = value
end

#define(loader_name, callable = nil, &block) ⇒ void

This method returns an undefined value.

Defines batch loader

Parameters:

  • loader_name (Symbol)

    Batch loader name, that is used when defining attribute with batch loader.

  • block (Proc)

    Block that can accept 3 parameters - keys, context, plan_point and returns hash where ids are keys and values are batch loaded objects/



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/serega/plugins/batch/lib/batch_config.rb', line 25

def define(loader_name, callable = nil, &block)
  if (!callable && !block) || (callable && block)
    raise SeregaError, "Batch loader can be specified with one of arguments - callable value or &block"
  end

  callable ||= block
  SeregaValidations::Utils::CheckExtraKeywordArg.call(callable, "batch loader `#{loader_name}`")
  params_count = SeregaUtils::ParamsCount.call(callable, max_count: 3)

  if params_count > 3
    raise SeregaError, "Batch loader can have maximum 3 parameters (keys, context, plan)"
  end

  loaders[loader_name] = callable
end

#fetch_loader(loader_name) ⇒ Proc

Finds previously defined batch loader by name

Parameters:

  • loader_name (Symbol)

Returns:

  • (Proc)

    batch loader block



53
54
55
# File 'lib/serega/plugins/batch/lib/batch_config.rb', line 53

def fetch_loader(loader_name)
  loaders[loader_name] || (raise SeregaError, "Batch loader with name `#{loader_name.inspect}` was not defined. Define example: config.batch.define(:#{loader_name}) { |keys| ... }")
end

#loadersHash

Shows defined loaders

Returns:

  • (Hash)

    defined loaders



43
44
45
# File 'lib/serega/plugins/batch/lib/batch_config.rb', line 43

def loaders
  opts[:loaders]
end