This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class AccountTest < ActiveSupport::TestCase | |
test 'presence on create' do | |
account = Account.create | |
refute account.valid? | |
end | |
test 'inclusion :subtype' do | |
# invalid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tag | |
class << self | |
attr_accessor :match | |
attr_accessor :children | |
end | |
self.children = [] | |
def self.inherited(klass) | |
Tag.children << klass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
module W | |
class Token | |
attr_reader :value, :tags | |
def initialize(value) | |
@value = value | |
@tags = [] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < W::Controller | |
permit_attributes :name, :email | |
def index | |
@users = User.order('created_at DESC') | |
end | |
def new | |
@user = User.new | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |