Class: Serega::SeregaPlugins::Batch::BatchOptionModel
- Inherits:
-
Object
- Object
- Serega::SeregaPlugins::Batch::BatchOptionModel
- Defined in:
- lib/serega/plugins/batch/lib/batch_option_model.rb
Overview
Combines options and methods needed to load batch for specific attribute
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#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(attribute) ⇒ void
constructor
Initializes BatchOptionModel.
-
#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(attribute) ⇒ void
Initializes BatchOptionModel
20 21 22 23 24 25 |
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 20 def initialize(attribute) @attribute = attribute @opts = attribute.batch @loaders = attribute.class.serializer_class.config.batch_loaders @many = attribute.many end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
10 11 12 |
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 10 def attribute @attribute end |
#loaders ⇒ Object (readonly)
Returns the value of attribute loaders.
10 11 12 |
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 10 def loaders @loaders end |
#many ⇒ Object (readonly)
Returns the value of attribute many.
10 11 12 |
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 10 def many @many end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
10 11 12 |
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 10 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
55 56 57 58 59 60 61 |
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 55 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.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 39 def key @batch_key ||= begin key = opts[:key] if key.is_a?(Symbol) proc do |object| handle_no_method_error { object.public_send(key) } end else key end end end |
#loader ⇒ #call
Returns proc that will be used to batch load registered keys values
29 30 31 32 33 34 35 |
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 29 def loader @batch_loader ||= begin loader = opts[:loader] loader = loaders.fetch(loader) if loader.is_a?(Symbol) loader end end |