mongo-hashie 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 Oliver Kiessler
1
+ Copyright (c) 2009/2010 Oliver Kiessler
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.rdoc CHANGED
@@ -1,6 +1,52 @@
1
1
  = mongo-hashie
2
2
 
3
- Description goes here.
3
+ mongo-hashie is a mongodb library that aims to be simple and effective. No need to explicitly define properties. Getters and setters are created through the underlying Mash class from hashie.
4
+
5
+ == Install
6
+
7
+ $ gem install mongo-hashie
8
+
9
+ == Usage
10
+
11
+ class Document < MongoHashie::Base
12
+ end
13
+
14
+ Simply add new properties by calling the setter.
15
+
16
+ Basic CRUD operations
17
+
18
+ $ document = Document.new(:author => "Oliver Kiessler")
19
+ $ document.name = "test attribute"
20
+ $ document.save # create
21
+ $ puts document._id # Mongo ObjectID String represenation
22
+
23
+ $ document.name = "simple change"
24
+ $ document.save # update
25
+
26
+ $ document.destroy
27
+
28
+ $ document2 = Document.create(:author => "Oliver Kiessler", :name => "Great Document Name")
29
+
30
+ $ Document.destroy_all
31
+
32
+ Find operations
33
+
34
+ $ Document.first
35
+ $ Document.find("4b3f4bd4c1d7110513000001") # Mongo ObjectID String represenation
36
+
37
+ $ Document.all
38
+ $ Document.all(:author => "Oliver Kiessler")
39
+ $ Document.find(:author => "Oliver Kiessler", :name => "Great Document Name")
40
+
41
+ $ Document.count
42
+
43
+ Meta Data
44
+
45
+ $ document1 = Document.create(:author => "Oliver Kiessler", :name => "Great Document Name", :prop1 => "Test")
46
+ $ document2 = Document.create(:author => "Oliver Kiessler", :name => "Great Document Name", :prop2 => "Test")
47
+ $ document3 = Document.create(:author => "Oliver Kiessler", :name => "Great Document Name", :prop3 => "Test")
48
+
49
+ $ puts Document.properties_used.inspect
4
50
 
5
51
  == Note on Patches/Pull Requests
6
52
 
@@ -14,4 +60,4 @@ Description goes here.
14
60
 
15
61
  == Copyright
16
62
 
17
- Copyright (c) 2009 Oliver Kiessler. See LICENSE for details.
63
+ Copyright (c) 2009/2010 Oliver Kiessler. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -2,8 +2,11 @@ module MongoHashie
2
2
  def self.included(base)
3
3
  base.class_eval do
4
4
  extend ClassMethods
5
- include InstanceMethods
5
+ extend Rails
6
+ extend MetaData::ClassMethods
6
7
  include Mongo
8
+ include InstanceMethods
9
+ include MetaData::InstanceMethods
7
10
  end
8
11
  end
9
12
 
@@ -13,12 +16,13 @@ module MongoHashie
13
16
  end
14
17
 
15
18
  def save
16
- if self._id.blank?
19
+ if self._id.nil?
17
20
  self._id = Mongo::ObjectID.new
18
21
  collection.insert(self)
19
22
  else
20
23
  collection.update({'_id' => self._id}, self)
21
24
  end
25
+ update_keys if MongoHashie::Configuration.create_meta_data and not self.is_a?(MetaDataProperties)
22
26
  self
23
27
  end
24
28
 
@@ -28,46 +32,50 @@ module MongoHashie
28
32
  end
29
33
 
30
34
  module ClassMethods
31
- def database
32
- @@database ||= "mongohashie"
33
- end
34
- def database=(value); @@database = value; end
35
-
36
35
  def connection
37
- Mongo::Connection.new("localhost", 27017, :pool_size => 5, :timeout => 5)
36
+ Mongo::Connection.new(MongoHashie::Configuration.host, MongoHashie::Configuration.port,
37
+ :pool_size => MongoHashie::Configuration.pool_size, :timeout => MongoHashie::Configuration.timeout)
38
38
  end
39
39
 
40
- def db; @@db ||= connection.db(database); end
41
- def create(hash = {}); self.new(hash).save; end
40
+ def db; @@db ||= connection.db(MongoHashie::Configuration.database); end
41
+ end
42
+ end
42
43
 
43
- def all(options = {})
44
- self.new.collection.find.collect {|doc| self.new(doc)}
45
- end
44
+ class MongoHashie::Base < Hashie::Mash
45
+ include MongoHashie
46
+ end
46
47
 
47
- def first(options = {})
48
- self.new(self.new.collection.find_one)
49
- end
48
+ class MongoHashie::MetaDataProperties < MongoHashie::Base
49
+ end
50
50
 
51
- def find(*args)
52
- if args.first.is_a?(Hash)
53
- result = self.new.collection.find(args.first)
54
- else
55
- result = self.new.collection.find('_id' => Mongo::ObjectID.from_string(args.first))
56
- end
57
- result.count == 1 ? result.collect {|doc| self.new(doc)}.first : result.collect {|doc| self.new(doc)}
58
- end
51
+ class MongoHashie::Configuration
52
+ def self.database
53
+ @@database ||= 'mongohashie'
54
+ end
55
+ def self.database=(value); @@database = value; end
59
56
 
60
- def count
61
- self.new.collection.count
62
- end
57
+ def self.host
58
+ @@host ||= 'localhost'
59
+ end
60
+ def self.host=(value); @@host = value; end
63
61
 
64
- def destroy_all
65
- self.new.collection.remove
66
- end
62
+ def self.port
63
+ @@port ||= 27017
67
64
  end
68
- end
65
+ def self.port=(value); @@port = value; end
69
66
 
70
- class MongoHashie::Base < Hashie::Mash
71
- include MongoHashie
72
- end
67
+ def self.pool_size
68
+ @@pool_size ||= 5
69
+ end
70
+ def self.pool_size=(value); @@pool_size = value; end
73
71
 
72
+ def self.timeout
73
+ @@timeout ||= 5
74
+ end
75
+ def self.timeout=(value); @@timeout = value; end
76
+
77
+ def self.create_meta_data
78
+ @@create_meta_data ||= true
79
+ end
80
+ def self.create_meta_data=(value); @@create_meta_data = value; end
81
+ end
@@ -0,0 +1,48 @@
1
+ module MongoHashie
2
+ module MetaData
3
+ module InstanceMethods
4
+ def update_keys
5
+ meta_data_wrapper = MetaDataProperties.first(:class_name => self.class.name)
6
+ if meta_data_wrapper
7
+ updated = false
8
+ keys.each do |key|
9
+ if not MetaDataProperties.ignored_keys.include?(key) and not meta_data_wrapper.key?(key)
10
+ meta_data_wrapper.send("#{key.to_sym}=", self.send(key.to_sym).class.name)
11
+ updated = true
12
+ end
13
+ meta_data_wrapper.save if updated
14
+ end
15
+ else
16
+ properties_used = {:class_name => self.class.name}
17
+ keys.each do |key|
18
+ properties_used[key] = self.send(key.to_sym).class.name unless MetaDataProperties.ignored_keys.include?(key)
19
+ end
20
+ MetaDataProperties.create(properties_used)
21
+ end
22
+ end
23
+
24
+ def reset_keys
25
+ meta_data_wrapper = MetaDataProperties.first(:class_name => self.class.name)
26
+ meta_data_wrapper.destroy if meta_data_wrapper
27
+ end
28
+ end
29
+
30
+ module ClassMethods
31
+ def properties_used
32
+ meta_data_properties = MetaDataProperties.first(:class_name => name)
33
+ meta_data_properties_pairs = {}
34
+ if meta_data_properties
35
+ properties = meta_data_properties.keys.select {|k| k unless ignored_keys.include?(k)}
36
+ properties.each do |key|
37
+ meta_data_properties_pairs[key] = meta_data_properties.send(key.to_sym)
38
+ end
39
+ end
40
+ meta_data_properties_pairs
41
+ end
42
+
43
+ def ignored_keys
44
+ ['_id', 'class_name']
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,33 @@
1
+ module MongoHashie
2
+ module Rails
3
+ def create(hash = {}); self.new(hash).save; end
4
+
5
+ def all(options = {})
6
+ self.new.collection.find.collect {|doc| self.new(doc)}
7
+ end
8
+
9
+ def first(options = {})
10
+ result = self.new.collection.find_one(options)
11
+ self.new(result) if result
12
+ end
13
+
14
+ def find(*args)
15
+ if args.first.is_a?(Hash)
16
+ result = self.new.collection.find(args.first)
17
+ else
18
+ result = self.new.collection.find('_id' => Mongo::ObjectID.from_string(args.first))
19
+ end
20
+ result.count == 1 ? result.collect {|doc| self.new(doc)}.first : result.collect {|doc| self.new(doc)}
21
+ end
22
+
23
+ def count
24
+ self.new.collection.count
25
+ end
26
+
27
+ def destroy_all
28
+ target = self.new
29
+ target.collection.remove
30
+ target.reset_keys
31
+ end
32
+ end
33
+ end
data/lib/mongo-hashie.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require "mongo"
2
2
  require "hashie"
3
+ require 'mongo-hashie/rails'
4
+ require 'mongo-hashie/meta_data'
5
+ require 'mongo-hashie/meta_data'
3
6
  require 'mongo-hashie/base'
4
-
data/mongo-hashie.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongo-hashie}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Oliver Kiessler"]
12
- s.date = %q{2009-12-12}
12
+ s.date = %q{2010-01-02}
13
13
  s.description = %q{Simple MongoDB Object Wrapper based on Hashie}
