Class: Grape::Entity::Exposure::Base
- Inherits:
-
Object
- Object
- Grape::Entity::Exposure::Base
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.
11
12
13
14
15
16
17
18
19
|
# File 'lib/grape_entity/exposure/base.rb', line 11
def initialize(attribute, options, conditions)
@attribute = attribute.try(:to_sym)
@options = options
@key = (options[:as] || attribute).try(:to_sym)
@is_safe = options[:safe]
@attr_path_proc = options[:attr_path]
@documentation = options[:documentation]
@conditions = conditions
end
|
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
5
6
7
|
# File 'lib/grape_entity/exposure/base.rb', line 5
def attribute
@attribute
end
|
#conditions ⇒ Object
Returns the value of attribute conditions.
5
6
7
|
# File 'lib/grape_entity/exposure/base.rb', line 5
def conditions
@conditions
end
|
#documentation ⇒ Object
Returns the value of attribute documentation.
5
6
7
|
# File 'lib/grape_entity/exposure/base.rb', line 5
def documentation
@documentation
end
|
#is_safe ⇒ Object
Returns the value of attribute is_safe.
5
6
7
|
# File 'lib/grape_entity/exposure/base.rb', line 5
def is_safe
@is_safe
end
|
#key ⇒ Object
Returns the value of attribute key.
5
6
7
|
# File 'lib/grape_entity/exposure/base.rb', line 5
def key
@key
end
|
Class Method Details
.new(attribute, options, conditions, *args, &block) ⇒ Object
7
8
9
|
# File 'lib/grape_entity/exposure/base.rb', line 7
def self.new(attribute, options, conditions, *args, &block)
super(attribute, options, conditions).tap { |e| e.setup(*args, &block) }
end
|
Instance Method Details
#==(other) ⇒ Object
29
30
31
32
33
34
|
# File 'lib/grape_entity/exposure/base.rb', line 29
def ==(other)
self.class == other.class &&
@attribute == other.attribute &&
@options == other.options &&
@conditions == other.conditions
end
|
#attr_path(entity, options) ⇒ Object
97
98
99
100
101
102
103
|
# File 'lib/grape_entity/exposure/base.rb', line 97
def attr_path(entity, options)
if @attr_path_proc
entity.exec_with_object(options, &@attr_path_proc)
else
@key
end
end
|
#conditional? ⇒ Boolean
85
86
87
|
# File 'lib/grape_entity/exposure/base.rb', line 85
def conditional?
!@conditions.empty?
end
|
#conditions_met?(entity, options) ⇒ Boolean
89
90
91
|
# File 'lib/grape_entity/exposure/base.rb', line 89
def conditions_met?(entity, options)
@conditions.all? { |condition| condition.met? entity, options }
end
|
#deep_complex_nesting? ⇒ Boolean
if we have any nesting exposures with the same name.
44
45
46
|
# File 'lib/grape_entity/exposure/base.rb', line 44
def deep_complex_nesting?
false
end
|
#dup(&block) ⇒ Object
21
22
23
|
# File 'lib/grape_entity/exposure/base.rb', line 21
def dup(&block)
self.class.new(*dup_args, &block)
end
|
#dup_args ⇒ Object
25
26
27
|
# File 'lib/grape_entity/exposure/base.rb', line 25
def dup_args
[@attribute, @options, @conditions.map(&:dup)]
end
|
#nesting? ⇒ Boolean
39
40
41
|
# File 'lib/grape_entity/exposure/base.rb', line 39
def nesting?
false
end
|
#serializable_value(entity, options) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/grape_entity/exposure/base.rb', line 61
def serializable_value(entity, options)
partial_output = valid_value(entity, options)
if partial_output.respond_to?(:serializable_hash)
partial_output.serializable_hash(options)
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
|
#setup ⇒ Object
36
37
|
# File 'lib/grape_entity/exposure/base.rb', line 36
def setup
end
|
#should_expose?(entity, options) ⇒ Boolean
93
94
95
|
# File 'lib/grape_entity/exposure/base.rb', line 93
def should_expose?(entity, options)
should_return_key?(options) && conditions_met?(entity, options)
end
|
#should_return_key?(options) ⇒ Boolean
81
82
83
|
# File 'lib/grape_entity/exposure/base.rb', line 81
def should_return_key?(options)
options.should_return_key?(@key)
end
|
#valid?(entity) ⇒ Boolean
48
49
50
51
52
53
54
55
|
# File 'lib/grape_entity/exposure/base.rb', line 48
def valid?(entity)
is_delegatable = entity.delegator.delegatable?(@attribute) || entity.respond_to?(@attribute, true)
if @is_safe
is_delegatable
else
is_delegatable || fail(NoMethodError, "#{entity.class.name} missing attribute `#{@attribute}' on #{entity.object}")
end
end
|
#valid_value(entity, options) ⇒ Object
77
78
79
|
# File 'lib/grape_entity/exposure/base.rb', line 77
def valid_value(entity, options)
value(entity, options) if valid?(entity)
end
|
#value(_entity, _options) ⇒ Object
57
58
59
|
# File 'lib/grape_entity/exposure/base.rb', line 57
def value(_entity, _options)
fail NotImplementedError
end
|
#with_attr_path(entity, options) ⇒ Object
105
106
107
108
109
110
|
# File 'lib/grape_entity/exposure/base.rb', line 105
def with_attr_path(entity, options)
path_part = attr_path(entity, options)
options.with_attr_path(path_part) do
yield
end
end
|