Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/gitmodel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
require 'gitmodel/index'
require 'gitmodel/persistable'
require 'gitmodel/transaction'
require 'gitmodel/serialization/yajl'
require 'gitmodel/serialization/yaml'

module GitModel

DEFAULT_SERIALIZER = GitModel::Serialization::Yajl
# db_root must be an existing git repo. (It can be created with create_db!)
# Bare repositories aren't supported yet, it must be a normal git repo with a
# working directory and a '.git' subdirectory.
Expand All @@ -38,6 +41,9 @@ module GitModel
mattr_accessor :memcache_servers
mattr_accessor :memcache_namespace

mattr_accessor :serializer
self.serializer = DEFAULT_SERIALIZER

def self.repo
@@repo = Grit::Repo.new(GitModel.db_root)
end
Expand Down Expand Up @@ -122,4 +128,13 @@ def self.head_sha(branch_name)
ref = File.join(repo.git.git_dir, "refs/heads/#{branch_name}")
File.exist?(ref) ? File.read(ref).chomp : nil
end

def self.attributes_filename
serializer.attributes_filename
end

def self.filename_extension
serializer.filename_extension
end

end
4 changes: 2 additions & 2 deletions lib/gitmodel/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def attr_index(attr)
end

def filename
File.join(@model_class.db_subdir, '_indexes.json')
File.join(@model_class.db_subdir, "_indexes.#{GitModel.filename_extension}")
end

def generated?(branch = GitModel.default_branch)
Expand All @@ -48,7 +48,7 @@ def save(options = {})
end
data << [attr,values_and_ids]
end
data = Yajl::Encoder.encode(data, nil, :pretty => true)
data = GitModel.serializer.encode(data)
t.index.add(filename, data)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gitmodel/persistable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def save(options = {})
transaction = options.delete(:transaction) || GitModel::Transaction.new(options)
result = transaction.execute do |t|
# Write the attributes to the attributes file
t.index.add(File.join(dir, 'attributes.json'), Yajl::Encoder.encode(attributes, nil, :pretty => true))
t.index.add(File.join(dir, 'attributes.json'), GitModel.serializer.encode(attributes))

# Write the blob files
blobs.each do |name, data|
Expand Down
19 changes: 19 additions & 0 deletions lib/gitmodel/serialization/yajl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module GitModel
module Serialization
class Yajl
class << self
def filename_extension
"json"
end

def attributes_filename
"attributes.#{filename_extension}"
end

def encode(data)
::Yajl::Encoder.encode(data, nil, :pretty => true)
end
end
end
end
end
19 changes: 19 additions & 0 deletions lib/gitmodel/serialization/yaml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module GitModel
module Serialization
class Yaml
class << self
def filename_extension
"yaml"
end

def attributes_filename
"attributes.#{filename_extension}"
end

def encode(data)
data.to_hash.to_yaml
end
end
end
end
end
18 changes: 15 additions & 3 deletions spec/gitmodel/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,22 @@
@i.attr_index(:x).should == {1 => SortedSet.new(["foo", "bar"]), 2 => SortedSet.new(["baz"])}
end

it "knows it's filename" do
@i.filename.should == "test_entities/_indexes.json"
end
context "with serializer" do
context "Yajl" do
it "knows it's filename" do
GitModel.serializer = GitModel::Serialization::Yajl
@i.filename.should == "test_entities/_indexes.json"
end
end

context "Yaml" do
it "knows it's filename" do
GitModel.serializer = GitModel::Serialization::Yaml
@i.filename.should == "test_entities/_indexes.yaml"
end
end
end

it "can save itself to a JSON file" do
@i.save
json = <<-END.strip
Expand Down
43 changes: 43 additions & 0 deletions spec/gitmodel/serialization/yajl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'spec_helper'

RSpec.configure do |c|
c.include RawData
end

describe GitModel::Serialization::Yajl do
describe ".encode" do
it "should encode data as json" do
data = {
x: 1,
y: "bob",
z: [4,5,6]
}
GitModel::Serialization::Yajl.encode(data).should ==
::Yajl::Encoder.encode(data, nil, :pretty => true)
end
end

context "set as GitModel serializer" do
before do
GitModel.serializer = GitModel::Serialization::Yajl
end


it "should use attributes.json as the attribute filename" do
GitModel.attributes_filename.should == "attributes.json"
end

it "should save attributes as json" do
data = {
x: 1,
y: "bob",
z: [4,5,6]
}

TestEntity.create!(:id => "foo",
:attributes => {:x => 1, :y => "bob", :z => [4,5,6]})

last_saved_entity_attributes('foo').should == Yajl::Encoder.encode(data, nil, :pretty => true)
end
end
end
44 changes: 44 additions & 0 deletions spec/gitmodel/serialization/yaml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'spec_helper'

RSpec.configure do |c|
c.include RawData
end

describe GitModel::Serialization::Yaml do
describe ".encode" do
it "should encode data as yaml" do
data = {
x: 1,
y: "bob",
z: [4,5,6]
}
GitModel::Serialization::Yaml.encode(data).should ==
data.to_yaml
end
end

context "set as GitModel serializer" do
before do
GitModel.serializer = GitModel::Serialization::Yaml
end


it "should use attributes.json as the attribute filename" do
GitModel.attributes_filename.should == "attributes.yaml"
end


it "should save attributes as yaml" do
data = {
'x' => 1,
'y' => "bob",
'z' => [4,5,6]
}

TestEntity.create!(:id => "foo",
:attributes => {:x => 1, :y => "bob", :z => [4,5,6]})

last_saved_entity_attributes('foo').should == data.to_yaml
end
end
end
5 changes: 5 additions & 0 deletions spec/gitmodel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@
end
end

describe ".serializer" do
it "should be Yajl by default" do
GitModel.serializer.should == GitModel::Serialization::Yajl
end
end
end

11 changes: 11 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

RSpec.configure do |c|
c.mock_with :rspec

c.before(:each) do
GitModel.serializer = GitModel::DEFAULT_SERIALIZER
end
end

class TestEntity
Expand All @@ -19,3 +23,10 @@ class TestEntity2

#GitModel.logger.level = ::Logger::DEBUG
GitModel.memcache_servers = ['localhost']

module RawData
def last_saved_entity_attributes(id)
repo = Grit::Repo.new(GitModel.db_root)
(repo.commits.first.tree / File.join(TestEntity.db_subdir, id, 'attributes.json')).data
end
end