Module: ErpIntegration::Fulfil::QueryMethods

Included in:
ApiResource
Defined in:
lib/erp_integration/fulfil/query_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#or_clausesObject

Returns the value of attribute or_clauses.



10
11
12
# File 'lib/erp_integration/fulfil/query_methods.rb', line 10

def or_clauses
  @or_clauses
end

#selected_fieldsObject

Returns the value of attribute selected_fields.



10
11
12
# File 'lib/erp_integration/fulfil/query_methods.rb', line 10

def selected_fields
  @selected_fields
end

#where_clausesObject

Returns the value of attribute where_clauses.



10
11
12
# File 'lib/erp_integration/fulfil/query_methods.rb', line 10

def where_clauses
  @where_clauses
end

Instance Method Details

#or(*args) ⇒ Object

The ‘or` method introduces an interface to query resources in Fulfil Is quite similar to the `where` method but with one big difference. The `or` method allows us to use the OR operator with several conditions

Examples:

$ ErpIntegration::SalesOrder.or(id: 100, carrier: 12).all
# => <ErpIntegration::Fulfil::Collection @items=[
       <ErpIntegration::SalesOrder:0x00007f8577cb6d40 @carrier=12, @id=3179663 />
       <ErpIntegration::SalesOrder:0x00007f8577cb6d40 @carrier=16, @id=100 /> ] />
$ ErpIntegration::SalesOrder.or(id: [100, 200]).all
# => <ErpIntegration::Fulfil::Collection @items=[
       <ErpIntegration::SalesOrder:0x00007f8577cb6d40 @id=100 />
       <ErpIntegration::SalesOrder:0x00007f8577cb6d40 @id=200 /> ] />

Now we are just accepting the last 'or' method so if we have multiple
we are only accepting the last one.


105
106
107
# File 'lib/erp_integration/fulfil/query_methods.rb', line 105

def or(*args)
  clone.or!(*args)
end

#or!(args) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/erp_integration/fulfil/query_methods.rb', line 109

def or!(args)
  where_clauses = []
  args.each_pair do |key, value_or_values|
    [*value_or_values].each do |value|
      where_clauses << WhereClause.new(
        key: key, value: value
      )
    end
  end

  self.or_clauses = [
    OrClause.new(
      where_clauses: where_clauses
    )
  ]

  self
end

#select(*fields) ⇒ Object

The ‘QueryMethods#select` works in two unique ways

First, it takes a block to allow it to function like a regular ‘Enumerable#select`.

Secondly, it allows us to select specific fields to be returned by Fulfil.

Fulfil only returns an ID by default when one would query their API. However, the ID is seldom the only value one will need. The ‘select` method allows selecting multiple fields at once.

When one calls the ‘all` method, it will fetch all the resources from Fulfil and it will return all the given fields (if available).

Both a list of Strings, Symbols or a combination of these can be passed to the ‘select` method.

Examples:

$ ErpIntegration::SalesOrder.select { |sales_order| sales_order.id > 100  }
# => [<ErpIntegration::SalesOrder @id=101 />, <ErpIntegration::Resource @id=102 />]
$ ErpIntegration::SalesOrder.select(:id, :name, 'product.name')
# => <ErpIntegration::Fulfil::Resources::SalesOrder @selected_fields=[:id, :name, "product.name"] />
$ ErpIntegration::SalesOrder.select(:id, :name, 'product.name').all
# => [<ErpIntegration::SalesOrder @id=101 />, <ErpIntegration::Resource @id=102 />]


39
40
41
42
43
44
45
46
47
# File 'lib/erp_integration/fulfil/query_methods.rb', line 39

def select(*fields)
  if block_given?
    raise ArgumentError, "'select' with block doesn't take arguments." if fields.any?

    return super()
  end

  clone.select!(*fields)
end

#select!(*fields) ⇒ Object



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

def select!(*fields)
  self.selected_fields = (selected_fields || []).concat(fields)
  self
end

#where(*args) ⇒ Object

Fulfil has a very powerful query syntax. However, that query syntax is rather complicated and it’s really specific to Fulfil.

The ‘where` method introduces a well-known interface (e.g. ActiveRecord::Relation) to query resources in Fulfil.

