Class: Grape::Entity::Options
- Inherits:
-
Object
- Object
- Grape::Entity::Options
- Defined in:
- lib/grape_entity/options.rb
Instance Attribute Summary collapse
-
#opts_hash ⇒ Object
readonly
Returns the value of attribute opts_hash.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #empty? ⇒ Boolean
- #except_fields(for_key = nil) ⇒ Object
- #for_nesting(key) ⇒ Object
-
#initialize(opts_hash = {}) ⇒ Options
constructor
A new instance of Options.
- #key?(key) ⇒ Boolean
- #merge(new_opts) ⇒ Object
- #only_fields(for_key = nil) ⇒ Object
- #reverse_merge(new_opts) ⇒ Object
- #should_return_key?(key) ⇒ Boolean
- #with_attr_path(part) ⇒ Object
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_hash ⇒ Object (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
52 53 54 55 56 57 58 |
# File 'lib/grape_entity/options.rb', line 52 def ==(other) if other.is_a? Options @opts_hash == other.opts_hash else @opts_hash == other end end |
#[](key) ⇒ Object
14 15 16 |
# File 'lib/grape_entity/options.rb', line 14 def [](key) @opts_hash[key] end |
#empty? ⇒ Boolean
48 49 50 |
# File 'lib/grape_entity/options.rb', line 48 def empty? @opts_hash.empty? end |
#except_fields(for_key = nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/grape_entity/options.rb', line 95 def except_fields(for_key = nil) return nil unless @has_except @except_fields ||= @opts_hash[:except].each_with_object({}) do |attribute, allowed_fields| if attribute.is_a?(Hash) attribute.each do |attr, nested_attrs| allowed_fields[attr] ||= [] allowed_fields[attr] += nested_attrs end else allowed_fields[attribute] = true end end.symbolize_keys if for_key && @except_fields[for_key].is_a?(Array) @except_fields[for_key] elsif for_key.nil? @except_fields end end |
#for_nesting(key) ⇒ Object
70 71 72 |
# File 'lib/grape_entity/options.rb', line 70 def for_nesting(key) @for_nesting_cache[key] ||= build_for_nesting(key) end |
#key?(key) ⇒ Boolean
18 19 20 |
# File 'lib/grape_entity/options.rb', line 18 def key?(key) @opts_hash.key? key end |
#merge(new_opts) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/grape_entity/options.rb', line 22 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/grape_entity/options.rb', line 74 def only_fields(for_key = nil) return nil unless @has_only @only_fields ||= @opts_hash[:only].each_with_object({}) do |attribute, allowed_fields| if attribute.is_a?(Hash) attribute.each do |attr, nested_attrs| allowed_fields[attr] ||= [] allowed_fields[attr] += nested_attrs end else allowed_fields[attribute] = true end end.symbolize_keys if for_key && @only_fields[for_key].is_a?(Array) @only_fields[for_key] elsif for_key.nil? @only_fields end end |
#reverse_merge(new_opts) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/grape_entity/options.rb', line 35 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
60 61 62 63 64 65 66 67 68 |
# File 'lib/grape_entity/options.rb', line 60 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
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/grape_entity/options.rb', line 116 def with_attr_path(part) stack = (opts_hash[:attr_path] ||= []) if part stack.push part result = yield stack.pop result else yield end end |