Skip to content

Instantly share code, notes, and snippets.

View strukturedkaos's full-sized avatar

Don Pottinger strukturedkaos

View GitHub Profile

Data Integrity

Programming is hard. We must valiantly fight complexity on a daily basis. Conditional statements quickly increase the complexity of a system. Every time we’re unsure about a value, we add a conditions to decide how to proceed. If only we could become more confident with the state of the system…

Hurrah! Data integrity can give us this confidence. By constraining our data, we can make some assumptions about the state of the system. Gone are the days of guarding potentially nil values. Out with orphaned and duplicate records! Data is the [life] of our software. Let’s take [life] by the reigns.

@strukturedkaos
strukturedkaos / student_presenter.rb
Last active August 29, 2015 14:07
Student Presenter example
# stored in app/presenters/student_presenter.rb
class StudentPresenter < SimpleDelegator
delegate :email, to: :user
def initialize(student)
@student = super(student)
end
def self.find(id)
@strukturedkaos
strukturedkaos / compond_assignment
Created August 27, 2014 18:54
|= compound assignment example
irb(main):013:0> a = []
=> []
irb(main):014:0> a |= [1]
=> [1]
irb(main):015:0> a |= [1]
=> [1]
irb(main):016:0> a
#!/usr/bin/env ruby
require 'prime'
class AdditiveFunctionValidator
attr_reader :limit, :prime_numbers
def initialize(limit:)
@limit = limit
@strukturedkaos
strukturedkaos / copy_aws_s3.rb
Created July 1, 2014 01:10
Copy files between AWS S3 buckets
s3.buckets['kickdrop-production-assets'].objects['uploads/'].copy_to('uploads/', :bucket_name => 'kickdrop-staging-assets')
source_bucket='kickdrop-production-assets'
target_bucket='kickdrop-staging-assets'
source_key = 'uploads'
target_key = 'uploads'
bucket1 = s3.buckets[source_bucket]
bucket2 = s3.buckets[target_bucket]
obj1 = bucket1.objects.with_prefix("uploads")
obj2 = bucket2.objects.with_prefix("uploads")
@strukturedkaos
strukturedkaos / mailchimp-controller-2.js
Created June 25, 2014 20:48
Angular Mailchimp Controller's POST request to Mailchimp API
// Send subscriber data to MailChimp
MailChimpSubscription.save(
// Successfully sent data to MailChimp.
function (response) {
// Define message containers.
mailchimp.successMessage = '';
mailchimp.errorMessage = '';
// Store the result from MailChimp
mailchimp.result = response.result;
@strukturedkaos
strukturedkaos / mailchimp-controller-1.js
Created June 25, 2014 20:28
Angular Mailchimp Controller's use of $resource
url = '//' + mailchimp.username + '.' + mailchimp.dc + '.list-manage.com/subscribe/post-json';
params = {
'EMAIL': mailchimp.email,
'FNAME': mailchimp.fname,
'LNAME': mailchimp.lname,
'c': 'JSON_CALLBACK',
'u': mailchimp.u,
'id': mailchimp.id
};
actions = {
@strukturedkaos
strukturedkaos / mailchimp-form-1.html
Created June 25, 2014 20:25
Angular Mailchimp Credentials
<input class="hidden" type="hidden" ng-model="mailchimp.username" ng-init="mailchimp.username='kickdrop'">
<input class="hidden" type="hidden" ng-model="mailchimp.dc" ng-init="mailchimp.dc='us3'">
<input class="hidden" type="hidden" ng-model="mailchimp.u" ng-init="mailchimp.u='425ab2b3e010a637cca296582'">
<input class="hidden" type="hidden" ng-model="mailchimp.id" ng-init="mailchimp.id='d2686186f0'">
@strukturedkaos
strukturedkaos / dependencies-mailchimp-form-1.html
Created June 25, 2014 20:15
Dependencies for Angular Mailchimp Form
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
<link href="css/app.css" media="all" rel="stylesheet">
<link href="bower_components/animate.css/animate.min.css" media="all" rel="stylesheet" type="text/css" />
<link href="css/animations.css" media="all" rel="stylesheet">
<link href="bower_components/font-awesome/css/font-awesome.min.css" media=all rel=stylesheet />
<link href='http://fonts.googleapis.com/css?family=Lato:100,300,400' rel='stylesheet' type='text/css'>
@strukturedkaos
strukturedkaos / dependencies-mailchimp-form-1.html
Created June 25, 2014 20:13
Dependencies for Angular Mailchimp Form
body>
...
<!-- In production use:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/x.x.x/angular.min.js"></script>
-->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-cookies/angular-cookies.js"></script>