Class: ApiMaker::RelationshipPreloader

Inherits:
Object
  • Object
show all
Defined in:
lib/api_maker/relationship_preloader.rb

Class Method Summary collapse

Class Method Details

.parse(preload_param) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/api_maker/relationship_preloader.rb', line 2

def self.parse(preload_param)
  if preload_param.nil?
    nil
  elsif preload_param.is_a?(String)
    ApiMaker::RelationshipPreloader.parse_string(preload_param)
  elsif preload_param.is_a?(Array)
    ApiMaker::RelationshipPreloader.parse_array(preload_param)
  else
    raise "Unexpected parameter given (#{preload_param.class.name}): #{preload_param}"
  end
end

.parse_array(preload_param) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/api_maker/relationship_preloader.rb', line 21

def self.parse_array(preload_param)
  result = {}
  preload_param.each do |preload_param_i|
    parsed = ApiMaker::RelationshipPreloader.parse(preload_param_i)
    parsed.each do |key, value|
      if result.key?(key)
        if result[key].is_a?(String)
          result[key] = [result[key], value]
        elsif result[key].is_a?(Array)
          result[key] << value
        else
          raise "Unknown object: #{result[key].class.name}"
        end
      else
        result[key] = value
      end
    end
  end

  result
end

.parse_string(preload_param) ⇒ Object



14
15
16
17
18
19
# File 'lib/api_maker/relationship_preloader.rb', line 14

def self.parse_string(preload_param)
  splitted = preload_param.split(".")
  initial = splitted.shift
  rest = splitted.join(".")
  {initial => rest}
end