Skip to content

Instantly share code, notes, and snippets.

View michaeltwofish's full-sized avatar

Michael C. Harris michaeltwofish

  • South Australia, Australia
View GitHub Profile
public function testMap() {
$model = $this->_model;
$model::config(array('connection' => false));
$collection = new DocumentSet(compact('model'));
$collection->map(function($data) {return $data;})
$this->assertEqual($model, $collection->model()); // passes because map() doesn't run
}
@michaeltwofish
michaeltwofish / gist:825434
Created February 14, 2011 03:15
Euler problem 1
# Brute force
sum = 0
(1..999).each do |num|
sum = sum + num if (num % 3 == 0 || num % 5 == 0)
end
puts "Brute force: #{sum}"
# More rubyish, but still brute force
sum = (1..999).inject(0) {|sum,num| (num % 3 == 0 || num % 5 == 0) ? sum + num : sum}
puts "More rubyish but still brute force: #{sum}"
@michaeltwofish
michaeltwofish / gist:828802
Created February 16, 2011 03:25
Euler problem 2
N = 4000000
# Brute force
def fib(num)
if num < 2
num
else
fib(num - 2) + fib(num - 1)
end
end
$clusters_list = Cluster::all(array('conditions' =>array(
'id' => array('!=' => implode(',', $form_clusters->map(function($cluster) {
return $cluster->id;
}, array('collect' => false)))),
)));
// Create a finder for clusters not linked to 'object'
static::finder('unlinked', function($self, $params, $chain) {
$object = $params['options']['object'];
unset($params['options']['object']);
// @todo Shouldn't make a direct call to getClusters, but should instead
// have a protected array of things that can be linked
$ids = $object->getClusters()->map(function($cluster) {
return $cluster->id;
}, array('collect' => false));
@michaeltwofish
michaeltwofish / 3.rb
Created February 20, 2011 07:18
Euler Problem 3
num = 600851475143
# Brute force
def is_prime?(n)
limit = Math.sqrt(n).round
(2..limit).each do |i|
return false if n % i == 0
end
return true
end
// Handle attaching children
$('#unattached_children a.add').click(function() {
var icons = $(this).parent();
var child_type = // work out what the name of the child is
// Update label text
$('#attached_children>label').html('Attached '+child_type);
// Move the item to the attached items list
$('#AttachedItems .list').append($(this).closest('.item'));
$('#attached_children .list .item:last-child').append('<input name="'+child_type+'[]" type="hidden" value="'+$('#attached_children .list .item:last-child').attr('id')+'" />');
bson (1.2.0)
bson_ext (1.2.0)
cucumber (0.10.0)
gherkin (2.3.2)
mechanize (1.0.0)
mongo (1.2.0)
mysql (2.8.1)
rake (0.8.7)
rdoc (2.5.8)
selenium-client (1.2.18)
Failing Scenarios:
cucumber ./cluster_create.feature:6 # Scenario: View the clusters page
cucumber ./cluster_create.feature:21 # Scenario: Create a new cluster
cucumber ./cluster_create.feature:27 # Scenario: Create a cluster and be directed to the clusters page
cucumber ./cluster_create.feature:77 # Scenario: Create a cluster and be directed to the clusters page
cucumber ./cluster_create.feature:86 # Scenario: Create a cluster when no forms exist
cucumber ./unit_update.feature:183 # Scenario: View a unit that has one item and see the item position
cucumber ./unit_update.feature:191 # Scenario: View a unit that has two items and see the item position
cucumber ./unit_retrieve.feature:34 # Scenario: Visit units page with one unit and see view link
cucumber ./form_retrieve.feature:28 # Scenario: Visit forms page with one form and see view link
@michaeltwofish
michaeltwofish / step_definitions.rb
Created March 8, 2011 21:59
Using XPath in webrat to specify that one element should come before another. Using id but could use anything.
Then /^I should see element "([^"]*)" before element "([^"]*)"$/ do |first, second|
xpath = "//*[@id='#{first}']/following-sibling::*[@id='#{second}']"
response_body.should have_xpath(xpath)
end