trimodel 0.0.1 → 0.0.2

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.
@@ -0,0 +1,54 @@
1
+ require 'rails/generators'
2
+
3
+ module Trimodel
4
+ class DeleteGenerator < Rails::Generators::Base
5
+ desc "Delete an existing triple model relationship"
6
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
7
+
8
+ class_option :models, :type => :array, :desc => "The model triplet from where the association will be removed"
9
+
10
+ def check_parameters
11
+ if options[:models].class == NilClass
12
+ puts "error: You need to give the models you want to associate"
13
+ puts "e.g. rails g trimodel:new --models a b c"
14
+ elsif options[:models].size != 3
15
+ puts "error: Wrong number of models, they should be 3"
16
+ end
17
+ end
18
+
19
+ def rollback_migrations
20
+ list_and_perform_on_files options[:models][0], options[:models][1] { |f,a,b| rollback_migration(f,a,b) }
21
+ list_and_perform_on_files options[:models][1], options[:models][2] { |f,a,b| rollback_migration(f,a,b) }
22
+ end
23
+
24
+ def delete_migration_files
25
+ list_and_perform_on_files options[:models][0], options[:models][1] { |f,a,b| delete_migration_file(f,a,b) }
26
+ list_and_perform_on_files options[:models][1], options[:models][2] { |f,a,b| delete_migration_file(f,a,b) }
27
+ end
28
+
29
+ def delete_trimodel_file
30
+ File.delete(Rails.root + "config/initializers/trimodel.rb")
31
+ end
32
+
33
+ private
34
+ def list_and_perform_on_files model_a, model_b
35
+ migration_files = Dir.entries(Rails.root + "db/migrate")
36
+ for file in migration_files
37
+ yield(file, model_a, model_b)
38
+ end
39
+ end
40
+
41
+ def delete_migration_file file, model_a, model_b
42
+ if /_create_#{model_a.pluralize.downcase}_#{model_b.pluralize.downcase}_trimodel/ =~ file
43
+ full_path = Rails.root.to_s + "/db/migrate/" + file
44
+ File.delete(full_path)
45
+ end
46
+ end
47
+
48
+ def rollback_migration file, model_a, model_b
49
+ if /_create_#{model_a.pluralize.downcase}_#{model_b.pluralize.downcase}_trimodel/ =~ file
50
+ %x[rake db:migrate:down VERSION=#{file[0,14]}]
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,120 @@
1
+ require 'rails/generators'
2
+ require 'active_support/inflector'
3
+ require 'rake'
4
+
5
+ module Trimodel
6
+ class NewGenerator < Rails::Generators::Base
7
+ desc "Creates a new triple model relationship"
8
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
9
+
10
+ class_option :models, :type => :array, :desc => "The triplet model that will be associated"
11
+
12
+ def check_parameters
13
+ if options[:models].class == NilClass
14
+ puts "error: You need to give the models you want to associate"
15
+ puts "e.g. rails g trimodel:new --models a b c"
16
+ elsif options[:models].size != 3
17
+ puts "error: Wrong number of models, they should be 3"
18
+ end
19
+ end
20
+
21
+ def create_migration_files
22
+ create_migration_file options[:models][0], options[:models][1]
23
+ #need to wait so the timestamp has a different value
24
+ #and forms a correct migration file name
25
+ sleep(1)
26
+ create_migration_file options[:models][1], options[:models][2]
27
+ end
28
+
29
+ def perform_migrations
30
+ %x[rake db:migrate]
31
+ end
32
+
33
+ def create_trimodel_file
34
+ File.open(Rails.root + "config/initializers/trimodel.rb",
35
+ File::CREAT|File::RDWR) do |f|
36
+ add_one_asoc_to_model f, options[:models][0], options[:models][1], options[:models][2]
37
+ add_two_asocs_to_model f, options[:models][1], options[:models][0], options[:models][2]
38
+ add_one_asoc_to_model f, options[:models][2], options[:models][1], options[:models][0]
39
+ end
40
+ end
41
+
42
+ private
43
+ def add_one_asoc_to_model file, model_a, model_b, model_c
44
+ file.write(create_class_definition(model_a))
45
+ file.write(add_n_to_n_association(model_a, model_b))
46
+ file.write(add_iterator_method(model_b, model_c))
47
+ file.write(add_end)
48
+ end
49
+
50
+ def add_two_asocs_to_model file, model_a, model_b, model_c
51
+ file.write(create_class_definition(model_a))
52
+ file.write(add_n_to_n_association(model_a, model_b))
53
+ file.write(add_n_to_n_association(model_a, model_c))
54
+ file.write(add_end)
55
+ end
56
+
57
+ def create_class_definition model
58
+ "class #{model} < ActiveRecord::Base\n"
59
+ end
60
+
61
+ def add_n_to_n_association model_a, model_b
62
+ " has_and_belongs_to_many :#{model_b.pluralize.downcase}\n"
63
+ end
64
+
65
+ def add_iterator_method bridge_model, target_model
66
+ method_body=<<-eos
67
+
68
+ def #{target_model.pluralize.downcase}
69
+ records = []
70
+ self.#{bridge_model.pluralize.downcase}.each do |bm|
71
+ records << bm.#{target_model.pluralize.downcase}.map { |obj| obj }
72
+ end
73
+ records.flatten
74
+ end
75
+ eos
76
+ method_body
77
+ end
78
+
79
+ def add_end
80
+ "end\n\n"
81
+ end
82
+
83
+ def create_migration_file model_a, model_b
84
+ path = Rails.root.to_s
85
+ path << "/db/migrate/#{create_timestamp}_"
86
+ path << "create_#{model_a.pluralize.downcase}_"
87
+ path << "#{model_b.pluralize.downcase}_trimodel_join_table.rb"
88
+ File.open(path,File::CREAT|File::RDWR) do |fi|
89
+ fi.write(write_migration_code(model_a, model_b))
90
+ end
91
+ end
92
+
93
+ def create_timestamp
94
+ Time.now.utc.to_s.gsub('-','').gsub(':','').gsub(' ','')[0..-4]
95
+ end
96
+
97
+ def write_migration_code model_a, model_b
98
+ if (model_a.pluralize.downcase[0] < model_b.pluralize.downcase)
99
+ first, second = model_a.pluralize.downcase, model_b.pluralize.downcase
100
+ else
101
+ first, second = model_b.pluralize.downcase, model_a.pluralize.downcase
102
+ end
103
+ code=<<-eos
104
+ class Create#{model_a.pluralize}#{model_b.pluralize}TrimodelJoinTable < ActiveRecord::Migration
105
+ def self.up
106
+ create_table :#{first}_#{second}, :id => false do |t|
107
+ t.integer :#{model_a.downcase}_id
108
+ t.integer :#{model_b.downcase}_id
109
+ end
110
+ end
111
+
112
+ def self.down
113
+ drop_table :#{first}_#{second}
114
+ end
115
+ end
116
+ eos
117
+ code
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails'
2
+
3
+ module Trimodel
4
+ class Engine < Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Trimodel
2
+ require 'trimodel/engine' if defined?(Rails)
3
+ end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trimodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Angelos
8
+ - Angelos Kapsimanis
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-12 00:00:00.000000000 Z
12
+ date: 2013-03-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! " A gem that enables transparent relationships \n between
15
15
  2 models associated through a third one.\n"
@@ -18,7 +18,10 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
- - lib/trimodel.rb
21
+ - trimodel.rb
22
+ - lib/trimodel/engine.rb
23
+ - lib/generators/trimodel/new_generator.rb
24
+ - lib/generators/trimodel/delete_generator.rb
22
25
  homepage: https://github.com/akxs14/trimodel
23
26
  licenses: []
24
27
  post_install_message:
@@ -39,7 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
42
  version: '0'
40
43
  requirements: []
41
44
  rubyforge_project:
42
- rubygems_version: 1.8.24
45
+ rubygems_version: 1.8.25
43
46
  signing_key:
44
47
  specification_version: 3
45
48
  summary: A negotiator between 3 models
@@ -1,3 +0,0 @@
1
- class Trimodel
2
-
3
- end