dm-drupal2 0.0.3

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in drupal.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Clément Hussenot
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Data Mapper Drupal
2
+
3
+ Help you to migrate Drupal (php) to a Ruby web project ;)
4
+ credit : https://github.com/quinn/dm-drupal
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'dm-drupal2'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install dm-drupal2
19
+
20
+ ## Usage
21
+
22
+ require 'rubygems'
23
+ require 'active_record'
24
+ require 'data_mapper' # requires all the gems listed above
25
+
26
+ require 'dm-core'
27
+ require 'dm-drupal2'
28
+
29
+ puts "ready"
30
+
31
+ DataMapper.setup(:drupal, 'postgres://postgres:password@localhost/database')
32
+
33
+ Drupal::Node.all.each do |n|
34
+ puts "Title : #{n.title}"
35
+ end
36
+
37
+ Drupal::User.all.each do |u|
38
+ puts "User : #{u.name}"
39
+ # u.profile
40
+ # u.mail
41
+ # u.nodes
42
+ end
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
51
+
52
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/drupal.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/drupal/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Clément Hussenot"]
6
+ gem.email = ["Chussenot@gmail.com"]
7
+ gem.description = %q{A datamapper wrapper for a drupal database.. great for migrations - repackage original dm-drupal gem}
8
+ gem.summary = %q{}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "dm-drupal2"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Drupal::VERSION
17
+ end
data/lib/drupal.rb ADDED
@@ -0,0 +1,59 @@
1
+ require "drupal/version"
2
+
3
+ module Drupal
4
+ RepositoryName = :drupal
5
+ Repository = repository(Drupal::RepositoryName)
6
+
7
+ def self.common
8
+ "
9
+ include DataMapper::Resource
10
+
11
+ def self.default_repository_name
12
+ Drupal::RepositoryName
13
+ end
14
+ "
15
+ end
16
+
17
+ class PostHook
18
+ attr_accessor :cls, :proc
19
+
20
+ def load
21
+ cls.class_eval &proc
22
+ end
23
+
24
+ def initialize cls, proc
25
+ self.cls = cls
26
+ self.proc = proc
27
+ end
28
+ end
29
+
30
+ def self.hooks
31
+ @hooks||= []
32
+ @hooks
33
+ end
34
+
35
+ def self.hooks= hook
36
+ hooks
37
+ @hooks << hook
38
+ end
39
+ end
40
+
41
+ # have to change this.. something broke somewhere (is sad)
42
+ module DataMapper::Resource::DrupalHook
43
+ def self.included model
44
+ model.class_eval do
45
+ def self.post_drupal &blk
46
+ Drupal.hooks = Drupal::PostHook.new self, blk
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ # module DataMapper::Resource
53
+ # extend DataMapper::Resource::ClassMethods
54
+ # end
55
+
56
+ $:<< File.expand_path(Pathname.new(__FILE__).dirname)
57
+ require 'drupal/user'
58
+ require 'drupal/node'
59
+ require 'drupal/cck'
data/lib/drupal/cck.rb ADDED
@@ -0,0 +1,92 @@
1
+ module Drupal
2
+ module CCK
3
+ module NodeMethods
4
+ def cck_class
5
+ @cck_class ||= eval("Drupal::#{type.camel_case}")
6
+ end
7
+
8
+ def cck
9
+ @cck ||= cck_class.first(:nid => nid)
10
+ end
11
+
12
+ def find_or_create_cck
13
+ return cck if cck
14
+ @cck = cck_class.create! :vid => vid, :nid => nid
15
+ end
16
+ end
17
+
18
+ class ContentType
19
+ eval Drupal.common
20
+ storage_names[:drupal] = 'node_type'
21
+
22
+ property :content_type, String,
23
+ :key => true, :field => 'type'
24
+ end
25
+
26
+ class ContentNodeFieldInstance
27
+ eval Drupal.common
28
+ storage_names[:drupal] = 'content_node_field_instance'
29
+
30
+ property :field_name, String,
31
+ :length => 32, :key => true
32
+ property :content_type, String,
33
+ :length => 32, :key => true,
34
+ :field => 'type_name'
35
+
36
+ def field
37
+ @field ||= ContentNodeField.first :field_name => field_name
38
+ end
39
+
40
+ def field_type
41
+ @field_type ||= field.type
42
+ end
43
+
44
+ def to_s
45
+ f = field_name.match(/^field_(.*)/)[1]
46
+ case field_type
47
+ when 'userreference'
48
+ if through?
49
+ r = "has 1, :#{f},
50
+ :class_name => Drupal::#{f.camel_case},
51
+ :child_key => [:nid]
52
+
53
+ has 1, :#{f}_user,
54
+ :remote_name => :user,
55
+ :class_name => Drupal::User,
56
+ :child_key => [:nid],
57
+ :through => :#{f}"
58
+ else
59
+ r = "belongs_to :#{field_name},
60
+ :class_name => 'Drupal::User'"
61
+ end
62
+ when 'number_integer'
63
+ r = "property :#{f}, Integer,
64
+ :field => 'field_#{f}_value'"
65
+ when 'text'
66
+ r = "property :#{f}, Text,
67
+ :field => 'field_#{f}_value'"
68
+ end
69
+ r
70
+ end
71
+
72
+ def through?
73
+ Drupal::CCK::Builder.fields.include? "content_#{field_name}"
74
+ end
75
+ end
76
+
77
+ class ContentNodeField
78
+ eval Drupal.common
79
+ storage_names[:drupal] = 'content_node_field'
80
+
81
+ property :type, String,
82
+ :length => 127
83
+ property :field_name, String,
84
+ :length => 32
85
+ end
86
+ end
87
+
88
+ Node.send :include, CCK::NodeMethods
89
+ end
90
+
91
+ require 'drupal/cck/fields.rb'
92
+ require 'drupal/cck/builder.rb'
@@ -0,0 +1,95 @@
1
+ module Drupal
2
+ module CCK
3
+ class Builder
4
+ def self.code
5
+ types.
6
+ map{|t| table = Table.new t}.
7
+ select{|t| t.valid?}.
8
+ join("\n")
9
+ end
10
+
11
+ def self.types
12
+ @types ||= ContentType.all.map{|t| t.content_type }
13
+ end
14
+
15
+ def self.fields
16
+ @fields ||= Drupal::Repository.adapter.query('show tables').select{|t| t.match(/^content_field/)}
17
+ end
18
+
19
+ module InstanceMethods
20
+ def generate_node
21
+ return node if node
22
+ self.node = Node.create! :type => content_type
23
+ self.vid = node.vid
24
+ save!
25
+ self.vid = node.vid
26
+ save!
27
+ end
28
+ end
29
+ end
30
+
31
+ class Table
32
+ attr_accessor :content_type
33
+
34
+ def initialize content_type
35
+ self.content_type = content_type
36
+ end
37
+
38
+ def table
39
+ "content_type_#{content_type}"
40
+ end
41
+
42
+ def fields
43
+ Drupal::CCK::ContentNodeFieldInstance.all :content_type => content_type
44
+ end
45
+
46
+ def columns
47
+ @columns ||= fields
48
+ end
49
+
50
+ def ignore_columns
51
+ %w{nid vid}
52
+ end
53
+
54
+ def to_s
55
+ "
56
+ class #{content_type.camel_case}
57
+ #{Drupal.common}
58
+ storage_names[:drupal] = '#{table}'
59
+ property :nid, Integer
60
+ property :vid, Integer, :key => true
61
+ belongs_to :node,
62
+ :child_key => [:nid]
63
+ #{columns.map{|c| c.to_s}.join "\n"}
64
+ def self.content_type; :#{content_type}; end
65
+ def content_type; :#{content_type}; end
66
+
67
+ extend CCK::FieldMethods
68
+ include CCK::Builder::InstanceMethods
69
+ end
70
+ "
71
+ end
72
+
73
+ def valid?
74
+ columns
75
+ true
76
+ rescue MysqlError
77
+ false
78
+ end
79
+ end
80
+ end
81
+
82
+ def self.generate
83
+ Drupal.class_eval do
84
+ eval( CCK::Builder.fields.map do |field|
85
+ CCK::ThroughField.new field
86
+ end.join("\n") )
87
+
88
+ eval CCK::Builder.code
89
+
90
+ Drupal.hooks.each do |hook|
91
+ hook.load
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,52 @@
1
+ module Drupal
2
+ module CCK
3
+ module FieldMethods
4
+ def fields
5
+ Drupal::CCK::ContentNodeFieldInstance.all :type_name => type
6
+ end
7
+ end
8
+
9
+ class ThroughField
10
+ attr_accessor :field_name, :table
11
+
12
+ def initialize field
13
+ self.table = field
14
+ self.field_name = field.match(/^content_field_(.*)/)[1]
15
+ end
16
+
17
+ def field
18
+ @field ||= ContentNodeField.first :field_name => "field_#{field_name}"
19
+ end
20
+
21
+ def valid?
22
+ !field.nil?
23
+ end
24
+
25
+ def field_type
26
+ case field.type
27
+ when 'userreference'
28
+ "belongs_to :user,
29
+ :class_name => 'Drupal::User',
30
+ :child_key => [:#{field.field_name}_uid]"
31
+ end
32
+ end
33
+
34
+ def to_s
35
+ return "" unless valid?
36
+ "
37
+ class #{field_name.camel_case}
38
+ #{Drupal.common}
39
+ storage_names[:drupal] = '#{table}'
40
+
41
+ property :vid, Integer, :key => true
42
+ belongs_to :node,
43
+ :class_name => 'Drupal::Node',
44
+ :child_key => [:nid]
45
+ Drupal::User
46
+ #{field_type}
47
+ end
48
+ "
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,58 @@
1
+ module Drupal
2
+ class Node
3
+ eval Drupal.common
4
+ storage_names[:drupal] = 'node'
5
+
6
+ property :nid, Serial
7
+ property :vid, Integer
8
+ property :uid, Integer
9
+ property :type, String
10
+ property :title, String
11
+
12
+ before :save, :assign_vid
13
+ after :save, :write_node_revision
14
+
15
+ def assign_vid
16
+ return true unless vid.nil?
17
+ self.vid ||= Drupal::Node.all.last.nid + 1
18
+ save!
19
+ self.vid = nid
20
+ save!
21
+ end
22
+
23
+ def write_node_revision
24
+ find_or_init_node_revision.attributes = {
25
+ :nid => nid,
26
+ :vid => vid,
27
+ :uid => uid,
28
+ :title => title,
29
+ :body => '',
30
+ :teaser => '',
31
+ :log => ''
32
+ }
33
+ node_revision.save!
34
+ end
35
+
36
+ def find_or_init_node_revision
37
+ return node_revision if node_revision
38
+ @node_revision = Drupal::NodeRevision.new
39
+ end
40
+
41
+ def node_revision
42
+ @node_revision ||= Drupal::NodeRevision.get vid
43
+ end
44
+ end
45
+
46
+ class NodeRevision
47
+ eval Drupal.common
48
+ storage_names[:drupal] = 'node_revision'
49
+
50
+ property :vid, Serial
51
+ property :nid, Integer
52
+ property :uid, Integer
53
+ property :title, String
54
+ property :body, Text
55
+ property :teaser, Text
56
+ property :log, Text
57
+ end
58
+ end
@@ -0,0 +1,28 @@
1
+ module Drupal
2
+ class User
3
+ eval Drupal.common
4
+ storage_names[:drupal] = 'users'
5
+
6
+ property :uid, Serial
7
+
8
+ property :name, String,
9
+ :length => 60,
10
+ :required => true
11
+
12
+ property :mail, String,
13
+ :length => 64
14
+
15
+ property :pass, String
16
+
17
+ has n, :nodes,
18
+ :child_key => [:uid],
19
+ :repository => Drupal::Repository
20
+
21
+ def profile
22
+ Drupal::Node.first :type => 'profile', :uid => uid
23
+ end
24
+
25
+ property :created, Date
26
+ def created_at ; Time.at(self[:created]) ; end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module Drupal
2
+ VERSION = "0.0.3"
3
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-drupal2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Clément Hussenot
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-07 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A datamapper wrapper for a drupal database.. great for migrations - repackage
15
+ original dm-drupal gem
16
+ email:
17
+ - Chussenot@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - drupal.gemspec
28
+ - lib/drupal.rb
29
+ - lib/drupal/cck.rb
30
+ - lib/drupal/cck/builder.rb
31
+ - lib/drupal/cck/fields.rb
32
+ - lib/drupal/node.rb
33
+ - lib/drupal/user.rb
34
+ - lib/drupal/version.rb
35
+ homepage: ''
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.21
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: ''
59
+ test_files: []