mongify 0.0.7 → 0.0.8

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/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.0.8 / 18 Jan 2011
2
+ * Added ability to embed as object on an embedded table
3
+ * Some bugs fixed
4
+ * Updated README with new table options
5
+ * Improved data conversion on processing
1
6
  == 0.0.7 / 17 Jan 2011
2
7
  * Added mysql2 as a dependency
3
8
  * Improved error message when configuration file not provided
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongify (0.0.7)
4
+ mongify (0.0.8)
5
5
  activerecord (>= 2.3.10)
6
6
  activesupport (>= 2.3.10)
7
7
  bson_ext (>= 1.1.5)
data/README.rdoc CHANGED
@@ -79,7 +79,13 @@ Creating a translation is pretty straight forward. It looks something like this:
79
79
  column "created_at", :datetime
80
80
  column "updated_at", :datetime
81
81
  end
82
-
82
+
83
+ table "preferences", :embed_in => :users, :as => :object do
84
+ column "id", :key
85
+ column "user_id", :integer, :references => "users"
86
+ column "notify_by_email", :boolean
87
+ end
88
+
83
89
  Save the file as <tt>"translation_file.rb"</tt> and run the command:
84
90
 
85
91
  mongify process translation_file.rb -c database.config
@@ -90,8 +96,15 @@ When dealing with a translation, there is a few options you can set
90
96
 
91
97
  === Table
92
98
 
93
- Tables have the ability to be embedded into another collection on durring processing by passing :embed_in => 'collection_name'.
94
- You can also specify :on option (while embedding) to specify which field you're linking the tables together on.
99
+ Table Options are as follow:
100
+ table "table_name" # Does a straight copy of the table
101
+ table "table_name", :embed_in => :users # Embeds table_name into users, assuming a user_id is present in table_name.
102
+ # This will also assume you want the table embedded as an array.
103
+ table "table_name", :embed_in => :users, :on => 'owner_id'# Embeds table_name into users, linking it via a owner_id
104
+ # This will also assume you want the table embedded as an array.
105
+ table "table_name", :embed_in => :users, :as => :object # Embeds table_name into users as a one to one relationship
106
+ # This also assumes you have a user_id present in table_name
107
+ <em>You can also specify both +:on+ and +:as+ options when embedding</em>
95
108
 
96
109
  === Columns
97
110
  Columns take a few more options:
@@ -122,7 +135,6 @@ More testing will be done once the basic functionality is done
122
135
 
123
136
  === while processing
124
137
 
125
- * Allow the ability to embed_table with a one to one relationship
126
138
  * Allow renaming tables
127
139
  * Allow renaming of columns
128
140
  * Allow deleting of columns
data/lib/mongify.rb CHANGED
@@ -2,6 +2,7 @@
2
2
  # Mongify's core functionality
3
3
  #
4
4
  require 'active_support/core_ext'
5
+ require 'active_record'
5
6
 
6
7
  require 'mongify/ui'
7
8
  require 'mongify/exceptions'
@@ -1,5 +1,3 @@
1
- require 'active_record'
2
-
3
1
  module Mongify
4
2
  module Database
5
3
  #
@@ -1,3 +1,4 @@
1
+ require 'active_record/connection_adapters/abstract/schema_definitions'
1
2
  module Mongify
2
3
  module Database
3
4
  #
@@ -23,10 +24,8 @@ module Mongify
23
24
  case type
24
25
  when :key
25
26
  {"pre_mongified_#{name}" => value}
26
- when :datetime
27
- {"#{name}" => value.blank? ? nil : value.to_time}
28
27
  else
29
- {"#{self.name}" => value}
28
+ {"#{name}" => type_cast(value)}
30
29
  end
31
30
  end
32
31
 
@@ -62,6 +61,24 @@ module Mongify
62
61
  private
63
62
  #######
64
63
 
64
+ def type_cast(value)
65
+ return nil if value.nil?
66
+ case type
67
+ when :string then value
68
+ when :text then value
69
+ when :integer then value.to_i rescue value ? 1 : 0
70
+ when :float then value.to_f
71
+ when :decimal then ActiveRecord::ConnectionAdapters::Column.value_to_decimal(value)
72
+ when :datetime then ActiveRecord::ConnectionAdapters::Column.string_to_time(value)
73
+ when :timestamp then ActiveRecord::ConnectionAdapters::Column.string_to_time(value)
74
+ when :time then ActiveRecord::ConnectionAdapters::Column.string_to_dummy_time(value)
75
+ when :date then ActiveRecord::ConnectionAdapters::Column.string_to_date(value)
76
+ when :binary then ActiveRecord::ConnectionAdapters::Column.binary_to_string(value)
77
+ when :boolean then ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
78
+ else value
79
+ end
80
+ end
81
+
65
82
  def key?
66
83
  self.type == :key
67
84
  end
@@ -8,10 +8,10 @@ module Mongify
8
8
  attr_accessor :name
9
9
  attr_reader :options, :columns
