lib/grape_entity/exposure/base.rb in grape-entity-0.6.1 vs lib/grape_entity/exposure/base.rb in grape-entity-0.7.0
- old
+ new
@@ -1,19 +1,22 @@
+# frozen_string_literal: true
+
module Grape
class Entity
module Exposure
class Base
- attr_reader :attribute, :key, :is_safe, :documentation, :conditions, :for_merge
+ attr_reader :attribute, :is_safe, :documentation, :conditions, :for_merge
def self.new(attribute, options, conditions, *args, &block)
super(attribute, options, conditions).tap { |e| e.setup(*args, &block) }
end
def initialize(attribute, options, conditions)
@attribute = attribute.try(:to_sym)
@options = options
- @key = (options[:as] || attribute).try(:to_sym)
+ key = options[:as] || attribute
+ @key = key.respond_to?(:to_sym) ? key.to_sym : key
@is_safe = options[:safe]
@for_merge = options[:merge]
@attr_path_proc = options[:attr_path]
@documentation = options[:documentation]
@conditions = conditions
@@ -39,11 +42,11 @@
def nesting?
false
end
# if we have any nesting exposures with the same name.
- def deep_complex_nesting?
+ def deep_complex_nesting?(entity) # rubocop:disable Lint/UnusedMethodArgument
false
end
def valid?(entity)
is_delegatable = entity.delegator.delegatable?(@attribute) || entity.respond_to?(@attribute, true)
@@ -100,14 +103,22 @@
else
@key
end
end
+ def key(entity = nil)
+ @key.respond_to?(:call) ? entity.exec_with_object(@options, &@key) : @key
+ end
+
def with_attr_path(entity, options)
path_part = attr_path(entity, options)
options.with_attr_path(path_part) do
yield
end
+ end
+
+ def replaceable_by?(other)
+ !nesting? && !conditional? && !other.nesting? && !other.conditional?
end
protected
attr_reader :options