Class: Fulfil::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/fulfil/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, model_name:) ⇒ Model

Returns a new instance of Model.



7
8
9
10
11
# File 'lib/fulfil/model.rb', line 7

def initialize(client:, model_name:)
  @client = client
  @model_name = model_name
  @query ||= Fulfil::Query.new
end

Instance Attribute Details

#model_nameObject (readonly)

Returns the value of attribute model_name.



5
6
7
# File 'lib/fulfil/model.rb', line 5

def model_name
  @model_name
end

Instance Method Details

#allObject



43
44
45
# File 'lib/fulfil/model.rb', line 43

def all
  search(domain: query)
end

#attributesObject



52
53
54
55
# File 'lib/fulfil/model.rb', line 52

def attributes
  results = @client.search(model: model_name, domain: [], limit: 1)
  @client.find(model: model_name, id: results.first.dig('id'))
end

#count(domain:) ⇒ Object



39
40
41
# File 'lib/fulfil/model.rb', line 39

def count(domain:)
  @client.count(model: model_name, domain: domain)
end

#fetch_associated(models:, association_name:, source_key:, fields:) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fulfil/model.rb', line 57

def fetch_associated(models:, association_name:, source_key:, fields:)
  source_keys = source_key.split('.')
  associated_ids =
    models.map { |model| model.dig(*source_keys) }.flatten.compact.uniq

  return [] if associated_ids.none?

  associated_models =
    @client.find(
      model: association_name, ids: associated_ids, fields: fields
    )

  associated_models_by_id = associated_models.map { |m| [m['id'], m] }.to_h

  models.each do |model|
    filtered_models =
      model.dig(*source_keys).map { |id| associated_models_by_id[id] }

    if source_keys.length > 1
      model.dig(*source_keys[0..-2]).store(
        source_keys.last,
        filtered_models
      )
    else
      model[source_keys.first] = filtered_models
    end
  end
end

#find(model: model_name, id:) ⇒ Object

Delegate this to the client, including the model_name so we don’t have to type it every time.



15
16
17
# File 'lib/fulfil/model.rb', line 15

def find(model: model_name, id:)
  @client.find(model: model, id: id)
end

#query(**args) ⇒ Object



47
48
49
50
# File 'lib/fulfil/model.rb', line 47

def query(**args)
  @query.search(**args).query if args.any?
  @query.query
end

#search(model: model_name, domain:, fields: %w[id rec_name], limit: nil, offset: nil, sort: nil) ⇒ Object

Delegate this to the client, including the model_name so we don’t have to type it every time.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fulfil/model.rb', line 21

def search(
  model: model_name,
  domain:,
  fields: %w[id rec_name],
  limit: nil,
  offset: nil,
  sort: nil
)
  @client.search(
    model: model,
    domain: domain,
    fields: fields,
    limit: limit,
    offset: offset,
    sort: sort
  )
end