10
10
 
11
- def initialize(name, *args, &block)
11
+ def initialize(name, options={}, &block)
12
12
  @columns = []
13
13
  @column_lookup = {}
14
- @options = args.extract_options!.stringify_keys
14
+ @options = options.stringify_keys
15
15
  self.name = name
16
16
 
17
17
  self.instance_exec(&block) if block_given?
@@ -60,13 +60,23 @@ module Mongify
60
60
  @options['embed_in'].to_s unless @options['embed_in'].nil?
61
61
  end
62
62
 
63
+ def embed_as
64
+ return nil unless embed?
65
+ return 'object' if @options['as'].to_s.downcase == 'object'
66
+ 'array'
67
+ end
68
+
69
+ def embed_as_object?
70
+ embed_as == 'object'
71
+ end
72
+
63
73
  def embed?
64
74
  embed_in.present?
65
75
  end
66
76
 
67
77
  def embed_on
68
78
  return nil unless embed?
69
- (@options['on'] || "#{@options['embed_in'].singularize}_id").to_s
79
+ (@options['on'] || "#{@options['embed_in'].to_s.singularize}_id").to_s
70
80
  end
71
81
 
72
82
  #######
@@ -47,7 +47,8 @@ module Mongify
47
47
  row.delete(t.embed_on)
48
48
  row.merge!(fetch_reference_ids(t, row))
49
49
  row.delete('pre_mongified_id')
50
- no_sql_connection.update(t.embed_in, target_row['_id'], {'$addToSet' => {t.name => row}})
50
+ save_function_call = t.embed_as_object? ? '$set' : '$addToSet'
51
+ no_sql_connection.update(t.embed_in, target_row['_id'], {save_function_call => {t.name => row}})
51
52
  end
52
53
  end
53
54
  end
@@ -1,3 +1,3 @@
1
1
  module Mongify
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -24,3 +24,12 @@ table "comments", :embed_in => :posts, :on => :post_id do
24
24
  column "created_at", :datetime
25
25
  column "updated_at", :datetime
26
26
  end
27
+
28
+ table "preferences", :embed_in => :users, :as => :object do
29
+ column "id", :key
30
+ column "user_id", :integer, :references => "users"
31
+ column "notify_by_email", :boolean
32
+ column "created_at", :datetime
33
+ column "updated_at", :datetime
34
+ end
35
+
@@ -94,13 +94,21 @@ describe Mongify::Database::Column do
94
94
  context "datetime" do
95
95
  it "should return a datetime format" do
96
96
  @column = Mongify::Database::Column.new('created_at', :datetime)
97
- @column.translate('2011-01-14 21:23:39').should == {'created_at' => Time.utc(2011, 01, 14, 21, 23,39)}
97
+ @column.translate('2011-01-14 21:23:39').should == {'created_at' => Time.local(2011, 01, 14, 21, 23,39)}
98
98
  end
99
99
  it "should return nil if input is nil" do
100
100
  @column = Mongify::Database::Column.new('created_at', :datetime)
101
101
  @column.translate(nil).should == {'created_at' => nil}
102
102
  end
103
103
  end
104
+ context :integer do
105
+ before(:each) do
106
+ @column = Mongify::Database::Column.new('account_id', :integer)
107
+ end
108
+ it "should return 10" do
109
+ @column.translate("10").should == {'account_id' => 10}
110
+ end
111
+ end
104
112
 
105
113
  it "should return pre_mongified_id when type is a key" do
106
114
  @column = Mongify::Database::Column.new('id', :key)
@@ -68,7 +68,7 @@ describe Mongify::Database::SqlConnection do
68
68
 
69
69
  context "tables" do
70
70
  it "should be able to get a list" do
71
- @sql_connection.tables.sort.should == ['comments', 'posts', 'users'].sort
71
+ @sql_connection.tables.sort.should == ['comments', 'posts', 'preferences', 'users'].sort
72
72
  end
73
73
  end
74
74
 
@@ -105,6 +105,28 @@ describe Mongify::Database::Table do
105
105
  Mongify::Database::Table.new('users').embed_in.should be_nil
106
106
  end
107
107
  end
108
+ context "embed_as" do
109
+ it "should return nil if it's not an embed table" do
110
+ Mongify::Database::Table.new('comments', :as => 'array').embed_as.should be_nil
111
+ end
112
+ it "should default to :array" do
113
+ Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id').embed_as.should == 'array'
114
+ end
115
+ it "should allow Array as a value" do
116
+ Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id', :as => :array).embed_as.should == 'array'
117
+ end
118
+ it "should allow Object as a value" do
119
+ Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id', :as => :object).embed_as.should == 'object'
120
+ end
121
+ context "embed_as_object?" do
122
+ it "should be true" do
123
+ Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id', :as => :object).should be_embed_as_object
124
+ end
125
+ it "should be false" do
126
+ Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id', :as => :array).should_not be_embed_as_object
127
+ end
128
+ end
129
+ end
108
130
  context "embed?" do
