Class: Wfirma::Drivers::Fake::Catalogue

Inherits:
Object
  • Object
show all
Includes:
Wfirma::DeepDup
Defined in:
lib/wfirma/drivers/fake/catalogue.rb

Overview

In-memory stand-in for wFirma's contractor catalogue, so the find -> add/edit flow behaves in dev the way it does against the real CRM: the same customer resolves to one record across invoices.

Instance Method Summary collapse

Methods included from Wfirma::DeepDup

deep_dup

Constructor Details

#initializeCatalogue

Returns a new instance of Catalogue.



10
11
12
13
# File 'lib/wfirma/drivers/fake/catalogue.rb', line 10

def initialize
  @records = {}
  @next_id = 0
end

Instance Method Details

#add(attrs) ⇒ Object



26
27
28
29
30
# File 'lib/wfirma/drivers/fake/catalogue.rb', line 26

def add(attrs)
  id = (@next_id += 1)
  @records[id] = deep_dup(attrs).merge("id" => id.to_s)
  deep_dup(@records[id])
end

#edit(id, attrs) ⇒ Object

Merges in only the fields given, the way contractors/edit does. Returns nil when there is no such record.



34
35
36
37
38
39
40
# File 'lib/wfirma/drivers/fake/catalogue.rb', line 34

def edit(id, attrs)
  record = @records[id.to_i]
  return nil unless record

  record.merge!(deep_dup(attrs))
  deep_dup(record)
end

#find(condition) ⇒ Object

Answers the single equality condition Contractors#find_by sends, in wFirma's numeric-string-keyed shape.



17
18
19
20
21
22
23
24
# File 'lib/wfirma/drivers/fake/catalogue.rb', line 17

def find(condition)
  field = condition["field"].to_s
  value = condition["value"].to_s
  matches = @records.values.select { |record| record[field].to_s == value }
  matches.each_with_index.to_h do |record, index|
    [index.to_s, { "contractor" => deep_dup(record) }]
  end
end