Skip to content

Instantly share code, notes, and snippets.

@davelnewton
davelnewton / phase1.rb
Created September 16, 2011 02:01
Repeats validator that adds model methods
module Newton
module Validator
class RepeatsValidator < ActiveModel::EachValidator
def initialize(options)
@attributes = options[:attributes]
super
@how_count = options[:how_count]
@upto = options[:upto]
end
@davelnewton
davelnewton / Main.java
Created October 5, 2011 03:46
Velocity array access
package main;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
@davelnewton
davelnewton / ControllerFragment.rb
Created October 25, 2011 21:08
Quick voting refactoring
def vote_yes
vote(:up_vote)
end
def vote_no
vote(:down_vote)
end
private
@davelnewton
davelnewton / Orig.java
Last active September 27, 2015 23:07
Constructor vs. factories to enhance clarity an readability
// Most everything left out for clarity.
public class RPCRespond<Result> {
public RPCRespond() {
this.code = 0;
this.success = true;
this.result = null;
}
@davelnewton
davelnewton / 00_misuse.java
Created November 12, 2011 15:27
Stupid singleton is stupid
public class UsesLongRunningCtor {
/** Retrieves data and returns processed result. */
public String transformData() {
// LongRunningCtor "embedded"; direct reference, like a static utility class.
String rawData = LongRunningCtor.instance().fetch();
return rawData.replace("Raw", "Processed");
}
}
@davelnewton
davelnewton / menus.rb
Created November 17, 2011 03:09
Proc version of menu selection
#
# Not saying it's a good idea, but it's interesting.
#
# A Proc is just something you can call, with arguments.
p = Proc.new { |wat| puts wat }
p.call "ohai"
# => "ohai"
@davelnewton
davelnewton / syntax_highlighting.py
Created December 22, 2011 18:13 — forked from norbajunior/syntax_highlighting.py
Ruby on Rails syntax highlight switcher for Sublime Text 2
import sublime, sublime_plugin
import os
class DetectFileTypeCommand(sublime_plugin.EventListener):
""" Detects current file type if the file's extension isn't conclusive """
""" Modified for Ruby on Rails and Sublime Text 2 """
""" Original pastie here: http://pastie.org/private/kz8gtts0cjcvkec0d4quqa """
def on_load(self, view):
filename = view.file_name()
@davelnewton
davelnewton / pom.xml
Created January 28, 2012 20:05
Maven build file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org xsd/maven-.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mygroup</groupId>
<artifactId>Test_Maven_03</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test_Maven_03</name>
<url>http://maven.apache.org</url>
@davelnewton
davelnewton / dm.rb
Created April 25, 2012 17:06
DataMapper create-/query-by-hash equivalence
customer = {
:name => 'Dan Kubb',
:orders => [
{
:reference => 'TEST1234',
:order_lines => [
{
:item => {
:sku => 'BLUEWIDGET1',
:unit_price => 1.00,
@davelnewton
davelnewton / 01_spec.rb
Created August 9, 2012 01:48
RSpec Foolishness
describe Foo do
it { should respond_to(:bar) }
end