Module: Serega::SeregaAttribute::AttributeInstanceMethods
- Included in:
- Serega::SeregaAttribute
- Defined in:
- lib/serega/attribute.rb
Overview
Attribute instance methods
Instance Attribute Summary collapse
-
#block ⇒ Proc
readonly
Returns attribute block.
-
#name ⇒ Symbol
readonly
Returns attribute name.
-
#opts ⇒ Hash
readonly
Returns attribute options.
Instance Method Summary collapse
-
#hide ⇒ Boolean?
Shows current opts[:hide] option.
-
#initialize(name:, opts: {}, block: nil) ⇒ Object
Initializes new attribute.
-
#key ⇒ Symbol
Method name that will be used to get attribute value.
-
#many ⇒ Boolean?
Shows current opts[:many] option.
-
#relation? ⇒ Boolean
Shows whether attribute has specified serializer.
-
#serializer ⇒ Serega?
Shows specified serializer class.
-
#value(object, context) ⇒ Object
Finds attribute value.
-
#value_block ⇒ Proc
Returns final block that will be used 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 block
22 23 24 |
# File 'lib/serega/attribute.rb', line 22 def block @block end |
#name ⇒ Symbol (readonly)
Returns attribute name
14 15 16 |
# File 'lib/serega/attribute.rb', line 14 def name @name end |
#opts ⇒ Hash (readonly)
Returns attribute options
18 19 20 |
# File 'lib/serega/attribute.rb', line 18 def opts @opts end |
Instance Method Details
#hide ⇒ Boolean?
Shows current opts[:hide] option
55 56 57 |
# File 'lib/serega/attribute.rb', line 55 def hide opts[:hide] end |
#initialize(name:, opts: {}, block: nil) ⇒ Object
Initializes new attribute
38 39 40 41 42 43 44 |
# File 'lib/serega/attribute.rb', line 38 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
Method name that will be used to get attribute value
49 50 51 |
# File 'lib/serega/attribute.rb', line 49 def key @key ||= opts.key?(:key) ? opts[:key].to_sym : name end |
#many ⇒ Boolean?
Shows current opts[:many] option
61 62 63 |
# File 'lib/serega/attribute.rb', line 61 def many opts[:many] end |
#relation? ⇒ Boolean
Shows whether attribute has specified serializer
67 68 69 |
# File 'lib/serega/attribute.rb', line 67 def relation? !opts[:serializer].nil? end |
#serializer ⇒ Serega?
Shows specified serializer class
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/serega/attribute.rb', line 73 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
106 107 108 |
# File 'lib/serega/attribute.rb', line 106 def value(object, context) value_block.call(object, context) end |
#value_block ⇒ Proc
Returns final block that will be used to find attribute value
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/serega/attribute.rb', line 87 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
119 120 121 122 123 124 125 126 |
# File 'lib/serega/attribute.rb', line 119 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 |