Class: Graphiti::ActiveGraph::JsonapiExt::IncludeDirective

Inherits:
JSONAPI::IncludeDirective
  • Object
show all
Defined in:
lib/graphiti/active_graph/jsonapi_ext/include_directive.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(include_args, options = {}) ⇒ IncludeDirective



5
6
7
8
9
10
# File 'lib/graphiti/active_graph/jsonapi_ext/include_directive.rb', line 5

def initialize(include_args, options = {})
  include_hash = JSONAPI::IncludeDirective::Parser.parse_include_args(include_args)
  @retain_rel_limit = options.delete(:retain_rel_limit)
  @hash = formulate_hash(include_hash, options)
  @options = options
end

Instance Attribute Details

#lengthObject

Returns the value of attribute length.



3
4
5
# File 'lib/graphiti/active_graph/jsonapi_ext/include_directive.rb', line 3

def length
  @length
end

#retain_rel_limitObject

Returns the value of attribute retain_rel_limit.



3
4
5
# File 'lib/graphiti/active_graph/jsonapi_ext/include_directive.rb', line 3

def retain_rel_limit
  @retain_rel_limit
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
# File 'lib/graphiti/active_graph/jsonapi_ext/include_directive.rb', line 22

def [](key)
  super&.descend(key) || {}
end

#add_self_reference(key, length) ⇒ Object



41
42
43
44
# File 'lib/graphiti/active_graph/jsonapi_ext/include_directive.rb', line 41

def add_self_reference(key, length)
  @hash = @hash.merge(key => self)
  self.length = length
end

#descend(key) ⇒ Object



26
27
28
# File 'lib/graphiti/active_graph/jsonapi_ext/include_directive.rb', line 26

def descend(key)
  length && length != '' ? dup.tap { |dup| dup.add_self_reference(key, length.to_i - 1) } : self
end

#getObject



16
# File 'lib/graphiti/active_graph/jsonapi_ext/include_directive.rb', line 16

alias get []

#key?(key) ⇒ Boolean



18
19
20
# File 'lib/graphiti/active_graph/jsonapi_ext/include_directive.rb', line 18

def key?(key)
  super && get(key).valid_length?
end

#keysObject



12
13
14
# File 'lib/graphiti/active_graph/jsonapi_ext/include_directive.rb', line 12

def keys
  super.select(&method(:key?))
end

#to_hashObject



34
35
36
37
38
39
# File 'lib/graphiti/active_graph/jsonapi_ext/include_directive.rb', line 34

def to_hash
  @hash.each_with_object({}) do |(key, value), hash|
    key = "#{key}*#{value.length}".to_sym if value.length
    hash[key] = value.to_hash unless value == self
  end
end

#valid_length?Boolean



30
31
32
# File 'lib/graphiti/active_graph/jsonapi_ext/include_directive.rb', line 30

def valid_length?
  length.nil? || length == '' || length.to_i.positive?
end