Skip to content

Instantly share code, notes, and snippets.

View nepalez's full-sized avatar

Andrew Kozin nepalez

  • Evil Martians
  • Lisbon, Portugal
View GitHub Profile
@nepalez
nepalez / test
Created October 25, 2014 16:58
json-schema test
require 'json-schema'
text = '{"uuid":"3ea8fd89-232f-4ed1-90b5-743da173cd7d","name":"FIRM"}'
schema = '
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
@nepalez
nepalez / gist:3f48353a206eaa244165
Created May 21, 2015 22:56
ROM commands chaining with mapper
require "rom"
ROM.setup :memory
class Users < ROM::Relation[:memory]
register_as :users
end
class Tasks < ROM::Relation[:memory]
register_as :tasks
end
@nepalez
nepalez / jruby
Last active August 29, 2015 14:24 — forked from d4rky-pl/meth_vs_proc.rb
Calculating -------------------------------------
method 56.995k i/100ms
proc 49.213k i/100ms
proc new 57.562k i/100ms
-------------------------------------------------
method 1.797M (±10.7%) i/s - 8.891M
proc 1.522M (± 8.3%) i/s - 7.579M
proc new 1.867M (± 7.9%) i/s - 9.267M
Comparison:
require 'equalizer'
module Checkproc
InvalidValueError = Class.new(StandardError) { include Equalizer.new(:message) }
class Composite
attr_reader :left
attr_reader :right
def initialize(left, right)
@nepalez
nepalez / ruby_challenges.rb
Last active August 29, 2015 14:25
ruby challenges
# identity
id = -> v { v }
# composition
composition = -> g, f { -> v { g[f[v]] } }
# testing for id
g = -> v { v * 2 }
composition(g, id)[3] == g[3] # true
composition(id, g)[3] == g[3] # true
require 'fiddle'
A = Class.new
B = Class.new
C = Class.new A
p C.superclass # => A
(Fiddle::Pointer[Fiddle.dlwrap C] + 16)[0, 8] = Fiddle::Pointer[Fiddle.dlwrap B].ref
p C.superclass # => B
require "rspec"
require "mustermann"
# Checks whether client condition has received given request
#
# @example
# allow(client)
# .to receive_request(:get, "example.com/users/:id/bills")
# .and_return({ id: 1, price: 100, currency: "RUR" })
#
require "rspec"
require "mustermann"
require "delegate"
# Gem-speficic RSpec mocks and expectations
#
# @example
# allow(client)
# .to receive_request(:get, '/users/1')
# .where { |req| req[:query].include? "auth" => "foobar" }
@nepalez
nepalez / martian_anniversaries.rb
Created March 31, 2016 22:57
Martian anniversaries
# Happy birthday to you...
#
# @example
# dates = MartianAnniversaries.from '2015-01-01'
# dates.take(2)
# # => ['2016-11-17', '2018-10-05']
#
class MartianAnniversaries
include Enumerable
@nepalez
nepalez / reform_ext.rb
Last active July 15, 2016 14:29
Reform monkey-patching
require 'reform'
require 'reform/form/coercion'
require 'hashie/mash'
module Reform
module DryTypes
def property(name, type: nil, **options, &block)
super(name, **options, &block).tap do
if type
# Tries to coerce value to the necessary type