Skip to content

Instantly share code, notes, and snippets.

@yesnik
yesnik / ruby_task_1.rb
Last active August 29, 2015 14:11
Задание по Ruby 1
=begin
Представим, что у нас есть некоторый внешний источник,
который возвращает нам данные в виде массива хэшей, причем все значения в хэше - строки.
Нам нужно написать много разных преобразователей, которые выполняют преобразование
переданных данных по заданным правилам.
=end
# Создайте такой модуль Converter, который позволит создавать такие классы-преобразователи:
# Пример преобразователя:
@yesnik
yesnik / ruby_task_1_answer.rb
Created December 23, 2014 05:03
Задание по Ruby 1 (ответ)
require 'time'
module Converter
extend Enumerable
@@methods = {}
def new(*args, &block)
initialize(*args, &block)
end
@yesnik
yesnik / ruby_task_2.rb
Created December 24, 2014 06:56
Задание по Ruby 2
# Пример 1
def m
a = 1
lambda { a + 1 }
end
f = m
a = 3
f.call #=> ?
# Пример 2
@yesnik
yesnik / ruby_task_io.rb
Last active August 29, 2015 14:12
Задание по Ruby (IO)
# Пример 1
f = File.open('file.txt', 'w+')
f.write('Hello')
# Почему выводит "", а не "Hello"?
p f.read() #=> ""
# Пример 2
file = File.open("file.txt", "r")
p file.read #=> "Hello"
# Почему второй read снова не выводит "Hello"?
@yesnik
yesnik / product_test.rb
Last active August 29, 2015 14:13
Пример синтаксиса Ruby тестов
# encoding: utf-8
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
# Определяем, какую из фикстур загружать для теста
# Это будет: test/fixtures/products.yml
# fixtures :products - Rails загрузит это по умолчанию
# Свойства товара не должны быть пустыми
# encoding: utf-8
class PayerDecorator < Draper::Decorator
delegate_all
decorates :payer
def inn_unique?(inn)
Payer.where { (major_requisite.eq 'INN') & (major_requisite_value.eq inn) & (id.not_eq id) }.exists?
end
end
@yesnik
yesnik / response_example.rb
Created October 21, 2015 12:01
Ruby on Rails request.env variable content
# This is the content of variable request.env
# We get this content in controller:
# class BlogsController < ApplicationController
# def index
# render plain: "Response: #{ YAML::dump(request.env) }"
# end
# end
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
UNICORN_GROUP=${UNICORN_GROUP:-${UNICORN_USER}}
UNICORN_CONFIGFILE=${UNICORN_CONFIGFILE:-/etc/unicorn/${RC_SVCNAME}.conf.rb}
command=${UNICORN_BINARY:-/usr/bin/unicorn_rails}
command_args="${UNICORN_ARGS:--E production} -D -c ${UNICORN_CONFIGFILE}"
pidfile=${UNICORN_PIDFILE:-/var/run/${RC_SVCNAME}/unicorn.pid}
#start_stop_daemon_args="--user ${UNICORN_USER} --env GEM_HOME=/home/dk/.gem/ruby/1.9"
@yesnik
yesnik / service_base_example.rb
Last active November 1, 2016 12:22
Базовый класс сервиса
# Базовый класс, от которого наследуются все сервисы
class ServiceBase
attr_reader :data
class << self
def call(data)
instance = new(data)
instance.call
instance
end
@yesnik
yesnik / seller_plan_report.sql
Created March 23, 2017 13:56
SQL-запрос отчета
WITH accessible_users AS (
SELECT DISTINCT public.users.* FROM public.users WHERE
public.users.id IN (50034, 33322, 11233)
)
SELECT
1 AS condition,
MAX(ac.title) AS accounting_center, -- Центр учета
MAX(users.name) AS seller, -- Продавец
MAX(clients.id) AS client_id, -- ИД Клиента
MAX(clients.title) AS client_title, -- Наименование клиента