14
14
  s.email = %q{kiessler@inceedo.com}
15
15
  s.extra_rdoc_files = [
@@ -25,6 +25,8 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "lib/mongo-hashie.rb",
27
27
  "lib/mongo-hashie/base.rb",
28
+ "lib/mongo-hashie/meta_data.rb",
29
+ "lib/mongo-hashie/rails.rb",
28
30
  "mongo-hashie.gemspec",
29
31
  "test/helper.rb",
30
32
  "test/test_mongo-hashie.rb"
data/test/helper.rb CHANGED
@@ -8,3 +8,5 @@ require 'mongo-hashie'
8
8
 
9
9
  class Test::Unit::TestCase
10
10
  end
11
+
12
+ MongoHashie::Configuration.database = 'mongo-hashie-testdb'
@@ -1,9 +1,76 @@
1
1
  require 'helper'
2
2
 
3
+ class BlogPost < MongoHashie::Base
4
+ end
5
+
3
6
  class TestMongoHashie < Test::Unit::TestCase
4
- should "test" do
5
- # TODO
6
- assert true
7
+ def setup
8
+ BlogPost.destroy_all; MongoHashie::MetaDataProperties.destroy_all
9
+ @blog_post_hash = {:title => 'Test Title', :text => 'Bodytext', :author => 'Oliver Kiessler', :views => 1,
10
+ :tags => ['test1', 'test2', 'test3']}
7
11
  end
8
- end
12
+
13
+ context "basic operations" do
14
+ should "have a connection and database set" do
15
+ assert_equal MongoHashie::Configuration.database, 'mongo-hashie-testdb'
16
+ assert_not_nil BlogPost.connection
17
+ assert_not_nil BlogPost.db
18
+ end
19
+
20
+ should "create a new object" do
21
+ blog_post = BlogPost.new(@blog_post_hash)
22
+ assert_equal BlogPost.count, 0
23
+ blog_post.save
24
+ assert_not_nil blog_post._id
25
+ assert_equal BlogPost.count, 1
26
+ end
27
+
28
+ should "destroy object" do
29
+ blog_post = BlogPost.new(@blog_post_hash)
30
+ blog_post.save
31
+ assert_equal BlogPost.count, 1
32
+ blog_post.destroy
33
+ assert_equal BlogPost.count, 0
34
+ end
9
35
 
36
+ should "create object by passing a hash" do
37
+ assert_equal BlogPost.count, 0
38
+ blog_post = BlogPost.create(@blog_post_hash)
39
+ assert_equal BlogPost.count, 1
40
+ end
41
+ end
42
+
43
+ context "meta data" do
44
+ should "have meta data properties" do
45
+ assert_equal BlogPost.count, 0
46
+ assert_equal MongoHashie::MetaDataProperties.count, 0
47
+ blog_post = BlogPost.create(@blog_post_hash)
48
+ assert_equal BlogPost.count, 1
49
+ assert_equal MongoHashie::MetaDataProperties.count, 1
50
+ end
51
+
52
+ should "include all keys in the meta data properties" do
53
+ blog_post = BlogPost.create(@blog_post_hash)
54
+ assert_equal BlogPost.properties_used.keys.size, 5
55
+ assert BlogPost.properties_used.keys.include?('title')
56
+ assert BlogPost.properties_used.keys.include?('text')
57
+ assert BlogPost.properties_used.keys.include?('author')
58
+ assert BlogPost.properties_used.keys.include?('views')
59
+ assert BlogPost.properties_used.keys.include?('tags')
60
+ end
61
+
62
+ should "update keys in the meta data properties" do
63
+ BlogPost.create(@blog_post_hash)
64
+ assert_equal BlogPost.properties_used.keys.size, 5
65
+ BlogPost.create(@blog_post_hash.merge(:likes => 2))
66
+ assert_equal BlogPost.properties_used.keys.size, 6
67
+ end
68
+
69
+ should "reset keys in the meta data properties when destroy_all is called" do
70
+ 2.times {BlogPost.create(@blog_post_hash)}
71
+ assert_not_nil MongoHashie::MetaDataProperties.first(:class_name => 'BlogPost')
72
+ BlogPost.destroy_all
73
+ assert_nil MongoHashie::MetaDataProperties.first(:class_name => 'BlogPost')
74
+ end
75
+ end
76
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo-hashie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Kiessler
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-12 00:00:00 +01:00
12
+ date: 2010-01-02 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -60,6 +60,8 @@ files:
60
60
  - VERSION
61
61
  - lib/mongo-hashie.rb
62
62
  - lib/mongo-hashie/base.rb
63
+ - lib/mongo-hashie/meta_data.rb
64
+ - lib/mongo-hashie/rails.rb
63
65
  - mongo-hashie.gemspec
64
66
  - test/helper.rb
65
67
  - test/test_mongo-hashie.rb