Skip to content

Instantly share code, notes, and snippets.

View esshka's full-sized avatar
🏠
Working from home

Eugeny esshka

🏠
Working from home
View GitHub Profile
'use strict';
var _ = require('lodash'),
moment = require('moment'),
Promise = require("bluebird");
module.exports = function (clamps) {
let start = +new Date(),
dayms = 1000 * 60 * 60 * 24;
@esshka
esshka / Enhance.js
Last active August 29, 2015 14:26 — forked from sebmarkbage/Enhance.js
Higher-order Components
import { Component } from "React";
export var Enhance = ComposedComponent => class extends Component {
constructor() {
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}
render() {
@esshka
esshka / recalc
Created June 30, 2015 19:56
recalc script for zoomator
#!/usr/bin/env node
'use strict';
var pg = require('pg');
var QueryStream = require('pg-query-stream');
var conString = "postgres://localhost/zoomator_new2";
var Writable = require('stream').Writable;
var deviceId = process.argv[2];
var util = require('util');
var _ = require('lodash');

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 stupid 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
@esshka
esshka / rails_sib.rb
Last active August 29, 2015 14:23 — forked from JoshCheek/rails_sib.rb
gem 'rails', '4.2.1' # prob works on others, too, but this is the one I figured it out on
require "rails"
require 'active_record'
require 'action_controller/railtie'
require 'action_view/railtie'
# ===== Configuration =====
Rails.logger = ActiveRecord::Base.logger = Logger.new $stdout
ActiveSupport::LogSubscriber.colorize_logging = false
;;
;; NS CHEATSHEET
;;
;; * :require makes functions available with a namespace prefix
;; and optionally can refer functions to the current ns.
;;
;; * :import refers Java classes to the current namespace.
;;
;; * :refer-clojure affects availability of built-in (clojure.core)
;; functions.
  • defprotocol: defines an interface
  • deftype: create a bare-bones object which implements a protocol
  • defrecord: creates an immutable persistent map which implements a protocol

Typically you'll use defrecord (or even a basic map);
unless you need some specific Java inter-op,
where by you'll want to use deftype instead.

Note: defprotocol allows you to add new abstractions in a clean way Rather than (like OOP) having polymorphism on the class itself,

@esshka
esshka / index.html
Last active August 29, 2015 14:23 — forked from enjalot/index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Testing Pie Chart</title>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.1.3"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.geom.js?2.1.3"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js?2.1.3"></script>
<style type="text/css">
den = Math.ceil(rawdata.length / 500);
processedData = _(rawdata)
.chunk(den)
.map(function(part){
if (!part) return
den = Math.ceil(rawdata.length / 500);
processedData = _(rawdata)
.chunk(den)
.map(function(part){
if (!part) return