Class: Grape::Entity::Exposure::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_entity/exposure/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, options, conditions) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/grape_entity/exposure/base.rb', line 16

def initialize(attribute, options, conditions)
  @attribute = attribute.try(:to_sym)
  @options = options
  key = options[:as] || attribute
  @key = key.respond_to?(:to_sym) ? key.to_sym : key
  @is_safe = options[:safe]
  @default_value = options[:default]
  @for_merge = options[:merge]
  @attr_path_proc = options[:attr_path]
  @documentation = options[:documentation]
  @override = options[:override]
  @conditions = conditions
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



10
11
12
# File 'lib/grape_entity/exposure/base.rb', line 10

def attribute
  @attribute
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



10
11
12
# File 'lib/grape_entity/exposure/base.rb', line 10

def conditions
  @conditions
end

#documentationObject (readonly)

Returns the value of attribute documentation.



10
11
12
# File 'lib/grape_entity/exposure/base.rb', line 10

def documentation
  @documentation
end

#for_mergeObject (readonly)

Returns the value of attribute for_merge.



10
11
12
# File 'lib/grape_entity/exposure/base.rb', line 10

def for_merge
  @for_merge
end

#is_safeObject (readonly)

Returns the value of attribute is_safe.



10
11
12
# File 'lib/grape_entity/exposure/base.rb', line 10

def is_safe
  @is_safe
end

#overrideObject (readonly)

Returns the value of attribute override.



10
11
12
# File 'lib/grape_entity/exposure/base.rb', line 10

def override
  @override
end

Class Method Details

.new(attribute, options, conditions) ⇒ Object



12
13
14
# File 'lib/grape_entity/exposure/base.rb', line 12

def self.new(attribute, options, conditions, ...)
  super(attribute, options, conditions).tap { |e| e.setup(...) }
end

Instance Method Details

#==(other) ⇒ Object



38
39
40
41
42
43
# File 'lib/grape_entity/exposure/base.rb', line 38

def ==(other)
  self.class == other.class &&
    @attribute == other.attribute &&
    @options == other.options &&
    @conditions == other.conditions
end

#attr_path(entity, options) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/grape_entity/exposure/base.rb', line 111

def attr_path(entity, options)
  if @attr_path_proc
    entity.exec_with_object(options, &@attr_path_proc)
  else
    @key
  end
end

#conditional?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/grape_entity/exposure/base.rb', line 99

def conditional?
  !@conditions.empty?
end

#conditions_met?(entity, options) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/grape_entity/exposure/base.rb', line 103

def conditions_met?(entity, options)
  @conditions.all? { |condition| condition.met? entity, options }
end

#deep_complex_nesting?(entity) ⇒ Boolean

if we have any nesting exposures with the same name.

Returns:

  • (Boolean)


52
53
54
# File 'lib/grape_entity/exposure/base.rb', line 52

def deep_complex_nesting?(entity) # rubocop:disable Lint/UnusedMethodArgument
  false
end

#dup(&block) ⇒ Object



30
31
32
# File 'lib/grape_entity/exposure/base.rb', line 30

def dup(&block)
  self.class.new(*dup_args, &block)
end

#dup_argsObject



34
35
36
# File 'lib/grape_entity/exposure/base.rb', line 34

def dup_args
  [@attribute, @options, @conditions.map(&:dup)]
end

#key(entity = nil) ⇒ Object



119
120
121
# File 'lib/grape_entity/exposure/base.rb', line 119

def key(entity = nil)
  @key.respond_to?(:call) ? entity.exec_with_object(@options, &@key) : @key
end

#nesting?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/grape_entity/exposure/base.rb', line 47

def nesting?
  false
end

#override?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/grape_entity/exposure/base.rb', line 128

def override?
  @override
end

#serializable_value(entity, options) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/grape_entity/exposure/base.rb', line 72

def serializable_value(entity, options)
  partial_output = valid_value(entity, options)

  if partial_output.respond_to?(:serializable_hash)
    partial_output.serializable_hash
  elsif partial_output.is_a?(Array) && partial_output.all? { |o| o.respond_to?(:serializable_hash) }
    partial_output.map(&:serializable_hash)
  elsif partial_output.is_a?(Hash)
    partial_output.each do |key, value|
      partial_output[key] = value.serializable_hash if value.respond_to?(:serializable_hash)
    end
  else
    partial_output
  end
end

#setupObject



45
# File 'lib/grape_entity/exposure/base.rb', line 45

def setup; end

#should_expose?(entity, options) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/grape_entity/exposure/base.rb', line 107

def should_expose?(entity, options)
  should_return_key?(options) && conditions_met?(entity, options)
end

#should_return_key?(options) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/grape_entity/exposure/base.rb', line 95

def should_return_key?(options)
  options.should_return_key?(@key)
end

#valid?(entity) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/grape_entity/exposure/base.rb', line 56

def valid?(entity)
  is_delegatable = entity.delegator.delegatable?(@attribute) || entity.respond_to?(@attribute, true)
  if @is_safe
    is_delegatable
  else
    is_delegatable || raise(
      NoMethodError,
      "#{entity.class.name} missing attribute `#{@attribute}' on #{entity.object}"
    )
  end
end

#valid_value(entity, options) ⇒ Object



88
89
90
91
92
93
# File 'lib/grape_entity/exposure/base.rb', line 88

def valid_value(entity, options)
  return unless valid?(entity)

  output = value(entity, options)
  output.blank? && !@default_value.nil? ? @default_value : output
end

#value(_entity, _options) ⇒ Object

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/grape_entity/exposure/base.rb', line 68

def value(_entity, _options)
  raise NotImplementedError
end

#with_attr_path(entity, options, &block) ⇒ Object



123
124
125
126
# File 'lib/grape_entity/exposure/base.rb', line 123

def with_attr_path(entity, options, &block)
  path_part = attr_path(entity, options)
  options.with_attr_path(path_part, &block)
end