Module: Serega::SeregaAttribute::AttributeInstanceMethods
- Included in:
- Serega::SeregaAttribute
- Defined in:
- lib/serega/attribute.rb
Overview
Attribute instance methods
Instance Attribute Summary collapse
-
#batch_loaders ⇒ Array<Symbol>
readonly
Batch loader names required to detect attribute value.
-
#default ⇒ Object?
readonly
Attribute :default option.
-
#hide ⇒ Boolean?
readonly
Attribute :hide option.
-
#initials ⇒ Hash
readonly
Attribute initial params.
-
#many ⇒ Boolean?
readonly
Attribute :many option.
-
#name ⇒ Symbol
readonly
Attribute name.
-
#preloads ⇒ Hash?
readonly
Attribute :preloads option.
-
#preloads_path ⇒ Array?
readonly
Attribute :preloads_path option.
Instance Method Summary collapse
-
#initialize(name:, opts: {}, block: nil) ⇒ Object
Initializes new attribute.
-
#relation? ⇒ Boolean
Shows whether attribute has specified serializer.
-
#serializer ⇒ Serega?
Shows specified serializer class.
-
#value(object, context, batches: nil) ⇒ Object
Finds attribute value.
-
#visible?(modifiers) ⇒ Boolean
Checks if attribute must be added to serialized response.
Instance Attribute Details
#batch_loaders ⇒ Array<Symbol> (readonly)
Batch loader names required to detect attribute value
42 43 44 |
# File 'lib/serega/attribute.rb', line 42 def batch_loaders @batch_loaders end |
#default ⇒ Object? (readonly)
Attribute :default option
26 27 28 |
# File 'lib/serega/attribute.rb', line 26 def default @default end |
#hide ⇒ Boolean? (readonly)
Attribute :hide option
30 31 32 |
# File 'lib/serega/attribute.rb', line 30 def hide @hide end |
#initials ⇒ Hash (readonly)
Attribute initial params
14 15 16 |
# File 'lib/serega/attribute.rb', line 14 def initials @initials end |
#many ⇒ Boolean? (readonly)
Attribute :many option
22 23 24 |
# File 'lib/serega/attribute.rb', line 22 def many @many end |
#name ⇒ Symbol (readonly)
Attribute name
18 19 20 |
# File 'lib/serega/attribute.rb', line 18 def name @name end |
#preloads ⇒ Hash? (readonly)
Attribute :preloads option
34 35 36 |
# File 'lib/serega/attribute.rb', line 34 def preloads @preloads end |
#preloads_path ⇒ Array? (readonly)
Attribute :preloads_path option
38 39 40 |
# File 'lib/serega/attribute.rb', line 38 def preloads_path @preloads_path end |
Instance Method Details
#initialize(name:, opts: {}, block: nil) ⇒ Object
Initializes new attribute
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/serega/attribute.rb', line 57 def initialize(name:, opts: {}, block: nil) serializer_class = self.class.serializer_class serializer_class::CheckAttributeParams.new(name, opts, block).validate @initials = SeregaUtils::EnumDeepFreeze.call( name: name, opts: SeregaUtils::EnumDeepDup.call(opts), block: block ) normalizer = serializer_class::SeregaAttributeNormalizer.new(initials) set_normalized_vars(normalizer) end |
#relation? ⇒ Boolean
Shows whether attribute has specified serializer
73 74 75 |
# File 'lib/serega/attribute.rb', line 73 def relation? !@serializer.nil? end |
#serializer ⇒ Serega?
Shows specified serializer class
79 80 81 82 83 84 |
# File 'lib/serega/attribute.rb', line 79 def serializer serializer = @serializer return serializer if (serializer.is_a?(Class) && (serializer < Serega)) || !serializer @serializer = serializer.is_a?(String) ? Object.const_get(serializer, false) : serializer.call end |
#value(object, context, batches: nil) ⇒ Object
Finds attribute value
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/serega/attribute.rb', line 94 def value(object, context, batches: nil) # Signatires should match allowed signatures in CheckOptValue and CheckBlock result = case value_block_signature when "1" then value_block.call(object) when "1_ctx" then value_block.call(object, ctx: context) when "1_batches" then value_block.call(object, batches: batches) when "1_batches_ctx" then value_block.call(object, ctx: context, batches: batches) when "2" then value_block.call(object, context) when "2_batches_ctx" then value_block.call(object, context, ctx: context, batches: batches) else value_block.call # signature is "0" - no parameters end result.nil? ? default : result end |
#visible?(modifiers) ⇒ Boolean
Checks if attribute must be added to serialized response
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/serega/attribute.rb', line 120 def visible?(modifiers) except = modifiers[:except] || FROZEN_EMPTY_HASH only = modifiers[:only] || FROZEN_EMPTY_HASH with = modifiers[:with] || FROZEN_EMPTY_HASH return false if except.member?(name) && except[name].empty? return true if only.member?(name) return true if with.member?(name) return false unless only.empty? !hide end |