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.



9
10
11
12
13
# File 'lib/fulfil/model.rb', line 9

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.



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

def model_name
  @model_name
end

Instance Method Details

#allObject



45
46
47
# File 'lib/fulfil/model.rb', line 45

def all
  search(domain: query)
end

#attributesObject



54
55
56
57
# File 'lib/fulfil/model.rb', line 54

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

#count(domain:) ⇒ Object



41
42
43
# File 'lib/fulfil/model.rb', line 41

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

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



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
85
86
# File 'lib/fulfil/model.rb', line 59

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.to_h { |m| [m['id'], m] }

  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(id:, model: model_name) ⇒ Object

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



17
18
19
# File 'lib/fulfil/model.rb', line 17

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

#query(**args) ⇒ Object



49
50
51
52
# File 'lib/fulfil/model.rb', line 49

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

#search(domain:, model: model_name, 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.



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

def search(
  domain:,
  model: model_name,
  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