Module: Serega::SeregaAttribute::AttributeInstanceMethods
- Included in:
- Serega::SeregaAttribute
- Defined in:
- lib/serega/attribute.rb
Overview
Attribute instance methods
Instance Attribute Summary collapse
-
#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.
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
#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 |
Instance Method Details
#initialize(name:, opts: {}, block: nil) ⇒ Object
Initializes new attribute
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/serega/attribute.rb', line 45 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
61 62 63 |
# File 'lib/serega/attribute.rb', line 61 def relation? !@serializer.nil? end |
#serializer ⇒ Serega?
Shows specified serializer class
67 68 69 70 71 72 |
# File 'lib/serega/attribute.rb', line 67 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) ⇒ Object
Finds attribute value
82 83 84 |
# File 'lib/serega/attribute.rb', line 82 def value(object, context) value_block.call(object, context) end |
#visible?(modifiers) ⇒ Boolean
Checks if attribute must be added to serialized response
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/serega/attribute.rb', line 96 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 |