Skip to content

Instantly share code, notes, and snippets.

require 'test_helper'
class AccountTest < ActiveSupport::TestCase
test 'presence on create' do
account = Account.create
refute account.valid?
end
test 'inclusion :subtype' do
# invalid
class Tag
class << self
attr_accessor :match
attr_accessor :children
end
self.children = []
def self.inherited(klass)
Tag.children << klass
#!/usr/bin/env ruby
module MethodFromClass
def self.included(klass)
Object.send(:define_method, klass.to_s) do |*args|
klass.new(*args)
end
end
end
#!/usr/bin/env ruby
module W
class Token
attr_reader :value, :tags
def initialize(value)
@value = value
@tags = []
end
class UsersController < W::Controller
permit_attributes :name, :email
def index
@users = User.order('created_at DESC')
end
def new
@user = User.new
end
# Dont try this at home
require 'relapse'
Relapse.handle ':ordinal of :month, :year' do |match|
day = match.ordinal[0..-2].to_i
Time.local(match.year, match.month, day)
end
Relapse.parse '4th of june, 2007'
# Dont try this at home
require 'relapse'
require 'active_support/all'
def chain_send(obj, *methods)
methods.flatten.inject(obj) { |o, a| o.send(a) }
end
def parse(match)
time = chain_send(match[1].to_i, match[2], match[3])
require 'relapse'
require "active_support/all"
def parse(string)
time = string[0].to_i.weeks.ago
hour = string[-4, 2].to_i
hour += 12 if string[-2, 2] == 'pm'
time.change(hour: hour)
end
#!/usr/bin/env ruby
def get_values(string, expression)
regex = Regexp.new(expression.gsub(/\{([^\}]+)\}/) { "(?<#{$1}>\\S+)" })
(match = regex.match(string)) ? Hash[match.names.zip(match.captures)] : {}
end
p get_values "Alice is 23 years old", "{name} is {age} years old" #=> {"name"=>"Alice", "age"=>"23"}
#!/usr/bin/env ruby
class Dre
MONTHS = %w(january february march april may june july
august september october november december)
def self.parse(string, options = {})
new(options).parse(string)
end