Class: JsonApi::Includes

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_ruby/includes.rb

Overview

Builds a traversible representation of the included resources

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#includesObject



38
39
40
# File 'lib/json_api_ruby/includes.rb', line 38

def includes
  @includes || []
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/json_api_ruby/includes.rb', line 4

def name
  @name
end

#nextObject



42
43
44
# File 'lib/json_api_ruby/includes.rb', line 42

def next
  @next || self.class.new
end

Class Method Details

.parse_includes(includes) ⇒ Object

Returns:

#<Includes>


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/json_api_ruby/includes.rb', line 10

def self.parse_includes(includes)
  first_include = self.new

  split_includes = Array(includes).map{|inc| inc.to_s.split('.')}
  if split_includes.blank?
    return first_include
  end

  first_array = Array(split_includes.first)
  other_arrays = split_includes[1..-1]
  zipped = first_array.zip(*other_arrays)
  first_array = zipped.shift
  first_include.includes = first_array.uniq.compact

  zipped.inject(first_include) do |base_include, object_name|
    new_include = self.new
    new_include.includes = object_name.uniq.compact
    base_include.next = new_include
    new_include
  end

  first_include
end

Instance Method Details

#has_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/json_api_ruby/includes.rb', line 34

def has_name?(name)
  includes.include?(name)
end