Class: Grape::Entity::Options

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/grape_entity/options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts_hash = {}) ⇒ Options

Returns a new instance of Options.


14
15
16
17
18
19
20
# File 'lib/grape_entity/options.rb', line 14

def initialize(opts_hash = {})
  @opts_hash = opts_hash
  @has_only = !opts_hash[:only].nil?
  @has_except = !opts_hash[:except].nil?
  @for_nesting_cache = {}
  @should_return_key_cache = {}
end

Instance Attribute Details

#opts_hashObject (readonly)

Returns the value of attribute opts_hash.


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

def opts_hash
  @opts_hash
end

Instance Method Details

#==(other) ⇒ Object


46
47
48
49
# File 'lib/grape_entity/options.rb', line 46

def ==(other)
  other_hash = other.is_a?(Options) ? other.opts_hash : other
  @opts_hash == other_hash
end

#except_fields(for_key = nil) ⇒ Object


75
76
77
78
79
80
81
82
83
# File 'lib/grape_entity/options.rb', line 75

def except_fields(for_key = nil)
  return nil unless @has_except

  @except_fields ||= @opts_hash[:except].each_with_object({}) do |attribute, allowed_fields|
    build_symbolized_hash(attribute, allowed_fields)
  end

  only_for_given(for_key, @except_fields)
end

#for_nesting(key) ⇒ Object


61
62
63
# File 'lib/grape_entity/options.rb', line 61

def for_nesting(key)
  @for_nesting_cache[key] ||= build_for_nesting(key)
end

#merge(new_opts) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/grape_entity/options.rb', line 22

def merge(new_opts)
  return self if new_opts.empty?

  merged = if new_opts.instance_of? Options
             @opts_hash.merge(new_opts.opts_hash)
           else
             @opts_hash.merge(new_opts)
           end

  Options.new(merged)
end

#only_fields(for_key = nil) ⇒ Object


65
66
67
68
69
70
71
72
73
# File 'lib/grape_entity/options.rb', line 65

def only_fields(for_key = nil)
  return nil unless @has_only

  @only_fields ||= @opts_hash[:only].each_with_object({}) do |attribute, allowed_fields|
    build_symbolized_hash(attribute, allowed_fields)
  end

  only_for_given(for_key, @only_fields)
end

#reverse_merge(new_opts) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/grape_entity/options.rb', line 34

def reverse_merge(new_opts)
  return self if new_opts.empty?

  merged = if new_opts.instance_of? Options
             new_opts.opts_hash.merge(@opts_hash)
           else
             new_opts.merge(@opts_hash)
           end

  Options.new(merged)
end

#should_return_key?(key) ⇒ Boolean

Returns:

  • (Boolean)

51
52
53
54
55
56
57
58
59
# File 'lib/grape_entity/options.rb', line 51

def should_return_key?(key)
  return true unless @has_only || @has_except

  only = only_fields.nil? ||
         only_fields.key?(key)
  except = except_fields&.key?(key) &&
           except_fields[key] == true
  only && !except
end

#with_attr_path(part) ⇒ Object


85
86
87
88
89
90
91
92
93
# File 'lib/grape_entity/options.rb', line 85

def with_attr_path(part)
  return yield unless part

  stack = (opts_hash[:attr_path] ||= [])
  stack.push part
  result = yield
  stack.pop
  result
end