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


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

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


6
7
8
# File 'lib/grape_entity/options.rb', line 6

def opts_hash
  @opts_hash
end

Instance Method Details

#==(other) ⇒ Object


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

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

#[](key) ⇒ Object


16
17
18
# File 'lib/grape_entity/options.rb', line 16

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

#empty?Boolean

Returns:

  • (Boolean)

54
55
56
# File 'lib/grape_entity/options.rb', line 54

def empty?
  @opts_hash.empty?
end

#except_fields(for_key = nil) ⇒ Object


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

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


20
21
22
# File 'lib/grape_entity/options.rb', line 20

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

#for_nesting(key) ⇒ Object


76
77
78
# File 'lib/grape_entity/options.rb', line 76

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)

24
25
26
# File 'lib/grape_entity/options.rb', line 24

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

#merge(new_opts) ⇒ Object


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

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


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

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


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

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)

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

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


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

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