109
131
  it "should be true" do
110
132
  Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id').should be_embed
@@ -78,7 +78,7 @@ describe Mongify::Translation::Process do
78
78
  context "copy_embed_tables" do
79
79
  before(:each) do
80
80
  @target_table = mock(:name => 'posts', :embed? => false)
81
- @embed_table = mock(:translate => {}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts')
81
+ @embed_table = mock(:translate => {}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => false)
82
82
  @no_sql_connection.stub(:find_one).and_return({'_id' => 500})
83
83
  @translation.stub(:tables).and_return([@target_table, @embed_table])
84
84
  @translation.stub(:fetch_reference_ids).and_return({})
@@ -90,17 +90,23 @@ describe Mongify::Translation::Process do
90
90
  @translation.send(:copy_embedded_tables)
91
91
  end
92
92
  it "should remove the pre_mongified_id before embedding" do
93
- @embed_table = mock(:translate => {'first_name' => 'bob', 'pre_mongified_id' => 1}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts')
93
+ @embed_table = mock(:translate => {'first_name' => 'bob', 'pre_mongified_id' => 1}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => false)
94
94
  @translation.stub(:tables).and_return([@target_table, @embed_table])
95
95
  @no_sql_connection.should_receive(:update).with("posts", 500, {"$addToSet"=>{"comments"=>{'first_name' => 'bob'}}})
96
96
  @translation.send(:copy_embedded_tables)
97
97
  end
98
98
  it "should remove the parent_id from the embedding row" do
99
- @embed_table = mock(:translate => {'first_name' => 'bob', 'post_id' => 1}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts')
99
+ @embed_table = mock(:translate => {'first_name' => 'bob', 'post_id' => 1}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => false)
100
100
  @translation.stub(:tables).and_return([@target_table, @embed_table])
101
101
  @no_sql_connection.should_receive(:update).with("posts", 500, {"$addToSet"=>{"comments"=>{'first_name' => 'bob'}}})
102
102
  @translation.send(:copy_embedded_tables)
103
103
  end
104
+ it "should call $addToSet on update of an embed_as_object table" do
105
+ @embed_table = mock(:translate => {'first_name' => 'bob', 'post_id' => 1}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => true)
106
+ @translation.stub(:tables).and_return([@target_table, @embed_table])
107
+ @no_sql_connection.should_receive(:update).with("posts", 500, {"$set"=>{"comments"=>{'first_name' => 'bob'}}})
108
+ @translation.send(:copy_embedded_tables)
109
+ end
104
110
  end
105
111
 
106
112
  context "update_reference_ids" do
@@ -35,8 +35,8 @@ describe Mongify::Translation do
35
35
 
36
36
  context "parsed content" do
37
37
  context "tables" do
38
- it "should have 3 tables" do
39
- @translation.should have(3).tables
38
+ it "should have 4 tables" do
39
+ @translation.should have(4).tables
40
40
  end
41
41
 
42
42
  it "should setup 'comments'" do
@@ -25,3 +25,11 @@ table "comments" do
25
25
  column "updated_at", :datetime
26
26
  end
27
27
 
28
+ table "preferences" do
29
+ column "id", :key
30
+ column "user_id", :integer, :references => "users"
31
+ column "notify_by_email", :boolean
32
+ column "created_at", :datetime
33
+ column "updated_at", :datetime
34
+ end
35
+
@@ -1,4 +1,3 @@
1
- require 'active_record'
2
1
  class GenerateDatabase
3
2
  def self.mysql_connection
4
3
  @sql_connection ||= Mongify::Database::SqlConnection.new( :adapter => CONNECTION_CONFIG.mysql['adapter'],
@@ -40,6 +39,12 @@ class GenerateDatabase
40
39
  t.timestamps
41
40
  end
42
41
 
42
+ conn.create_table(:preferences) do |t|
43
+ t.integer :user_id
44
+ t.boolean :notify_by_email
45
+ t.timestamps
46
+ end
47
+
43
48
  if include_data
44
49
 
45
50
  #Users
@@ -71,6 +76,15 @@ class GenerateDatabase
71
76
  VALUES ('#{v[:body]}', #{v[:post_id]}, #{v[:user_id]}, '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
72
77
  end
73
78
 
79
+ [
80
+ {:user_id => 1, :notify_by_email => true},
81
+ {:user_id => 2, :notify_by_email => true},
82
+ {:user_id => 3, :notify_by_email => false}
83
+ ].each do |v|
84
+ conn.insert("INSERT INTO preferences (user_id, notify_by_email, created_at, updated_at)
85
+ VALUES (#{v[:user_id]}, '#{v[:notify_by_email]}', '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
86
+ end
87
+
74
88
  end
75
89
 
76
90
  sqlite_connection.database
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongify
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Kalek
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-17 00:00:00 -05:00
18
+ date: 2011-01-18 00:00:00 -05:00
19
19
  default_executable: mongify
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency