DO's:
- Use explicit contracts to pipe data & events between systems
- Business rules should bubble towards the top, UI and semantics should sink towards the bottom
DONT's:
| # Call scopes directly from your URL params: | |
| # | |
| # @products = Product.filter(params.slice(:status, :location, :starts_with)) | |
| module Filterable | |
| extend ActiveSupport::Concern | |
| module ClassMethods | |
| # Call the class methods with the same name as the keys in <tt>filtering_params</tt> | |
| # with their associated values. Most useful for calling named scopes from |
| items = %w{foo bar baz goo gar gaz hoo har haz} | |
| def recurse_array(ary, &block) | |
| head, *tail = ary | |
| head and block.call(head) | |
| tail.any? and recurse_array(tail, &block) | |
| end | |
| puts "First the items in order:\n" | |
| recurse_array items do |item| |
| defmodule MacroExp do | |
| defmacro attr_accessor(atom) do | |
| getter = String.to_atom("get_#{atom}") | |
| setter = String.to_atom("set_#{atom}") | |
| quote do | |
| def unquote(getter)(data) do | |
| data |> Map.from_struct |> Map.get(unquote(atom)) | |
| end | |
| def unquote(setter)(data, value) do | |
| data |> Map.put(unquote(atom), value) |
| var API = { | |
| get: function() { | |
| return new Promise(function() { | |
| // ... some code to get remotely | |
| }); | |
| } | |
| } | |
| var UserAPI = { | |
| getById: function(id) { |
| #!/usr/bin/ruby -w | |
| require 'csv' | |
| require 'active_support/core_ext' | |
| class Parser | |
| attr_accessor :input_folder | |
| attr_accessor :output_folder | |
| attr_accessor :filename |
| module Kernel | |
| def system(*args) | |
| rd, wr = IO.pipe | |
| # Create a new subprocess that will just exec the requested program. | |
| pid = fork do | |
| # The sub-process closes its copy of the reading end of the pipe | |
| # because it only needs to write. | |
| rd.close |
| # I'm no benchmark guru. Just did a bunch of: | |
| $ time ruby <filename> | |
| # Note: This is just an 80mb XML file with 38,000 nodes. | |
| ox_dom.rb 4.56s user 0.78s system 93% cpu 5.714 total (550mb) | |
| ox_dom.rb 4.58s user 0.79s system 87% cpu 6.126 total (550mb) | |
| ox_dom.rb 4.60s user 0.80s system 87% cpu 6.140 total (550mb) | |
| nokigiri_dom.rb 11.75s user 1.02s system 94% cpu 13.518 total (895mb) | |
| nokigiri_dom.rb 11.36s user 1.02s system 93% cpu 13.211 total (895mb) |
| # A small DSL for helping parsing documents using Nokogiri::XML::Reader. The | |
| # XML Reader is a good way to move a cursor through a (large) XML document fast, | |
| # but is not as cumbersome as writing a full SAX document handler. Read about | |
| # it here: http://nokogiri.org/Nokogiri/XML/Reader.html | |
| # | |
| # Just pass the reader in this parser and specificy the nodes that you are interested | |
| # in in a block. You can just parse every node or only look inside certain nodes. | |
| # | |
| # A small example: | |
| # |
| daemon off; | |
| # Heroku dynos have at least 4 cores. | |
| worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>; | |
| events { | |
| use epoll; | |
| accept_mutex on; | |
| worker_connections 1024; | |
| } |