Skip to content

Instantly share code, notes, and snippets.

extension JSONValue: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let value = try? container.decode(Bool.self) {
self = .boolean(value)
} else if let value = try? container.decode(Double.self) {
// Double covers Int as well
self = .number(value)
} else if let value = try? container.decode(String.self) {
import XCTest
fileprivate extension JSONValue {
var objectValue: [String: JSONValue]? {
switch self {
case .object(let value): return value
default: return nil
}
}
var arrayValue: [JSONValue]? {
@bricker
bricker / application.html.erb
Created June 21, 2012 22:47
Simple breadcrumbs for Rails
<!-- Simple breadcrumbs for Rails -->
<!-- Example output -->
<!-- This example uses the Twitter Bootstrap classes for breadcrumbs -->
<!-- http://twitter.github.com/bootstrap/components.html#breadcrumbs -->
<% if breadcrumbs %>
<ul class="breadcrumb">
<% breadcrumbs.each do |crumb| %>
<li>
@bricker
bricker / promises.md
Last active March 14, 2018 00:33
Promises in Rails callbacks, using after_save and after_commit together.

"Russian-Doll Caching" is great. It embraces the Rails (and Ruby) goal to "make the developer happy". And it does. Not having to worry about cache expiration is superb.

It has its limits, though. If you're trying to avoid any database queries, russian-doll caching will not work for you. If you are trying to represent thousands, or even hundreds, of objects under a single cache fragment, russian-doll caching is not the best option.

We use it whenever it makes sense, but sometimes we just have to bite the bullet and expire a cache fragment manually. When you want to start manually expiring cache on a fairly busy website, you have to start considering race conditions. I recently ran into the following scenario:

class Post < ActiveRecord::Base
  after_save :expire_cache
  
require 'find'
require 'pathname'
##
# Given a directory, loop through all of its files recursively,
# and then search the project for references to each file.
name = "image-references"
app_base = "/Users/bricker/websites/kpcc/SCPRv4"
files_base = "#{app_base}/app/assets/images"
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
/**
* Bryan Ricker
* CS130 Lab #3
*/
public class TwoDArrayOperations {
public static void main(String[] args) throws IOException {
@bricker
bricker / 2012073001_rails_dbsync.mdown
Created July 30, 2012 22:08
Rails: Synchronizing your development database with your production database using Rake tasks

Rails: Synchronizing your development database with your production database using Rake tasks

July 30, 2012

If you run an active website with lots of new content every day, it's often helpful to keep your local database up-to-date with your production database, so when you're working on the application locally you're working with fresh content.

Here are a couple rake tasks that you can copy-and-paste into your local Rails application which will make this syncing process a one-step process.

mv .git/ /tmp
git init .
git add -A
git commit -m "Cleared history"
git remote add origin https://bitbucket.com/your-repo.git
git push -f origin
public abstract class BaseClient
{
public static final String ENDPOINT = "http://something.com/api/"
private String getAbsoluteUrl(String relativeUrl)
{
return ENDPOINT + API_ROOT + relativeUrl;
}
}
class CoolModel
def self.create_with_options(options={})
# ...
self.create(something)
end
end
CoolModel.create_with_options(...)