Module: Serega::SeregaAttribute::AttributeInstanceMethods
- Included in:
- Serega::SeregaAttribute
- Defined in:
- lib/serega/attribute.rb
Overview
Attribute instance methods
Instance Attribute Summary collapse
-
#hide ⇒ Boolean?
readonly
Attribute :hide option.
-
#initials ⇒ Hash
readonly
Attribute initial params.
-
#many ⇒ Boolean?
readonly
Attribute :many option.
-
#name ⇒ Symbol
readonly
Attribute name.
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) ⇒ Object
Finds attribute value.
-
#visible?(modifiers) ⇒ Boolean
Checks if attribute must be added to serialized response.
Instance Attribute Details
#hide ⇒ Boolean? (readonly)
Attribute :hide option
26 27 28 |
# File 'lib/serega/attribute.rb', line 26 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 |
Instance Method Details
#initialize(name:, opts: {}, block: nil) ⇒ Object
Initializes new attribute
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/serega/attribute.rb', line 41 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
57 58 59 |
# File 'lib/serega/attribute.rb', line 57 def relation? !@serializer.nil? end |
#serializer ⇒ Serega?
Shows specified serializer class
63 64 65 66 67 68 |
# File 'lib/serega/attribute.rb', line 63 def serializer ser = @serializer return ser if (ser.is_a?(Class) && (ser < Serega)) || !ser @serializer = ser.is_a?(String) ? Object.const_get(ser, false) : ser.call end |
#value(object, context) ⇒ Object
Finds attribute value
78 79 80 |
# File 'lib/serega/attribute.rb', line 78 def value(object, context) value_block.call(object, context) end |
#visible?(modifiers) ⇒ Boolean
Checks if attribute must be added to serialized response
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/serega/attribute.rb', line 92 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 |