-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow a model to be converted into a hash or a json string (#5)
* Allow models to be converted to a hash or JSON string This is mostly useful when processing messages on a Transaction Notification Service. This gem tries to minimise the shock value of the EML APIs and return sensible values but it's likely that the result set will need to be added to a database so a mechanism is required to convert back from custom objects to something that can be stored * 👮
- Loading branch information
1 parent
9f847b8
commit adb7359
Showing
32 changed files
with
381 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.env | ||
coverage | ||
Gemfile.lock | ||
pkg | ||
spec/examples.txt | ||
spec/vcr_cassettes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task :default => :spec | ||
task default: :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module EML | ||
class ModelHash | ||
extend T::Sig | ||
|
||
sig { params(model: EML::Model).returns(T::Hash[Symbol, T.untyped]) } | ||
def self.call(model) | ||
new(model).call | ||
end | ||
|
||
sig { params(model: EML::Model).void } | ||
def initialize(model) | ||
@model = T.let(model, EML::Model) | ||
@hash = T.let({}, T::Hash[Symbol, T.untyped]) | ||
end | ||
|
||
sig { returns(T::Hash[Symbol, T.untyped]) } | ||
def call | ||
@model.class.enumerate_fields do |_, local_name| | ||
value = @model.public_send(local_name) | ||
add_value(local_name, value) | ||
end | ||
|
||
@hash | ||
end | ||
|
||
private | ||
|
||
sig { params(name: Symbol, value: T.untyped).void } | ||
def add_value(name, value) | ||
@hash[name] = stored_value(value) | ||
end | ||
|
||
sig { params(value: T.untyped).returns(T.untyped) } | ||
def stored_value(value) | ||
if value.is_a?(Array) | ||
array_value(value) | ||
elsif value.respond_to?(:to_h) | ||
value.to_h | ||
else | ||
value | ||
end | ||
end | ||
|
||
sig { params(value: T::Array[T.untyped]).returns(T::Array[T.untyped]) } | ||
def array_value(value) | ||
value.each_with_object([]) do |item, array| | ||
array << stored_value(item) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.