Skip to content

Instantly share code, notes, and snippets.

View wojtha's full-sized avatar

Vojtěch Kusý wojtha

View GitHub Profile
@wojtha
wojtha / question_multiple_choice.rb
Created January 23, 2017 10:08
Class Question::MultipleChoice is a simple, closed-ended question type that lets respondents select one or multiple answers from a defined list of choices.
# Class Question::MultipleChoice is a simple, closed-ended question type that lets respondents select one or multiple
# answers from a defined list of choices.
#
# @example Definition
# question = Question::MultipleChoice.new("Reference doesn't seem to be regular release. MAKE A BUILD?")
# .choice(:build, 'b', '[b]uild')
# .choice(:install, 'i', '[i]nstall and build')
# .choice(:clean, 'c', '[c]lean install and build')
# .choice(:skip, 's', '[s]kip')
# .choice(:quit, 'q', '[q]uit')
@wojtha
wojtha / json_array.rb
Last active January 15, 2017 10:47
ActiveRecord serialize loader for ActiveModel::Serializers
class JSONArray
attr_accessor :object_class, :serializer_class
def initialize(object_class, serializer_class = nil)
@object_class = object_class
@serializer_class = serializer_class || object_class.new.active_model_serializer
end
def dump(arr)
@wojtha
wojtha / result_after.rb
Created January 14, 2017 19:39
Status factory lib for Ruby
class Result
include StatusFactory.new(:create, :update, :delete, :identical, callbacks: true)
end
@wojtha
wojtha / active_record_soft_delete.rb
Created January 7, 2017 23:21
Implementing soft deletes in Rails, source: http://patrikonrails.com/posts/3
class Comment < ApplicationRecord
default_scope -> { where(deleted_at: nil) }
def soft_delete
update(deleted_at: Time.current)
end
end
class CastingStruct < Struct
def self.new(hash, &blk)
super(*hash.keys) do
define_method :initialize do |*args|
super *hash.values.map { |method|
args.shift.public_send method
}
end
class_eval(&blk) if blk
end
# Class FeatureFlag defines and stores feature flags
#
# @see http://blog.arkency.com/2015/11/simple-feature-toggle-for-rails-app/
#
class FeatureFlag
def initialize
@flags = {}
yield self if block_given?
end
@wojtha
wojtha / alias_matchers.md
Created October 31, 2016 12:17 — forked from JunichiIto/alias_matchers.md
List of alias matchers in RSpec 3

This list is based on aliases_spec.rb.

You can see also Module: RSpec::Matchers API.

matcher aliased to description
a_truthy_value be_truthy a truthy value
a_falsey_value be_falsey a falsey value
be_falsy be_falsey be falsy
a_falsy_value be_falsey a falsy value
@wojtha
wojtha / download.rb
Last active February 3, 2023 14:06
Download binary file with 'open-uri' and 'IO.copy_stream'
require 'open-uri'
# Net::HTTP.get(uri) does't work for binary files. File gets corrupted.
#
# File is opended in binary write mode "wb". The "open" method is provided
# by open-uri and is considered unsafe for user input! The "rb" stands for
# binary read mode.
#
# IO.copy_stream does not load the whole file in memory.
#
@wojtha
wojtha / rails_cheatsheet.md
Created October 6, 2016 16:02 — forked from taktran/rails_cheatsheet.md
Rails cheatsheet

An incomplete cheatsheet for rails 3. Things are added as they are required.

## Debugging

Check errors

@user = User.new(:email => email)
puts @user.valid?
@user.errors.each { |e| puts "#{e}: #{@user.errors[e]}" }
@wojtha
wojtha / bench_slim_md.rb
Last active October 4, 2016 16:09
Benchmark Slim with Markdown
require 'slim'
require 'benchmark/ips'
Benchmark.ips do |x|
x.report("pure slim") { Slim::Template.new('template_slim.slim').render }
x.report("slim with markdown") { Slim::Template.new('template_md.slim').render }
x.compare!
end