Module: HouseTest

Defined in:
lib/house_test.rb,
lib/house_test/version.rb,
lib/house_test/house_server.rb

Overview

Representing House objects in ‘backend’ (memory)

Constant Summary collapse

PORT =

Port for server to run at

14_000
VERSION =
'0.1.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.housesObject

Returns the value of attribute houses.



11
12
13
# File 'lib/house_test/house_server.rb', line 11

def houses
  @houses
end

Class Method Details

.add(house_params = {}) ⇒ String

Add a new house according to parameters passed

Returns:

  • (String)

    Id of house just created



15
16
17
18
19
# File 'lib/house_test/house_server.rb', line 15

def add(house_params = {})
  house = Backend::House.new(house_params)
  @houses << house
  house.id
end

.delete(id) ⇒ Integer

Delete house at id

Parameters:

  • id (Integer)

    Id of house to delete

Returns:

  • (Integer)

    Id of house deleted. Nil if not found



42
43
44
45
46
47
48
# File 'lib/house_test/house_server.rb', line 42

def delete(id)
  found_house = get id
  return nil unless found_house

  houses.delete(found_house)
  id
end

.descriptionsString

Returns Description of all houses.

Returns:

  • (String)

    Description of all houses



51
52
53
# File 'lib/house_test/house_server.rb', line 51

def descriptions
  houses.map(&:description)
end

.get(id) ⇒ House

Get an existing house

Parameters:

  • id (Integer)

    Id of house to get

Returns:

  • (House)

    House found. Nil if nothing found



24
25
26
# File 'lib/house_test/house_server.rb', line 24

def get(id)
  houses.find { |house| house.id == id }
end

.update(id, key, value) ⇒ Integer

Update house at id’s key to value

Parameters:

  • id (Integer)

    Id of house to update

Returns:

  • (Integer)

    Id of house updated



31
32
33
34
35
36
37
# File 'lib/house_test/house_server.rb', line 31

def update(id, key, value)
  found_house = get id
  return nil unless found_house

  found_house.send("#{key}=", value)
  id
end