Skip to content

Instantly share code, notes, and snippets.

View durran's full-sized avatar

Durran Jordan durran

View GitHub Profile
@durran
durran / bson.rb
Created June 3, 2014 21:12
BSON Custom Serialization/Deserialization
class MyObject
def to_bson(encoded = ''.force_encoding(BINARY))
to_hash.to_bson(encoded)
end
def self.from_bson(bson)
new(Hash.from_bson(bson))
end
end
@durran
durran / reasoning.txt
Created August 6, 2015 06:37
Mongoid Migration
The problem with the Github issues is a tradeoff. In order for MongoDB to officially
support Mongoid we need to have the issues in Jira in order to properly adhere to our
commercial support contracts with respect to response times, as well as free support.
There's no simple way to setup 301 redirects from the old Github issues to the new Jira issues,
but every single Github issue was migrated - over 4000 since the project's inception in
2009 and all are public (https://jira.mongodb.org/browse/MONGOID). Google will eventually
resolve them. It's better in the short and long term to give up some of this to have Mongoid
officially supported and actively developed.
As for the documentation, the separation of the Mongoid 5 docs and the 3/4 docs was to
# lib/multi_parameter_attributes.rb
module MultiParameterAttributes
def time(attributes, name)
attrs = attributes.collect do |key, value|
if key =~ /^#{Regexp.escape(name.to_s)}\((\d+)(\w)\)$/
[$1.to_i, value.send("to_#$2")]
end
end.compact.sort_by(&:first).map(&:last)
Time.zone.local(*attrs) unless attrs.empty?
Given /^the following (.+?)(|\s+only)$/ do |model_name, only, table|
parser = Mongoid::Cucumber::Parser.new(model_name)
model, klass = parser.model, parser.klass
klass.destroy_all if only =~ /only$/
@objects = Mongoid::Cucumber::Factory.create(klass, table)
@objects.each(&:save!)
@object = @objects.last if @objects.size == 1
instance_variable_set("@#{model}", @object)
end
class PetOwner < Mongoid::Document
field :title
has_one :address
end
class Address < Mongoid::Document
field :street
belongs_to :addressable, :inverse_of => :address
end
#<HelpFile _id: test-help-file, title: Test help file, excerpt: Hello world!..., is_published: true, content: Hello world!, publish_date: Sun Jan 03 20:10:35 UTC 2010>
Mon Jan 04 07:10:35 +1100 2010
#<ActiveSupport::TimeZone:0x1a831e8 @tzinfo=nil, @name="Canberra", @utc_offset=36000>
Spec::Runner.configure do |config|
config.mock_with :mocha
config.after :suite do
Mongoid.database.collections.each(&:drop)
end
end
require "spec_helper"
describe Mongoid::Criteria do
describe "#max" do
before do
10.times do |n|
Person.create(:title => "Sir", :age => (n * 10), :aliases => ["D", "Durran"])
end
[ durran@modetojoy-3 | ~/Work/mongoid ] > irb
irb(main):001:0> require "mongoid"
=> true
irb(main):002:0> class Person
irb(main):003:1> include Mongoid::Document
irb(main):004:1> field :age, :type => Integer
irb(main):005:1> end
=> nil
irb(main):006:0> conn = Mongo::Connection.new
=> #<Mongo::Connection:0x18d925c @slave_ok=nil, @queue=#<ConditionVariable:0x18d91a8>, @nodes=[["localhost", 27017]], @safe_mutex=#<Mutex:0x18d91bc>, @options={}, @size=1, @checked_out=[], @connection_mutex=#<Mutex:0x18d91d0>, @port=27017, @logger=nil, @id_lock=#<Mutex:0x18d9220>, @sockets=[], @timeout=5.0, @host="localhost">
context "when document contains a hash field" do
before do
@map = { "first" => 10, "second" => "Blah" }
@person = Person.create(:map => @map)
end
it "properly gets and sets the has attributes" do
@person.map.should == @map
@from_db = Person.find(@person.id)