Class: Simplifyapi::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/simplifyapi/collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, representer, &block) ⇒ Collection

Returns a new instance of Collection.



3
4
5
6
7
8
# File 'lib/simplifyapi/collection.rb', line 3

def initialize name, representer, &block
  @name = name
  @representer = representer

  instance_eval &block if block_given?
end

Instance Method Details

#export(model_or_collection) ⇒ Object

Depending on how you use a collection, it can be invoked with an array or with a model if you’re defining nested collections like a has_many relationship



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simplifyapi/collection.rb', line 18

def export model_or_collection
  if getter_defined?
    result = @getter.call model_or_collection
  else
    if model_or_collection.is_a? Array
      result = model_or_collection
    else
      result = model_or_collection.send(@name)
    end
  end

  wrap_representers result
end

#getter_defined?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/simplifyapi/collection.rb', line 38

def getter_defined?
  !!@getter
end

#importObject



10
11
12
# File 'lib/simplifyapi/collection.rb', line 10

def import
  raise "TODO"
end

#setter_defined?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/simplifyapi/collection.rb', line 42

def setter_defined?
  !!@setter
end

#wrap_representers(collection) ⇒ Object



32
33
34
35
36
# File 'lib/simplifyapi/collection.rb', line 32

def wrap_representers collection
  collection.map do |item|
    @representer.export(item)
  end
end