Skip to content

Instantly share code, notes, and snippets.

View ismasan's full-sized avatar

Ismael Celis ismasan

View GitHub Profile

As I see it, there's a few options, each with its own tradeoffs.

1. Compute heavy operation on write

Run the expensive query/calculation when you write relevant data, synchronously, and store it in a way that's optimized for fast reads (ie. in a cache somewhere, or just a JSON blob in the database).

PROS:

  • optimized for fast reads, data is kept up to date if you make sure to compute it in every write operation that might affect the outcome.
# frozen_string_literal: true
require 'faraday'
require 'parametric/types'
require 'digest'
include Parametric
# A quick helper to benchmark function calls
def bench(&block)
# https://vimeo.com/113707214
class Result
attr_reader :value
def initialize(v)
@value = v
end
class << self
def success(v)
# frozen_string_literal: true
class Worker
def self.wrap(callable)
if callable.respond_to?(:setup)
callable
elsif callable.respond_to?(:call)
new(callable)
else
raise ArgumentError, "worker must implement #setup, #run, #shutdown"
module Identities
TypeError = Class.new(ArgumentError)
class Nope
def matches?(_)
false
end
def call(value)
value
@ismasan
ismasan / field_policies.rb
Created February 11, 2020 15:52
Playing with composable value coercion policies
Result = Struct.new('Result', :mode, :value) do
def pass
copy(:pass)
end
def halt
copy(:halt)
end
def copy(new_mode)
Parametric.policy(:nullable_integer) do
PARAMETRIC_INT_EXP = /^\d+$/.freeze
coerce do |value, _key, _context|
if value.to_s =~ PARAMETRIC_INT_EXP
value.to_i
else
nil
end
end
@ismasan
ismasan / validations_for.rb
Created June 26, 2019 09:22
Declarative AM validations depending on arbitrary attributes
# class Page
# include ValidationsFor
#
# validations_for page_type: 'offer' do
# validates :amount, presence: true
# end
#
# validations_for page_type: 'page' do
# validates :title, presence: true
# end

Bodegas default

Para integrar ecommerce <-> inventario.

  • world
  • ecommerce
  • sales
done evento Bootic Transferencia de inventario
{% if product.has_multiple_prices or product.price.positive %}
<span class="bootic-price">{{ product.price.format }}</span>
<span class="bootic-price-comparison-box" style="display: {% if product.has_price_comparison %}inline-block{% else %}none{% endif %}">
<span class="bootic-price-comparison-label">{{ 'products.price_comparison_label' | t }}</span>
<strike class="bootic-price-comparison">
{% if product.has_price_comparison %}
{{ product.price_comparison.format }}
{% endif %}
</strike>