Skip to content

Instantly share code, notes, and snippets.

@jayshrivastava
Last active June 11, 2020 19:10
Show Gist options
  • Save jayshrivastava/787c7de55104548ae82fb20bfb362bb7 to your computer and use it in GitHub Desktop.
Save jayshrivastava/787c7de55104548ae82fb20bfb362bb7 to your computer and use it in GitHub Desktop.
Shopify Engineering Blog Post Code Snippets
def action
result, errors = foo()
return nil unless errors.empty?
result
end
action.value.to_h
sig { returns(T.nilable(Result)) }
def action
result, errors = foo()
return nil unless errors.empty?
result
end
action.value.to_h
ad = {
:id => 1,
:name => "Email Marketer",
:state => "Running",
:keywords => nil,
:start_date => date,
:end_date => nil,
:score, 1.5
}
module Input
class Ad < T::Struct
const :name, String
const :state, State
const :keywords, T::Array[String]
const :start_date, Date
const :end_date, T.nilable(Date)
end
end
module Database
class Ad < T::Struct
const :ad, Input::Ad
const :id, Integer
const :score, Float
delegate :name, :state, :keywords, :start_date, :end_date, to: :ad
end
end
def example(keywords, async)
if async
Action.perform(SynchronousIndexer.new, keywords)
else
Action.perform(AsynchronousIndexer.new, keywords)
end
end
class Action
def self.perform(indexer, keywords)
result = indexer.index(keywords)
return nil unless result.errors.empty?
return result
end
end
module Indexer
extend T::Sig
extend T::Helpers
interface!
sig do
abstract.params(
keywords: T::Array[String]
).returns(Result, T::Array[String])
end
def index(keywords); end
end
class AsynchronousIndexer
extend T::Sig
include Indexer
sig do
override.params(
keywords: T::Array[String]
).returns(Result, T::Array[String])
end
def index(keywords)
foo(keywords)
end
end
class SynchronousIndexer
extend T::Sig
include Indexer
sig do
override.params(
keywords: T::Array[String]
).returns(Result, T::Array[String])
end
def index(keywords)
bar(keywords)
end
end
class Action
extend T::Sig
sig do
abstract.params(
indexer: Indexer,
keywords: T::Array[String]
).returns(T.nilable(Result))
end
def self.perform(indexer, keywords)
result, errors = indexer.index(keywords)
return nil unless result.errors.empty?
return result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment