Skip to content

Instantly share code, notes, and snippets.

View wojtha's full-sized avatar

Vojtěch Kusý wojtha

View GitHub Profile
@wojtha
wojtha / getdeep.py
Created April 28, 2022 08:22 — forked from av1m/getdeep.py
Crosses a list or dictionary in an elegant way using list or str as key
"""Utility functions"""
from functools import reduce
from operator import getitem
from typing import Any, Mapping, Union
def getdeep(data: Mapping, map_list: Union[list, str], default: Any = None) -> Any:
"""Iterate nested dictionary|list and can return default value if key not found
@wojtha
wojtha / .gitattributes
Created October 22, 2020 21:45 — forked from tekin/.gitattributes
An example .gitattributes file that will configure custom hunk header patterns for some common languages and file formats. See https://tekin.co.uk/2020/10/better-git-diff-output-for-ruby-python-elixir-and-more for more details.
*.c diff=cpp
*.h diff=cpp
*.c++ diff=cpp
*.h++ diff=cpp
*.cpp diff=cpp
*.hpp diff=cpp
*.cc diff=cpp
*.hh diff=cpp
*.m diff=objc
*.mm diff=objc
@wojtha
wojtha / 00_README.md
Created October 24, 2017 20:42 — forked from dbalatero/00_README.md
This is an example of how I combine interaction/service classes with Wisper event broadcasting inside Rails.

This is an example of how I combine interaction/service classes with Wisper event broadcasting in Rails.

In this example, I show a UsersController#create API, a corresponding service object, and all the test code/listeners to make it all happen.

The outcome is:

  • Concepts in your system ("Signing up a user", "Creating an order") have a single entry point in your codebase, vs. making raw ActiveRecord calls to object.save in dozens of places.
  • Since your concept has one entry point (the service class), you can easily grep for usage of it.
  • Stupid easy to attach listeners to the service class
  • All event listeners are very small and easily unit tested
@wojtha
wojtha / private_constants.rb
Created October 23, 2017 23:26 — forked from skwp/private_constants.rb
private_constants.rb
# A simple way to create private constants without all the noise of ruby's 'private_constant'
#
# Before:
# class Foo
# FOO = "bar"
# private_constant :FOO
# end
#
# After:
# class Foo
@wojtha
wojtha / Ruby and Rails Interview Cheat Sheet.md
Created August 11, 2017 12:28 — forked from ahmadhasankhan/Ruby and Rails Interview Cheat Sheet.md
This is my Ruby interview cheat sheet. Feel free to fork it, Use it, Share it, or do whatever you want with it. PLEASE let me know if there is any error or if anything crucial is missing. I will keep updating...

Ruby and Rails Interview Questions

Ruby

  • What is a class?
  • What is an object?
  • What is a module? Can you tell me the difference between classes and modules?
  • Can you tell me the three levels of method access control for classes and modules? What do they imply about the method?
  • There are three ways to invoke a method in ruby. Can you give me at least two?
  • Explain this ruby idiom: a ||= b
@wojtha
wojtha / answers.md
Created August 11, 2017 12:27 — forked from austenito/answers.md
Answers to Ruby Interview Questions

Answers to [Ruby Interview Questions][1]

What is a class?

A text-book answer: classes are a blue-print for constructing computer models for real or virtual objects... boring.

In reality: classes hold data, have methods that interact with that data, and are used to instantiate objects.

Like this.

@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 / 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 / gist:d4d874c962ae5cc6d5c858a4aa081674
Created June 23, 2016 13:53 — forked from sgergely/gist:3793166
Midnight Commander Keyboard Shortcuts for Mac OSX
----- Esc -----
Quick change directory: Esc + c
Quick change directory history: Esc + c and then Esc + h
Quick change directory previous entry: Esc + c and then Esc + p
Command line history: Esc + h
Command line previous command: Esc + p
View change: Esc + t (each time you do this shortcut a new directory view will appear)
Print current working directory in command line: Esc + a
Switch between background command line and MC: Ctrl + o
Search/Go to directory in active panel: Esc + s / Ctrl + s then start typing directory name
@wojtha
wojtha / README.md
Created March 8, 2016 11:17 — forked from derwiki/README.md
Ruby module that you can use in a `before_action` on sensitive controllers for which you'd like a usage audit trail

Adding an audit log to your Rails app

If you have any sort of administrative interface on your web site, you can easily imagine an intruder gaining access and mucking about. How do you know the extent of the damage? Adding an audit log to your app is one quick solution. An audit log should record a few things:

  • controller entry points with parameter values
  • permanent information about the user, like user_id
  • transient information about the user, like IP and user_agent

Using the Rails framework, this is as simple as adding a before_action to your admin controllers. Here’s a basic version that I’m using in production.