money

php
<?php

namespace App\Domain;

use InvalidArgumentException;

Serializing a Money Value Object with a Custom Symfony Normalizer

symfony serializer value-object
by codesnips 3 tabs
rust
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Currency {
    pub code: &'static str,
    pub symbol: &'static str,
    pub exponent: u32,
}

Locale-Aware Money Formatting With Minor Units in Rust

rust money currency
by codesnips 3 tabs
ruby
class CreateAccountsAndLedger < ActiveRecord::Migration[7.1]
  def change
    create_table :accounts do |t|
      t.string :name, null: false
      t.string :currency, null: false, default: "USD"
      t.bigint :balance_cents, null: false, default: 0

Atomic Account Transfers in Rails With Row Locks and a Balance Service

rails postgres transactions
by codesnips 4 tabs
typescript
import { Knex } from 'knex';

export interface RawDailyRow {
  day: string;
  order_count: string;
  gross_cents: string;

Aggregate Daily Order Totals Into a Report With Knex and a Formatter

knex postgres reporting
by codesnips 3 tabs
ruby
class Order < ApplicationRecord
  has_many :line_items, inverse_of: :order, dependent: :destroy

  accepts_nested_attributes_for :line_items,
    allow_destroy: true,
    reject_if: ->(attrs) { attrs["product_id"].blank? && attrs["quantity"].blank? }

Validate Nested Attributes for an Order and Its Line Items in Rails

rails activerecord validations
by codesnips 3 tabs
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

Apply and Validate Coupon Codes with a Cart Totals Service in Laravel

laravel eloquent service-layer
by codesnips 4 tabs