Skip to content

Instantly share code, notes, and snippets.

View cheerfulstoic's full-sized avatar

Brian Underwood cheerfulstoic

View GitHub Profile
def die_roll
rand(6) + 1
end
def attack(attacking_count, defending_count)
attacking_dice = [attacking_count, 3].min
defending_dice = [defending_count, 2].min
attacking_rolls = attacking_dice.times.map { die_roll }.sort.reverse
defending_rolls = defending_dice.times.map { die_roll }.sort.reverse
@cheerfulstoic
cheerfulstoic / gist:3142ed2e15ad08ef6631
Last active March 4, 2019 12:56
Script for "Neo4j.rb Screencast #1 - Create a Neo4j Rails Application"

Welcome to the first episode in a series of short screencasts for the Ruby neo4j gem. Each episode will describe a different aspect of the gem. In this first episode, we will start by discussing how to set up a new Ruby on Rails app using Neo4j. The neo4j gem is not Rails specific so you can use other frameworks like Sinatra or Lotus, but because of the popularity of Ruby on Rails we will be using it for this screencast series.

In this series we’re going to create an application to host digital assets which we’ll call "asset_portal". To create your Rails app you can run a rails new command as usual. To use Neo4j instead of ActiveRecord you can give arguments which will set up a new Rails app using Neo4j models.

rails new asset_portal -m http://neo4jrb.io/neo4j/neo4j.rb -O

The dash-m argument runs a script from the neo4j gem project and the dash-capital-O argument says to exclude ActiveRecord. There aren’t any restrictions on using Neo4j with other ORMs like ActiveRecord, but you will need to conf

@cheerfulstoic
cheerfulstoic / trace_execution.rb
Last active February 27, 2018 14:07
trace_execution
def trace_execution(levels = 4)
cyan = "\e[36m"
clear = "\e[0m"
green = "\e[32m"
indent = 0
output = ''
trace = TracePoint.new(:call, :c_call, :return, :c_return) do |tp|
if [:return, :c_return].include?(tp.event) && indent.nonzero?
@cheerfulstoic
cheerfulstoic / gist:7d64cc4a3d87162c09562ffe283d813c
Created January 31, 2018 14:03
Queries and R code for Paradise Papers presentation
# R setup
library(RNeo4j)
graph = startGraph("http://localhost:7474/db/data/")
# Queries:
# What types of objects are we dealing with?
MATCH (n)
@cheerfulstoic
cheerfulstoic / .tmux.conf
Created September 22, 2017 16:57
My ~/.tmux.conf
set -g mouse on
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
bind-key -n S-Up resize-pane -U 5
bind-key -n S-Down resize-pane -D 5
bind-key -n S-Left resize-pane -L 5
bind-key -n S-Right resize-pane -R 5
@cheerfulstoic
cheerfulstoic / neo4jrb.yml
Last active September 22, 2017 16:54
tmuxinator file for working on Neo4j.rb projects
# ~/.tmuxinator/neo4jrb.yml
name: neo4jrb
root: ~/github/neo4jrb
# Optional tmux socket
# socket_name: foo
# Runs before everything. Use it to start daemons etc.
pre:
@cheerfulstoic
cheerfulstoic / gist:b6f6b10a380aec27d097
Last active July 11, 2017 16:56
Script for "Neo4j.rb Screencast #2 - Properties"

Welcome to the second in a series of short screencasts for the Ruby neo4j gem. Today we’ll be discussing properties on ActiveNode models. Everything discussed here applies to ActiveRel models as well, but we’ll talk about ActiveRel in a later episode.

Nodes in Neo4j are much like documents in MongoDB. They can have any number of properties and those properties can be assigned values of different types.

In the neo4j gem we can define properties on an ActiveNode model class by calling the property method. Here we have an Asset model describing assets. Calling the property method with the property’s name as the only argument is sufficient to declare the property and to assign values to it.

property :title
asset.title = "Widget"
asset # =>
@cheerfulstoic
cheerfulstoic / gist:262412202b7f8fb283dd
Last active April 17, 2017 21:14
Script for "Neo4j.rb Screencast #6 - Deeper Querying"

Welcome to the sixth episode in a series of short screencasts about the Ruby neo4j gem. Previously we learned how to chain associations together to create queries which can hop across entities and use any part of the entire path to query records of interest. In this episode we dig deeper into the tools behind the magic. A familiarity with the Cypher query language is advised. See the show notes for a link to a Cypher tutorial.

As discussed previously, when we call associations we get a proxy object:

user.created_assets

There are two kinds of proxy objects: AssociationProxy and QueryProxy. The details aren't important for the moment, but behind the scenes both are causing a Query object to be built. We can see this Query object by calling the query method at any point:

user.created_assets.query
@cheerfulstoic
cheerfulstoic / gist:7ace53e80a6d9efc75b30b376b97da1a
Created December 23, 2016 13:53
Rails generator wizard idea
rails g migration
What would you like to call your migration? > Something
Which of the following would you like to do:
1 - create a table
2 - add a column
3 - remove a column
# etc…
> 1
What would you like to call your table? somethings
Please give the first column for your table: name
@cheerfulstoic
cheerfulstoic / neo4jrb 8.0 upgrade notes.md
Created July 4, 2016 12:21
Neo4j.rb 8.0 upgrade notes
  • server_db mode has been removed and should be replaced by either http or bolt