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.
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
40 41 42 |
# File 'lib/serega/attribute.rb', line 40 def batch_loaders @batch_loaders end |
#default ⇒ Object? (readonly)
Attribute :default option
28 29 30 |
# File 'lib/serega/attribute.rb', line 28 def default @default end |
#hide ⇒ Boolean? (readonly)
Attribute :hide option
32 33 34 |
# File 'lib/serega/attribute.rb', line 32 def hide @hide end |
#initials ⇒ Hash (readonly)
Attribute initial params
16 17 18 |
# File 'lib/serega/attribute.rb', line 16 def initials @initials end |
#many ⇒ Boolean? (readonly)
Attribute :many option
24 25 26 |
# File 'lib/serega/attribute.rb', line 24 def many @many end |
#name ⇒ Symbol (readonly)
Attribute name
20 21 22 |
# File 'lib/serega/attribute.rb', line 20 def name @name end |
#preloads ⇒ Hash? (readonly)
Attribute :preloads option
36 37 38 |
# File 'lib/serega/attribute.rb', line 36 def preloads @preloads end |
Instance Method Details
#initialize(name:, opts: {}, block: nil) ⇒ Object
Initializes new attribute
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/serega/attribute.rb', line 55 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
71 72 73 |
# File 'lib/serega/attribute.rb', line 71 def relation? !@serializer.nil? end |
#serializer ⇒ Serega?
Shows specified serializer class
77 78 79 80 81 82 |
# File 'lib/serega/attribute.rb', line 77 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/serega/attribute.rb', line 92 def value(object, context, batches: nil) # Signatures should match allowed signatures in CheckOptValue 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
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/serega/attribute.rb', line 118 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 |