Module: Serega::SeregaAttribute::AttributeInstanceMethods
- Included in:
- Serega::SeregaAttribute
- Defined in:
- lib/serega/attribute.rb
Overview
Stores Attribute instance methods
Instance Attribute Summary collapse
-
#block ⇒ Proc
readonly
Attribute originally added block.
-
#name ⇒ Symbol
readonly
Attribute name.
-
#opts ⇒ Hash
readonly
Attribute options.
Instance Method Summary collapse
-
#hide ⇒ Boolean?
Attribute initial :hide option value.
-
#initialize(name:, opts: {}, block: nil) ⇒ Object
Initializes new attribute.
-
#key ⇒ Symbol
Object method name to will be used to get attribute value unless block provided.
-
#many ⇒ Boolean?
Attribute initialization :many option.
-
#relation? ⇒ Boolean
Checks if attribute is relationship (if :serializer option exists).
-
#serializer ⇒ Serega?
Attribute serializer if exists.
-
#value(object, context) ⇒ Object
Finds attribute value.
-
#value_block ⇒ Proc
Proc to find attribute value.
-
#visible?(except:, only:, with:) ⇒ Boolean
Checks if attribute must be added to serialized response.
Instance Attribute Details
#block ⇒ Proc (readonly)
Returns Attribute originally added block.
19 20 21 |
# File 'lib/serega/attribute.rb', line 19 def block @block end |
#name ⇒ Symbol (readonly)
Returns Attribute name.
13 14 15 |
# File 'lib/serega/attribute.rb', line 13 def name @name end |
#opts ⇒ Hash (readonly)
Returns Attribute options.
16 17 18 |
# File 'lib/serega/attribute.rb', line 16 def opts @opts end |
Instance Method Details
#hide ⇒ Boolean?
Returns Attribute initial :hide option value.
49 50 51 |
# File 'lib/serega/attribute.rb', line 49 def hide opts[:hide] end |
#initialize(name:, opts: {}, block: nil) ⇒ Object
Initializes new attribute
35 36 37 38 39 40 41 |
# File 'lib/serega/attribute.rb', line 35 def initialize(name:, opts: {}, block: nil) self.class.serializer_class::CheckAttributeParams.new(name, opts, block).validate @name = name.to_sym @opts = SeregaUtils::EnumDeepDup.call(opts) @block = block end |
#key ⇒ Symbol
Returns Object method name to will be used to get attribute value unless block provided.
44 45 46 |
# File 'lib/serega/attribute.rb', line 44 def key @key ||= opts.key?(:key) ? opts[:key].to_sym : name end |
#many ⇒ Boolean?
Returns Attribute initialization :many option.
54 55 56 |
# File 'lib/serega/attribute.rb', line 54 def many opts[:many] end |
#relation? ⇒ Boolean
Returns Checks if attribute is relationship (if :serializer option exists).
59 60 61 |
# File 'lib/serega/attribute.rb', line 59 def relation? !opts[:serializer].nil? end |
#serializer ⇒ Serega?
Returns Attribute serializer if exists.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/serega/attribute.rb', line 64 def serializer return @serializer if instance_variable_defined?(:@serializer) serializer = opts[:serializer] @serializer = case serializer when String then Object.const_get(serializer, false) when Proc then serializer.call else serializer end end |
#value(object, context) ⇒ Object
Finds attribute value
91 92 93 |
# File 'lib/serega/attribute.rb', line 91 def value(object, context) value_block.call(object, context) end |
#value_block ⇒ Proc
Returns Proc to find attribute value.
77 78 79 80 81 |
# File 'lib/serega/attribute.rb', line 77 def value_block return @value_block if instance_variable_defined?(:@value_block) @value_block = block || opts[:value] || const_block || delegate_block || keyword_block end |
#visible?(except:, only:, with:) ⇒ Boolean
Checks if attribute must be added to serialized response
104 105 106 107 108 109 110 111 |
# File 'lib/serega/attribute.rb', line 104 def visible?(except:, only:, with:) 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 |