Class: Serega::SeregaPlugins::Batch::BatchModel
- Inherits:
-
Object
- Object
- Serega::SeregaPlugins::Batch::BatchModel
- Defined in:
- lib/serega/plugins/batch/batch.rb
Overview
Stores batch config for specific attribute
Instance Attribute Summary collapse
-
#loaders ⇒ Object
readonly
Returns the value of attribute loaders.
-
#many ⇒ Object
readonly
Returns the value of attribute many.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
-
#default_value ⇒ Object
Returns default value to use if batch loader does not return value for some key.
-
#initialize(opts, loaders, many) ⇒ void
constructor
Initializes batch model.
-
#key ⇒ Object
Returns proc that will be used to find batch_key for current attribute.
-
#loader ⇒ #call
Returns proc that will be used to batch load registered keys values.
Constructor Details
#initialize(opts, loaders, many) ⇒ void
Initializes batch model
157 158 159 160 161 |
# File 'lib/serega/plugins/batch/batch.rb', line 157 def initialize(opts, loaders, many) @opts = opts @loaders = loaders @many = many end |
Instance Attribute Details
#loaders ⇒ Object (readonly)
Returns the value of attribute loaders.
147 148 149 |
# File 'lib/serega/plugins/batch/batch.rb', line 147 def loaders @loaders end |
#many ⇒ Object (readonly)
Returns the value of attribute many.
147 148 149 |
# File 'lib/serega/plugins/batch/batch.rb', line 147 def many @many end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
147 148 149 |
# File 'lib/serega/plugins/batch/batch.rb', line 147 def opts @opts end |
Instance Method Details
#default_value ⇒ Object
Returns default value to use if batch loader does not return value for some key
184 185 186 187 188 189 190 |
# File 'lib/serega/plugins/batch/batch.rb', line 184 def default_value if opts.key?(:default) opts[:default] elsif many FROZEN_EMPTY_ARRAY end end |
#key ⇒ Object
Returns proc that will be used to find batch_key for current attribute.
175 176 177 178 179 180 |
# File 'lib/serega/plugins/batch/batch.rb', line 175 def key @batch_key ||= begin key = opts[:key] key.is_a?(Symbol) ? proc { |object| object.public_send(key) } : key end end |
#loader ⇒ #call
Returns proc that will be used to batch load registered keys values
165 166 167 168 169 170 171 |
# File 'lib/serega/plugins/batch/batch.rb', line 165 def loader @batch_loader ||= begin loader = opts[:loader] loader = loaders.fetch(loader) if loader.is_a?(Symbol) loader end end |