Class: RememberTheRuby::EntityList

Inherits:
Array
  • Object
show all
Defined in:
lib/remember-the-ruby/entity_list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport, type) ⇒ EntityList

Returns a new instance of EntityList.



6
7
8
9
10
# File 'lib/remember-the-ruby/entity_list.rb', line 6

def initialize(transport, type)
  @transport = transport
  @type      = type
  @defaults  = {}
end

Instance Attribute Details

#defaultsObject

Returns the value of attribute defaults.



4
5
6
# File 'lib/remember-the-ruby/entity_list.rb', line 4

def defaults
  @defaults
end

Class Method Details

.from_element(transport, type, rsp, xpath) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/remember-the-ruby/entity_list.rb', line 49

def self.from_element(transport, type, rsp, xpath)
  entity_list = EntityList.new(transport, type)
  rsp.get_elements(xpath).map do |element|
    entity_list << type.from_element(transport, element)
  end
  entity_list
end

Instance Method Details

#find(options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/remember-the-ruby/entity_list.rb', line 20

def find(options)

  case options
    when String, Integer then 
      params = { 'id' => options.to_s }
      amount = :one
    when Hash then 
      params = options
      amount = :many
    else 
      params = {}
      amount = :many
  end
  
  matches = EntityList.new(@transport, @type)
  self.each do |entity|
    matched = true
    params.each do |key, value|
      unless entity[key] == value
        matched = false
        break
      end
    end
    matches << entity if matched
  end
  
  amount == :many ? matches : matches.first
end

#newObject



12
13
14
15
16
17
18
# File 'lib/remember-the-ruby/entity_list.rb', line 12

def new
  entity = @type.new(@transport)
  @defaults.each do |key, value|
    entity[key] = value
  end
  entity
end