Class: ErpIntegration::Fulfil::WhereClause

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

Overview

The ‘WhereClause` model encapsulates the logic for the filter options for Fulfil. It transforms the attributes passed to any of the where lookup methods into a format that Fulfil can actually understand.

For more information, see their documentation. developers.fulfil.io/guides/searching-filtering/#field-filter-expressions

Examples:

WhereClause.new({ key: :id, value: 100, comparison_operator: 'ilike' })
# => <WhereClause @key="id" value="100" comparison_operator="ilike" />
$ WhereClause.new({ key: :id, value: 100 })
# => <WhereClause @key="id" value="100" comparison_operator="=" />

Constant Summary collapse

COMPARISON_OPERATORS =
[
  '=', '!=', '<', '<=', '>=', '>', 'like', 'ilike', 'in', 'not in'
].freeze
DEFAULT_COMPARISON_OPERATOR =
'='

Instance Method Summary collapse

Constructor Details

#initialize(key:, value:, domain: nil, comparison_operator: DEFAULT_COMPARISON_OPERATOR) ⇒ WhereClause

Returns a new instance of WhereClause.

Parameters:

  • key (String)

    The name of filtered attribute.

  • value (String)

    The value by filtering will be performed.

  • domain (String) (defaults to: nil)

    The domain model name to specify filtering attributes location.

  • comparison_operator (String) (defaults to: DEFAULT_COMPARISON_OPERATOR)

    The way the key and the value are compared.



33
34
35
36
37
38
# File 'lib/erp_integration/fulfil/where_clause.rb', line 33

def initialize(key:, value:, domain: nil, comparison_operator: DEFAULT_COMPARISON_OPERATOR)
  @comparison_operator = verify_comparison_operator(comparison_operator)
  @key = key.to_s
  @value = to_extended_query_value(value)
  @domain = domain
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

The ‘===` allows comparing different WhereClause objects. Under the hood, this is used by `.uniq` to determine whether objects are equal to each other.

Note: ‘.uniq` also depends on `.hash` on the `WhereClause` instance. See the `.hash` method to see how the two objects are compared.

Examples:

$ WhereClause.new(id: 100) == WhereClause.new(id: 100)
# => true

$ WhereClause.new(id: 100) == WhereClause.new(id: 101)
# => false

$ WhereClause.new(id: 100) == WhereClause.new(name: "PT100")
# => false

Parameters:

  • other (WhereClause)

    Another ‘WhereClause` instance.

Returns:

  • (Boolean)

    Whether or not the ‘WhereClause`s are equal.



64
65
66
# File 'lib/erp_integration/fulfil/where_clause.rb', line 64

def ==(other)
  to_filter == other.to_filter
end

#hashFixnum

The ‘.hash` allows comparing two `WhereClause` instances for uniqueness. See rubyapi.org/2.3/o/object#method-i-hash

Returns:

  • (Fixnum)

    A Fixnum that identifies the ‘WhereClause`.



72
73
74
# File 'lib/erp_integration/fulfil/where_clause.rb', line 72

def hash
  to_filter.hash
end

#to_filterArray<String>

Transforms the ‘WhereClause` into a filter object for Fulfil.

Returns:

  • (Array<String>)

    The formatted filter for Fulfil.



42
43
44
# File 'lib/erp_integration/fulfil/where_clause.rb', line 42

def to_filter
  [@key, @comparison_operator, @value, @domain].compact
end