Class: Hat::RelationIncludes

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Comparable
Defined in:
lib/hat/relation_includes.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*includes) ⇒ RelationIncludes

Returns a new instance of RelationIncludes.



14
15
16
17
# File 'lib/hat/relation_includes.rb', line 14

def initialize(*includes)
  @includes = includes.compact
  super(@includes)
end

Class Method Details

.from_params(params) ⇒ Object



19
20
21
# File 'lib/hat/relation_includes.rb', line 19

def self.from_params(params)
  from_string(params[:include])
end

.from_string(string) ⇒ Object



23
24
25
26
27
28
# File 'lib/hat/relation_includes.rb', line 23

def self.from_string(string)
  return new if string.blank?

  includes_hash = build_hash_from_string(string)
  new(*flatten(includes_hash))
end

Instance Method Details

#&(other_includes) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/hat/relation_includes.rb', line 45

def &(other_includes)
  hash = self.class.build_hash_from_string(to_s)
  other_hash = self.class.build_hash_from_string(other_includes.to_s)

  intersected_paths = (to_s.split(',') & other_includes.to_s.split(','))
  self.class.from_string(intersected_paths.join(','))
end

#<=>(other_includes) ⇒ Object



53
54
55
# File 'lib/hat/relation_includes.rb', line 53

def <=>(other_includes)
  to_s <=> other_includes.to_s
end

#find(attr_name) ⇒ Object



66
67
68
69
70
71
# File 'lib/hat/relation_includes.rb', line 66

def find(attr_name)
  includes.find do |relation|
    (relation.kind_of?(Symbol) && attr_name == relation) ||
      (relation.kind_of?(Hash) && relation.keys.include?(attr_name))
  end
end

#for_query(serializer_class) ⇒ Object

Return an expanded version of the includes for use in a query. This api is still pretty rough and likely to change



82
83
84
# File 'lib/hat/relation_includes.rb', line 82

def for_query(serializer_class)
  RelationIncludes.new(*ExpandedRelationIncludes.new(self, serializer_class))
end

#include(additional_includes) ⇒ Object



57
58
59
60
# File 'lib/hat/relation_includes.rb', line 57

def include(additional_includes)
  includes.concat(additional_includes)
  self
end

#includes_relation?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/hat/relation_includes.rb', line 62

def includes_relation?(attr_name)
  find(attr_name).present?
end

#nested_includes_for(attr_name) ⇒ Object



73
74
75
76
77
78
# File 'lib/hat/relation_includes.rb', line 73

def nested_includes_for(attr_name)
  nested = find(attr_name)
  if nested.kind_of?(Hash)
    nested[attr_name]
  end
end

#to_sObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hat/relation_includes.rb', line 30

def to_s
  @to_s ||= begin
    paths = []
    includes.each do |item|
      if item.is_a? Hash
        stringify_keys(paths, item)
      else
        paths << [item]
      end
    end
    paths = paths.map { |p| p.join('.') }
    paths.sort.join(',')
  end
end