If one adds the ‘comparison_operator` key to the arguments, it will use that comparison operator to build the query to Fulfil.

All the other ‘where_` methods use this technique to provide logic for all the different comparison operators.

Examples:

$ ErpIntegration::SalesOrder.where(id: 100).all
# => <ErpIntegration::Fulfil::Collection @items=[<ErpIntegration::SalesOrder @id=100 />] />


69
70
71
# File 'lib/erp_integration/fulfil/query_methods.rb', line 69

def where(*args)
  clone.where!(*args)
end

#where!(args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/erp_integration/fulfil/query_methods.rb', line 73

def where!(args)
  domain = args.delete(:domain)
  comparison_operator = args.delete(:comparison_operator)

  args.each_pair do |key, value|
    self.where_clauses =
      (where_clauses || []) << WhereClause.new(
        key: key, value: value, domain: domain, comparison_operator: comparison_operator
      )
  end

  self
end

#where_domain(domain, args) ⇒ Object

The ‘where_domain` method provides an interface for querying resources in Fulfil when filtered attributes require specifying the name of the domain model in which they are located.

Adds the domain model name as a fourth argument to each filers parameter

Examples:

$ ErpIntegration::SalesOrder.where_domain('stock.shipment.out', 'shipment.state': 'done').all
# => <ErpIntegration::Fulfil::Collection @items=[<ErpIntegration::SalesOrder @id=100 />] />
:filters=>[
  ['shipment.state', '=', 'done', 'stock.shipment.out']
]


142
143
144
# File 'lib/erp_integration/fulfil/query_methods.rb', line 142

def where_domain(domain, args)
  where(args.merge!(domain: domain))
end

#where_ilike(args) ⇒ Object



146
147
148
# File 'lib/erp_integration/fulfil/query_methods.rb', line 146

def where_ilike(args)
  where(args.merge(comparison_operator: 'ilike'))
end

#where_in(args) ⇒ Object



150
151
152
# File 'lib/erp_integration/fulfil/query_methods.rb', line 150

def where_in(args)
  where(args.merge(comparison_operator: 'in'))
end

#where_less_or_equal_to(args) ⇒ Object



158
159
160
# File 'lib/erp_integration/fulfil/query_methods.rb', line 158

def where_less_or_equal_to(args)
  where(args.merge(comparison_operator: '<='))
end

#where_less_than(args) ⇒ Object



154
155
156
# File 'lib/erp_integration/fulfil/query_methods.rb', line 154

def where_less_than(args)
  where(args.merge(comparison_operator: '<'))
end

#where_like(args) ⇒ Object



162
163
164
# File 'lib/erp_integration/fulfil/query_methods.rb', line 162

def where_like(args)
  where(args.merge(comparison_operator: 'like'))
end

#where_more_or_equal_to(args) ⇒ Object



170
171
172
# File 'lib/erp_integration/fulfil/query_methods.rb', line 170

def where_more_or_equal_to(args)
  where(args.merge(comparison_operator: '>='))
end

#where_more_than(args) ⇒ Object



166
167
168
# File 'lib/erp_integration/fulfil/query_methods.rb', line 166

def where_more_than(args)
  where(args.merge(comparison_operator: '>'))
end

#where_not(args) ⇒ Object



174
175
176
# File 'lib/erp_integration/fulfil/query_methods.rb', line 174

def where_not(args)
  where(args.merge(comparison_operator: '!='))
end

#where_not_in(args) ⇒ Object



178
179
180
# File 'lib/erp_integration/fulfil/query_methods.rb', line 178

def where_not_in(args)
  where(args.merge(comparison_operator: 'not in'))
end