Class: Coach4rb::Resource::Page

Inherits:
Object
  • Object
show all
Includes:
Mixin::AsHash, Mixin::AutoConstructor, Mixin::Iterable, Enumerable
Defined in:
lib/coach4rb/resource/page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::AsHash

included

Methods included from Mixin::Iterable

included

Methods included from Mixin::AutoConstructor

included

Instance Attribute Details

#availableObject

Returns the value of attribute available.



12
13
14
# File 'lib/coach4rb/resource/page.rb', line 12

def available
  @available
end

#endObject

Returns the value of attribute end.



12
13
14
# File 'lib/coach4rb/resource/page.rb', line 12

def end
  @end
end

Returns the value of attribute links.



12
13
14
# File 'lib/coach4rb/resource/page.rb', line 12

def links
  @links
end

#startObject

Returns the value of attribute start.



12
13
14
# File 'lib/coach4rb/resource/page.rb', line 12

def start
  @start
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/coach4rb/resource/page.rb', line 12

def type
  @type
end

#uriObject

Returns the value of attribute uri.



12
13
14
# File 'lib/coach4rb/resource/page.rb', line 12

def uri
  @uri
end

Class Method Details

.from_coach(a_hash, entity_class = Entity) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/coach4rb/resource/page.rb', line 14

def self.from_coach(a_hash, entity_class=Entity)
  new_hash = a_hash.dup
  type_plural = pluralize(new_hash[:type]) if new_hash[:type]
  type_plural ||= false
  new_hash[:links] ||= []
  new_hash[:type] = type_plural
  new_hash[:entity_class] ||= entity_class
  self.new new_hash
end

Instance Method Details

#[](key) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/coach4rb/resource/page.rb', line 41

def [](key)
  case key
    when Integer
      entities[key]
    when Symbol
      super(key)
    else
      raise 'Error: param not supported!'
  end
end

#each(&block) ⇒ Object



53
54
55
56
57
# File 'lib/coach4rb/resource/page.rb', line 53

def each(&block)
  entities.each do |item|
    block.call item
  end
end

#entitiesObject Also known as: items



25
26
27
28
29
30
31
32
# File 'lib/coach4rb/resource/page.rb', line 25

def entities
  if @type && @entities.nil?
    hashes = instance_variable_get("@#{@type}")
    @entities = hashes.map { |a_hash| @entity_class.from_coach a_hash }
  else
    @entities
  end
end

#sizeObject



36
37
38
# File 'lib/coach4rb/resource/page.rb', line 36

def size
  entities.size
end

#to_aObject



59
60
61
# File 'lib/coach4rb/resource/page.rb', line 59

def to_a
  entities
end