Class: Grape::Entity::Options

Inherits:
Object
  • Object
show all
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.



6
7
8
9
10
11
12
# File 'lib/grape_entity/options.rb', line 6

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.



4
5
6
# File 'lib/grape_entity/options.rb', line 4

def opts_hash
  @opts_hash
end

Instance Method Details

#==(other) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/grape_entity/options.rb', line 56

def ==(other)
  @opts_hash == if other.is_a? Options
                  other.opts_hash
                else
                  other
                end
end

#[](key) ⇒ Object



14
15
16
# File 'lib/grape_entity/options.rb', line 14

def [](key)
  @opts_hash[key]
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @opts_hash.empty?
end

#except_fields(for_key = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/grape_entity/options.rb', line 88

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

#fetch(*args) ⇒ Object



18
19
20
# File 'lib/grape_entity/options.rb', line 18

def fetch(*args)
  @opts_hash.fetch(*args)
end

#for_nesting(key) ⇒ Object



74
75
76
# File 'lib/grape_entity/options.rb', line 74

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/grape_entity/options.rb', line 22

def key?(key)
  @opts_hash.key? key
end

#merge(new_opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/grape_entity/options.rb', line 26

def merge(new_opts)
  if new_opts.empty?
    self
  else
    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
end

#only_fields(for_key = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/grape_entity/options.rb', line 78

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



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/grape_entity/options.rb', line 39

def reverse_merge(new_opts)
  if new_opts.empty?
    self
  else
    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
end

#should_return_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

#with_attr_path(part) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/grape_entity/options.rb', line 98

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