Module: Jat::Attribute::InstanceMethods

Included in:
Jat::Attribute
Defined in:
lib/jat/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



9
10
11
# File 'lib/jat/attribute.rb', line 9

def opts
  @opts
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/jat/attribute.rb', line 9

def params
  @params
end

Instance Method Details

#blockObject



63
64
65
66
67
68
69
70
# File 'lib/jat/attribute.rb', line 63

def block
  return @block if instance_variable_defined?(:@block)

  current_block = params.fetch(:block)
  current_block ||= keyword_block

  @block = current_block
end

#exposed?Boolean

Checks if attribute is exposed

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
# File 'lib/jat/attribute.rb', line 34

def exposed?
  return @exposed if instance_variable_defined?(:@exposed)

  @exposed =
    case self.class.jat_class.config[:exposed]
    when :all then opts.fetch(:exposed, true)
    when :none then opts.fetch(:exposed, false)
    else opts.fetch(:exposed, !relation?)
    end
end

#initialize(name:, opts: {}, block: nil) ⇒ Object



11
12
13
14
15
16
# File 'lib/jat/attribute.rb', line 11

def initialize(name:, opts: {}, block: nil)
  check_block_valid(block)

  @opts = EnumDeepDup.call(opts)
  @params = EnumDeepFreeze.call(name: name, opts: @opts, block: block)
end

#keyObject

Object method name to get attribute value



24
25
26
# File 'lib/jat/attribute.rb', line 24

def key
  @key ||= opts.key?(:key) ? opts[:key].to_sym : original_name
end

#many?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/jat/attribute.rb', line 45

def many?
  return @many if instance_variable_defined?(:@many)

  @many = opts[:many]
end

#nameObject

Attribute name that will be used in serialized response



29
30
31
# File 'lib/jat/attribute.rb', line 29

def name
  @name ||= original_name
end

#original_nameObject

Attribute name that was provided when initializing attribute



19
20
21
# File 'lib/jat/attribute.rb', line 19

def original_name
  @original_name ||= params.fetch(:name).to_sym
end

#relation?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/jat/attribute.rb', line 51

def relation?
  return @relation if instance_variable_defined?(:@relation)

  @relation = opts.key?(:serializer)
end

#serializerObject



57
58
59
60
61
# File 'lib/jat/attribute.rb', line 57

def serializer
  return @serializer if instance_variable_defined?(:@serializer)

  @serializer = opts[:serializer]
end

#value(object, context) ⇒ Object



72
73
74
# File 'lib/jat/attribute.rb', line 72

def value(object, context)
  block.call(object, context)
end