-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add date and datetime conversions #56
base: main
Are you sure you want to change the base?
Changes from all commits
129f611
6ad7605
d977c2a
2337bb4
888e707
da79924
71e0542
e0e851c
46f519f
ebf3bc7
eff44a2
e88f514
ee92aa7
4a52e04
570a74a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module Fulfil | ||
# The Fulfil::Conversions module provides utility methods for converting | ||
# Date and DateTime objects into a standardized hash format. | ||
class Converter | ||
class << self | ||
def date_or_datetime_as_object(date_or_datetime) | ||
case date_or_datetime | ||
when Date | ||
date_as_object(date_or_datetime) | ||
when DateTime | ||
datetime_as_object(date_or_datetime) | ||
end | ||
end | ||
|
||
def datetime_as_object(datetime) | ||
{ | ||
__class__: 'datetime', | ||
iso_string: datetime.new_offset(0).iso8601 | ||
} | ||
end | ||
|
||
def date_as_object(date) | ||
datetime_as_object(date.to_datetime) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'fulfil/converter' | ||
|
||
module Fulfil | ||
# The Fulfil::DomainParser module provides utility methods for converting | ||
# Date and DateTime objects into a standardized hash format. | ||
# The module iterates over given parameters, identifies | ||
# Date and DateTime objects, and converts them into a hash with a class descriptor | ||
# and an ISO 8601 formatted string. | ||
class DomainParser | ||
attr_reader :domain | ||
|
||
def initialize(domain) | ||
@domain = domain | ||
|
||
disable_escape_html_entities | ||
end | ||
|
||
def parsed | ||
new_domain = domain.map do |values| | ||
update_values(values) | ||
end | ||
|
||
enable_escape_html_entities | ||
new_domain | ||
end | ||
|
||
def update_values(values) | ||
values.map do |value| | ||
case value.class.name | ||
when 'Date' | ||
date_as_object(value) | ||
when 'DateTime' | ||
datetime_as_object(value) | ||
else | ||
value | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def date_as_object(date) | ||
Converter.date_as_object(date) | ||
end | ||
|
||
def datetime_as_object(datetime) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fulfil::DomainParser#datetime_as_object doesn't depend on instance state (maybe move it to another class?) |
||
Converter.datetime_as_object(datetime) | ||
end | ||
|
||
def disable_escape_html_entities | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fulfil::DomainParser#disable_escape_html_entities doesn't depend on instance state (maybe move it to another class?) |
||
return unless defined?(ActiveSupport) && ActiveSupport.respond_to?(:escape_html_entities_in_json=) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fulfil::DomainParser#disable_escape_html_entities manually dispatches method call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cdmwebs |
||
|
||
ActiveSupport.escape_html_entities_in_json = false | ||
end | ||
|
||
def enable_escape_html_entities | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fulfil::DomainParser#enable_escape_html_entities doesn't depend on instance state (maybe move it to another class?) |
||
return unless defined?(ActiveSupport) && ActiveSupport.respond_to?(:escape_html_entities_in_json=) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fulfil::DomainParser#enable_escape_html_entities manually dispatches method call |
||
|
||
ActiveSupport.escape_html_entities_in_json = true | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ def search( | |
offset: nil, | ||
sort: nil | ||
) | ||
|
||
@client.search( | ||
model: model, | ||
domain: domain, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'date' | ||
require 'fulfil/converter' | ||
|
||
module Fulfil | ||
class Query | ||
def initialize | ||
|
@@ -86,11 +89,25 @@ def build_search_term(field:, value:, options:, prefix: nil) | |
[[key, 'ilike', value]] | ||
end | ||
when 'Hash' | ||
handle_hash(field, key, value, options) | ||
when 'Date', 'DateTime' | ||
[[key, '=', Converter.date_or_datetime_as_object(value)]] | ||
else | ||
raise "Unhandled value type: #{value} (#{value.class.name})" | ||
end | ||
end | ||
|
||
def handle_hash(field, key, value, options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fulfil::Query#handle_hash has 4 parameters There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fulfil::Query#handle_hash has approx 7 statements |
||
if %i[gte gt lte lt].any? { |op| value.key?(op) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fulfil::Query#handle_hash refers to 'value' more than self (maybe move it to another class?) |
||
value.map do |operator, val| | ||
op_map = { gte: '>=', gt: '>', lte: '<=', lt: '<' } | ||
converted_value = Converter.date_or_datetime_as_object(val) | ||
[key, op_map[operator], converted_value] | ||
end | ||
else | ||
value.flat_map do |nested_field, nested_value| | ||
build_search_term(prefix: field, field: nested_field, value: nested_value, options: options) | ||
end | ||
else | ||
raise "Unhandled value type: #{value} (#{value.class.name})" | ||
end | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fulfil::DomainParser#date_as_object doesn't depend on instance state (maybe move it to another class?)