Skip to content

Instantly share code, notes, and snippets.

View igor-alexandrov's full-sized avatar
😀
Ruby, Ruby, Ruby, Ruby... Crystal :)

Igor Aleksandrov igor-alexandrov

😀
Ruby, Ruby, Ruby, Ruby... Crystal :)
View GitHub Profile
jobs:
rubocop:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: .ruby-version
bundler-cache: true
- name: RuboCop
def fetch_using_http(uri)
HTTP::Client.get(uri.to_s) do |response|
if response.status == HTTP::Status::OK
parse_type(response.body_io) # return FastImage::Meta or raises an error
end
end
end
def process(uri)
if uri.scheme == "http" || uri.scheme == "https"
fetch_using_http(uri) # returns FastImage::Meta
elsif (uri.scheme.nil? || uri.scheme == "file") && !uri.path.nil?
fetch_using_file(uri) # returns FastImage::Meta
end
end
module PluginA
DEFAULT_OPTIONS = {
c: 3,
d: 4,
}
module InstanceMethods
def plugin_a_method_a
"plugin_a_method_a"
end
module Spectator::Matchers
struct HavePermissionsMatcher(ExpectedType) < Spectator::Matchers::ValueMatcher(ExpectedType)
def description : String
"have permissions of #{expected.label}"
end
# Checks whether the matcher is satisfied with the expression given to it.
private def match?(actual : Spectator::TestExpression(T)) : Bool forall T
File.info(actual.value).permissions.value == expected.value
end
@igor-alexandrov
igor-alexandrov / form.rb
Last active July 19, 2019 03:13
dry-validation conditional validation
class Fund::CreateCallForm < BaseForm
property :type
property :fund
validation do
required(:type) { filled? & included_in?(TRANSACTION_TYPES['Fund::Call']) }
end
validation if: -> (results) { rebalance? } do
required(:fund).filled
@igor-alexandrov
igor-alexandrov / form.rb
Last active March 29, 2017 08:51
reform2 custom predicates with dry-validation
class Fund::CreateCallForm < BaseForm
# your properties are here
# ...
validation do
configure do
config.messages_file = 'config/locales/dry-validation/errors.yml'
# don't forget about this option to be able to access your form object from your predicates
option :form
@purchased_at = Date.civil(params[:purchased_at]["(1i)"].to_i,
params[:purchased_at]["(2i)"].to_i,
params[:purchased_at]["(3i)"].to_i)
@code = Partner::Code.lookup("Vendor", params[:code]) || build_vendor_code
@not_redeem = nil
@error_text = nil
@to_late = nil
if @code.nil?
@igor-alexandrov
igor-alexandrov / gist:6942754
Created October 11, 2013 22:05
Complete example of how to map association from Sequel::Model to AR. Eager loading works like a charm!
class Tour < Sequel::Model
many_to_one :vendor_pansion,
:dataset => proc{ Vendor::Pansion.where(:id => self.vendor_pansion_id) },
:reciprocal => nil, :class => 'Vendor::Pansion',
:eager_loader => (proc do |opts|
opts[:rows].each{ |object| object.associations[:vendor_pansion] = nil }
Vendor::Pansion.where(:id => opts[:id_map].keys).each do |vendor_pansion|
if tours = opts[:id_map][vendor_pansion.id]
tours.each do |tour|
@igor-alexandrov
igor-alexandrov / gist:4612075
Created January 23, 2013 19:41
Parameters fix for Rails 2.3.x and Ruby 1.9.x
class ActionController::Base
def force_utf8_params
traverse = lambda do |object, block|
if object.kind_of?(Hash)
object.each_value { |o| traverse.call(o, block) }
elsif object.kind_of?(Array)
object.each { |o| traverse.call(o, block) }
else
block.call(object)