Skip to content

Instantly share code, notes, and snippets.

View ShopifyEng's full-sized avatar

Shopify Engineering Communications ShopifyEng

View GitHub Profile
# ----------------------------- Gem -----------------------------
module PricingEngine
module PricingRepositoryInterface
extend T::Sig
extend T::Helpers
interface!
sig { abstract.params(ids: T::Array[Integer]).returns(T::Array[Schema::Variant]) }
def variants_by_ids(ids);end
# ----------------------------- Gem -----------------------------
module PricingEngine
class PricingRepositoryInterface
def variants_by_ids(ids)
raise NotImplemented
end
def variants_by_titles(titles)
raise NotImplemented
end
module PricingEngine
class Engine
# @param repository [PricingEngine::PricingRepositoryInterface] environment specific functions to
# access db and cache data.
#
# @return [void]
def initialize(repository)
@repository = repository
end
# Consumer 2 Logic
require 'pricing_engine'
module StorefrontRenderer
class PricingRepository < PricingEngine::PricingRepositoryInterface
def variants_by_ids(ids)
variants = DataStoreConnection.execute("SELECT title, price FROM variants WHERE id in (?)", ids).limit(50)
variants.map { |variant| PricingEngine::Schema::Variant.new(title: variant.title, price: variant.price) }
# Consumer 1 Logic
require 'pricing_engine'
module ShopifyCore
class PricingRepository < PricingEngine::PricingRepositoryInterface
def variants_by_ids(ids)
variants = ProductVariant.where(id: ids).select(:title, :price).limit(50)
variants.map { |variant| PricingEngine::Schema::Variant.new(title: variant.title, price: variant.price) }
end
module PricingEngine
class PricingRepositoryInterface
def variants_by_ids(ids)
raise NotImplemented
end
def variants_by_titles(titles)
raise NotImplemented
end
end
@ShopifyEng
ShopifyEng / example-pricing-gem.rb
Created March 18, 2021 12:43
Repository Pattern
# Gem
module PricingEngine
class Engine
#...
# Calculates the price for variants given the buyer context.
#
# @param context [Schema::BuyerContext] the buyer context
#
# @return [Array<PricingEngine::Schema::Price>] An array of prices for the context passed.
def calculate_prices_for_variants(context)
{
"payload": {
"op": "c",
"ts_ms": 1465491411815,
"before": null,
"after": {
"id": 1,
"first_name": "Peyton",
"last_name": "Manning",
"address1": "18 Willow St",
@ShopifyEng
ShopifyEng / Confetti.tsx
Created April 6, 2020 17:11
Building Arrive's Confetti in React Native with Reanimated - Confetti Final
import React, {useMemo} from 'react'
import Animated from 'react-native-reanimated'
import {View, Dimensions, StyleSheet} from 'react-native'
import FastImage from 'react-native-fast-image'
import ConfettiImage from 'assets/images/confetti.png'
const NUM_CONFETTI = 100
const COLORS = ['#00e4b2', '#09aec5', '#107ed5']
const CONFETTI_SIZE = 16
@ShopifyEng
ShopifyEng / profile.rb
Created March 30, 2020 20:05
Optimizing Ruby Lazy Initialization in TruffleRuby with Deoptimization - Code ||= Lazy Initialization Profiling
# When run on the following 20 repositories, it found 2082 ||= and it was used as lazy init 64.3% of the time
# Those numbers are fuzzy, as there is not certain way to prove if something was used as a lazy init
# This script only marks a usage as a lazy init if the variable is being set to a constant,
# in which case if it is not a lazy init then the developers have done something weird.
# It is also marked as a lazy init if the variable is an instance variable or a class variable (either @var or @@var)
# AND the lazy init is used in the top level of a function, AND the function name is contained by the variable or vice versa.
# These are almost certain to be lazy initialized because the assumed behaviour is to always call the method instead of the variable,
# and other usages would be both weird usage of instance/class variables and/or weird usage of the method naming
# This of course, misses a variety of cases but likely does not get any cases that do not lazy initialize.
# A better way to profile could be