Skip to content

Instantly share code, notes, and snippets.

View rinaldifonseca's full-sized avatar

Rinaldi Fonseca rinaldifonseca

View GitHub Profile
@rinaldifonseca
rinaldifonseca / domain.md
Created January 6, 2020 16:18 — forked from vsavkin/domain.md
Building Rich Domain Models in Rails

Building Rich Domain Models in Rails

Abstract

Domain model is an effective tool for software development. It can be used to express really complex business logic, and to verify and validate the understanding of the domain among stakeholders. Building rich domain models in Rails is hard. Primarily, because of Active Record, which doesn't play well with the domain model approach.

One way to deal with this problem is to use an ORM implementing the data mapper pattern. Unfortunately, there is no production ready ORM doing that for Ruby. DataMapper 2 is going to be the first one.

Another way is to use Active Record just as a persistence mechanism and build a rich domain model on top of it. That's what I'm going to talk about here.

  • Dynamic Dispatch
  • Dynamic Method
  • Ghost Methods
  • Dynamic Proxies
  • Blank Slate
  • Kernel Method
  • Flattening the Scope (aka Nested Lexical Scopes)
  • Context Probe
  • Class Eval (not really a 'spell' more just a demonstration of its usage)
  • Class Macros
@rinaldifonseca
rinaldifonseca / PaperclipStringFile
Created May 4, 2018 03:54 — forked from bendangelo/PaperclipStringFile
Paperclip string to file
module Paperclip
#converts a string into a file for paperclip to save
# useage
# self.avatar = Paperclip::string_to_file('bob.png', 'image/png', 'BASE64 here')
def self.string_to_file(name, type, data)
image = StringIO.new(data)
image.class.class_eval { attr_accessor :original_filename, :content_type }
image.original_filename = name
image.content_type = type
@rinaldifonseca
rinaldifonseca / deploy.rb
Created January 8, 2018 17:31 — forked from cannikin/deploy.rb
Notify Sentry of a new release via Capistrano
# This task will notify Sentry via their API[1] that you have deployed
# a new release. It uses the release timestamp as the `version`
# (like 20151113182847) and the git ref as the optional `ref` value.
#
# This task requires several environment variables be set (or just
# hardcode the values in here if you like living on the edge):
#
# ENV['SENTRY_API_ENDPOINT'] : API endpoint, https://app.getsentry.com
# ENV['SENTRY_ORG'] : the organization for this app
# ENV['SENTRY_PROJECT'] : the project for this app
FLCNBYGG-W4BMMPTX-QVOCMG4S-EG5CWUPH-BYMM6QKU-DI7HT4D2-GAWAEFBU-5PQPK3U6-KTLAYUKP-HEYE2CZA-B5L4DMQC-CRGQJKG5-AABZGREJ-LIEAPOBV-5LQ2HGOY-OUGQKQIP
module PayPal::SDK
module REST
module DataTypes
Payer.class_eval do
object_of :merchant_id, String
end
class ChargeModel < Base
object_of :id, String
object_of :type, String
package com.trailbehind.downloads;
import android.util.Log;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectMetadataRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;
import com.crashlytics.android.Crashlytics;
package com.memtrip;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.graphics.Bitmap;
@rinaldifonseca
rinaldifonseca / flush.rb
Created April 21, 2015 02:26
redis-flushall
port = 6379
$redis = Redis.new(:port => port)
$redis.flushall
# ListRecurringDetailsResponse#details
def details
mapped_attributes = {
:recurring_detail_reference => "recurringDetailReference",
:creation_date => "creationDate",
:variant => "variant",
:card_holder_name => "card.holderName",
:card_expiry_month => "card.expiryMonth",
:card_expiry_year => "card.expiryYear",