Last active
June 11, 2020 19:10
-
-
Save jayshrivastava/787c7de55104548ae82fb20bfb362bb7 to your computer and use it in GitHub Desktop.
Shopify Engineering Blog Post Code Snippets
This file contains hidden or 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
| def action | |
| result, errors = foo() | |
| return nil unless errors.empty? | |
| result | |
| end | |
| action.value.to_h |
This file contains hidden or 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
| sig { returns(T.nilable(Result)) } | |
| def action | |
| result, errors = foo() | |
| return nil unless errors.empty? | |
| result | |
| end | |
| action.value.to_h | |
This file contains hidden or 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
| ad = { | |
| :id => 1, | |
| :name => "Email Marketer", | |
| :state => "Running", | |
| :keywords => nil, | |
| :start_date => date, | |
| :end_date => nil, | |
| :score, 1.5 | |
| } |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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