Class: JSONAPI::IncludeDirective
- Inherits:
-
Object
- Object
- JSONAPI::IncludeDirective
- Defined in:
- lib/jsonapi/include_directive.rb,
lib/jsonapi/include_directive/parser.rb
Overview
Represent a recursive set of include directives (c.f. jsonapi.org/format/#fetching-includes)
Addition to the spec: two wildcards, namely ‘*’ and ‘**’. The former stands for any one level of relationship, and the latter stands for any number of levels of relationships.
Defined Under Namespace
Modules: Parser
Instance Method Summary collapse
- #[](key) ⇒ IncludeDirective?
-
#initialize(include_args, options = {}) ⇒ IncludeDirective
constructor
A new instance of IncludeDirective.
- #key?(key) ⇒ Boolean
- #keys ⇒ Array<Symbol>
- #to_hash ⇒ Hash{Symbol => Hash}
- #to_string ⇒ String
Constructor Details
#initialize(include_args, options = {}) ⇒ IncludeDirective
Returns a new instance of IncludeDirective.
16 17 18 19 20 21 22 |
# File 'lib/jsonapi/include_directive.rb', line 16 def initialize(include_args, = {}) include_hash = Parser.parse_include_args(include_args) @hash = include_hash.each_with_object({}) do |(key, value), hash| hash[key] = self.class.new(value, ) end = end |
Instance Method Details
#[](key) ⇒ IncludeDirective?
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/jsonapi/include_directive.rb', line 37 def [](key) case when @hash.key?(key.to_sym) @hash[key.to_sym] when [:allow_wildcard] && @hash.key?(:**) self.class.new({ :** => {} }, ) when [:allow_wildcard] && @hash.key?(:*) @hash[:*] end end |
#key?(key) ⇒ Boolean
25 26 27 28 |
# File 'lib/jsonapi/include_directive.rb', line 25 def key?(key) @hash.key?(key.to_sym) || ([:allow_wildcard] && (@hash.key?(:*) || @hash.key?(:**))) end |
#keys ⇒ Array<Symbol>
31 32 33 |
# File 'lib/jsonapi/include_directive.rb', line 31 def keys @hash.keys end |
#to_hash ⇒ Hash{Symbol => Hash}
49 50 51 52 53 |
# File 'lib/jsonapi/include_directive.rb', line 49 def to_hash @hash.each_with_object({}) do |(key, value), hash| hash[key] = value.to_hash end end |
#to_string ⇒ String
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/jsonapi/include_directive.rb', line 56 def to_string string_array = @hash.map do |(key, value)| string_value = value.to_string if string_value == '' key.to_s else string_value .split(',') .map { |x| key.to_s + '.' + x } .join(',') end end string_array.join(',') end |