Skip to content

Instantly share code, notes, and snippets.

@bf4
bf4 / gist:67754b65ce30c37fad6a
Last active September 21, 2015 04:32 — forked from kurko/gist:6662513
OO solutions for imperative problems

OO solutions for imperative problems

[september, 22nd 2013]

On september 17th, 2013, Aaron Cruz wrote in the Objects on Rails mailing list:

I have a very imperative problem. Something needs to be done, then something else, then something else, constantly changing state from one step to the next.

Video Import example >

post_data = {}
post_data[:name] = 'Title for my link'
post_data[:link] = 'http://path.to/my/link'
post_data[:caption] = 'A caption'
post_data[:description] = 'A description'
post_data[:picture] = 'http://path.to/myimage.jpg'
post_data[:actions] = { :name => 'My site name', :link => 'http://link.to/my/site'}.to_json
client.post("feed", nil, post_data)
@bf4
bf4 / Facebook Phonebook Export
Created February 8, 2011 00:01
Modified from http://userscripts.org/scripts/show/92383, need to enable on each page, doesn't iterate
// ==UserScript==
// @name FB phonebook Exporter
// @namespace elleestcrimi
// @include https://www.facebook.com/friends/edit/?sk=phonebook*
// ==/UserScript==
var $;
// Add jQuery
(function()
@bf4
bf4 / config
Created February 10, 2011 22:59 — forked from bruno/campfire_export.rb
campfire transcript export
---
BUNDLE_WITHOUT: ""
+module Sunspot
+ # monkey patch for Sunspot v 1.1.1 to add support for solr "start" query parameter
+ # we can now optionally add a :start number to offset search results
+ # in this scenario, we want the first 3 results for the carousel, and then 5 more
+ # the code as is requires the start to be set to 3 and the per_page to 8 for this to work
+ # a better solution would be to modify the per_page= method but I was unable to do this
+ # with alias_method_chain, BF 12/6
+ module Query
+ class Pagination
+
@bf4
bf4 / ARCHITECTURE.md
Created October 1, 2015 20:45
activemodel::serializers architecture draft

I'd like to narrow the Serializer interface rather than broaden it. Every method we add has the potential to interfere with an attribute on the model.

Also, with respect to this change, couldn't you just use the key option?

class FooSerializer < AM::Serializer attribute :name, key: title end FooSerializer.new(foo).attributes FooSerializer._attributes_keys

@bf4
bf4 / rails_attribute_serializer_macro.rb
Created July 20, 2012 17:08
Rails attribute serializer macro
module Macros
module Serializer
# usage
# include Macros::Serializer
# e.g. a field named :metadata must be a Hash
# serializer :metadata, Hash
def self.included(host_class)
# field must be a symbol
@bf4
bf4 / ruby_module_pattern.rb
Created August 23, 2012 16:29
Ruby Module pattern
module NameSpace
module MyClass
def self.included(host_class)
host_class.instance_eval do
has_one :thing
attr_accessible :something
validate :foo
extend ClassMethods
@bf4
bf4 / logical_active_record.rb
Last active October 9, 2015 04:28
Failed attempt at a Logical ActiveRecord layer
# modeled off the Physical / Service / Logical layers
# advocated by http://enterpriserails.chak.org/full-text/chapter-3-organizing-with-modules
module Logical
class ARModelName
PHYSICAL = ARModel
attr_accessor :physical
private :physical
def initialize(physical)
@physical = physical.is_a?(PHYSICAL) ? physical : PHYSICAL.new