Skip to content

Instantly share code, notes, and snippets.

View odigity's full-sized avatar

Ofer Nave odigity

View GitHub Profile
@odigity
odigity / Firebase Database API Cheatsheet
Last active June 9, 2022 03:45
Firebase Database API Cheatsheet
There is no way to store an empty object/array/null value.
There are also no actual arrays. Array values get stored as objects with integer keys.
(If all keys are integers, it will be returned as an array.)
Basically, it's one giant tree of hashes with string keys.
Simply write a value to any location, and the intermediary locations will automatically come into existance.
── Classes ──
DataSnapshot : Container for a subtree of data at a particular location.
@odigity
odigity / .bashrc
Last active October 14, 2015 16:37
repo(){
cd "$HOME/Dropbox/Personal/src/$1"
echo -en "\033]0;$1\a"
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]${PWD#/home/ofer/Dropbox/Personal/src/}\[\033[01;33m\] \$ \[\033[00m\]'
}
@odigity
odigity / base.rb
Created October 9, 2015 17:49
Sequel model class config example
def self.configure_class(table:, filter: {}, &block)
ds = db[table]
ds = ds.filter filter unless filter.blank?
ds = ds.filter &block if block_given?
set_dataset(ds)
fixture_keys.each { |k| FixtureDependencies.class_map[k] = self }
end
def self.fixture_keys
# PorcIO::ID::Models::User -> [ :user, :porc_io/id/models/user ]
@odigity
odigity / database.rb
Created October 8, 2015 15:44
Colorizing Sequel logger
require 'colorize'
class CLogger < Logger
def info(message)
color = case message
when Regexp.union(%w[CREATE ALTER DROP])
:light_blue
when Regexp.union(%w[INSERT UPDATE DELETE])
:light_yellow
else
@odigity
odigity / sequel_scopes.rb
Last active June 15, 2025 15:48
The Sequel Gem: Everything About Scopes
# (I recommend understanding the basics of this first: http://sequel.jeremyevans.net/rdoc/files/doc/object_model_rdoc.html)
# Extending the underlying dataset (http://sequel.jeremyevans.net/rdoc/files/README_rdoc.html#label-Extending+the+underlying+dataset)
# The recommended way to implement table-wide logic by defining methods on the dataset using dataset_module:
class Post < Sequel::Model
dataset_module do
def posts_with_few_comments
where{num_comments < 30}
class ModelBase
# depends on Dirty plugin: http://www.rubydoc.info/gems/sequel/Sequel/Plugins/Dirty
def validates_unchanged(attribute)
errors.add(attribute, 'cannot be modified') if !new? and column_changed?(attribute)
end
end
class Model < ModelBase
def validate
validates_unchanged :username
irb(main):010:0> Sequel::Model.instance_eval do
irb(main):011:1* def fixture_key
irb(main):012:2> singularize(underscore(demodulize(name))).to_sym
irb(main):013:2> end
irb(main):014:1> end
=> :fixture_key
irb(main):015:0> Sequel::Model.fixture_key
=> :model
root@a16708715370:/tmpdev# ruby -e 'puts $LOADED_FEATURES.sort'
/usr/local/lib/ruby/2.2.0/monitor.rb
/usr/local/lib/ruby/2.2.0/unicode_normalize.rb
/usr/local/lib/ruby/2.2.0/x86_64-linux/enc/encdb.so
/usr/local/lib/ruby/2.2.0/x86_64-linux/enc/trans/transdb.so
/usr/local/lib/ruby/2.2.0/x86_64-linux/rbconfig.rb
/usr/local/lib/ruby/2.2.0/x86_64-linux/thread.so
/usr/local/lib/ruby/site_ruby/2.2.0/rubygems.rb
/usr/local/lib/ruby/site_ruby/2.2.0/rubygems/basic_specification.rb
/usr/local/lib/ruby/site_ruby/2.2.0/rubygems/compatibility.rb
class MembershipLevel
include Comparable
include ActiveModel::Validations
## Constants
MONTHLY = 10
ANNUAL = 20
LIFETIME = 30
d = moment('2015-03-01')
Object { _isAMomentObject: true, _i: "2015-03-01", _f: "YYYY-MM-DD ", _isUTC: false, _pf: Object, _locale: Object, _d: Date 2015-03-01T05:00:00.000Z }
d.format('YYYY-MM-DD')
"2015-03-01"