Class: NaranyaEcm::Rest::Relation

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/naranya_ecm/rest/relation.rb

Direct Known Subclasses

AssociationRelation, Search::Results

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(given_klass, given_conditions = {}) ⇒ Relation

Returns a new instance of Relation.



10
11
12
# File 'lib/naranya_ecm/rest/relation.rb', line 10

def initialize(given_klass, given_conditions = {})
  @klass, @conditions = given_klass, given_conditions.with_indifferent_access
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



8
9
10
# File 'lib/naranya_ecm/rest/relation.rb', line 8

def conditions
  @conditions
end

#elementsObject (readonly)

Returns the value of attribute elements.



8
9
10
# File 'lib/naranya_ecm/rest/relation.rb', line 8

def elements
  @elements
end

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/naranya_ecm/rest/relation.rb', line 8

def klass
  @klass
end

Instance Method Details

#[](n) ⇒ Object



14
15
16
# File 'lib/naranya_ecm/rest/relation.rb', line 14

def [](n)
  to_a[n]
end

#fetch_from_serverObject



31
32
33
34
35
# File 'lib/naranya_ecm/rest/relation.rb', line 31

def fetch_from_server
  NContent::SDK::RESTClient.get do |req|
    req.url "#{klass.path}.json", conditions
  end.body
end

#limit(given_limit) ⇒ Object



22
23
24
# File 'lib/naranya_ecm/rest/relation.rb', line 22

def limit(given_limit)
  spawn_chain(limit: given_limit.to_i)
end

#load(given_list) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/naranya_ecm/rest/relation.rb', line 37

def load(given_list)
  # Revisar que todos los elementos sean Hash o la clase asociada:
  if given_list.count > 0 && given_list.map{ |e| e.is_a?(Hash) || e.is_a?(@klass) }.reduce { |res, e| res && e } == false
    raise "Type Mismatch: some elements are not a hash nor associated_class"
  end

  @elements = given_list.map do |element|
    if element.is_a?(Hash)
      loaded_element = @klass.send :_load_from_fetched_attributes, element
      loaded_element
    else
      element
    end
  end
end

#to_aObject



26
27
28
29
# File 'lib/naranya_ecm/rest/relation.rb', line 26

def to_a
  materialize! if @elements.nil?
  elements
end

#where(given_conditions = {}) ⇒ Object



18
19
20
# File 'lib/naranya_ecm/rest/relation.rb', line 18

def where(given_conditions = {})
  spawn_chain(given_conditions)
end