dm-drupal 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,77 @@
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 ||= Drupal::Repository.adapter.query('select type from node').uniq
13
+ end
14
+
15
+ def self.fields
16
+ @fields ||= Drupal::Repository.adapter.query('show tables').select{|t| t.match(/^content_field/)}
17
+ end
18
+ end
19
+
20
+ class Table
21
+ attr_accessor :type
22
+
23
+ def initialize type
24
+ self.type = type
25
+ end
26
+
27
+ def table
28
+ "content_type_#{type}"
29
+ end
30
+
31
+ def fields
32
+ Drupal::CCK::ContentNodeFieldInstance.all :type_name => type
33
+ end
34
+
35
+ def columns
36
+ @columns ||= fields
37
+ end
38
+
39
+ def ignore_columns
40
+ %w{nid vid}
41
+ end
42
+
43
+ def to_s
44
+ "
45
+ class #{type.camel_case}
46
+ #{Drupal.common}
47
+ storage_names[:drupal] = '#{table}'
48
+ property :nid, Serial
49
+ property :vid, Integer
50
+ belongs_to :node,
51
+ :child_key => [:nid]
52
+ #{columns.map{|c| c.to_s}.join "\n"}
53
+ def self.type; :#{type}; end
54
+ extend CCK::FieldMethods
55
+ end
56
+ "
57
+ end
58
+
59
+ def valid?
60
+ columns
61
+ true
62
+ rescue MysqlError
63
+ false
64
+ end
65
+ end
66
+ end
67
+
68
+ def self.generate
69
+ Drupal.class_eval do
70
+ eval( CCK::Builder.fields.map do |field|
71
+ CCK::ThroughField.new field
72
+ end.join("\n") )
73
+
74
+ eval CCK::Builder.code
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,51 @@
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
+ belongs_to :node,
42
+ :class_name => 'Drupal::Node',
43
+ :child_key => [:nid]
44
+ Drupal::User
45
+ #{field_type}
46
+ end
47
+ "
48
+ end
49
+ end
50
+ end
51
+ end
data/lib/drupal/cck.rb ADDED
@@ -0,0 +1,78 @@
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 ContentNodeFieldInstance
19
+ eval Drupal.common
20
+ storage_names[:drupal] = 'content_node_field_instance'
21
+
22
+ property :field_name, String,
23
+ :length => 32, :key => true
24
+ property :type_name, String,
25
+ :length => 32, :key => true
26
+
27
+ def field
28
+ @field ||= ContentNodeField.first :field_name => field_name
29
+ end
30
+
31
+ def field_type
32
+ @field_type ||= field.type
33
+ end
34
+
35
+ def to_s
36
+ f = field_name.match(/^field_(.*)/)[1]
37
+ case field_type
38
+ when 'userreference'
39
+ if through?
40
+ r = "has 1, :#{f},
41
+ :class_name => Drupal::#{f.camel_case},
42
+ :child_key => [:#{field_name}_uid]"
43
+ else
44
+ r = "belongs_to :#{field_name},
45
+ :class_name => 'Drupal::User'"
46
+ end
47
+ # when 'nodereference'
48
+ # r = "belongs_to :#{field_name},
49
+ # :class_name => 'Drupal::Node',
50
+ # :child_key => [:#{field_name}_nid]"
51
+ # else
52
+ # r = "property :#{field_name}, #{type}"
53
+ # r += ", :field => '#{field_name}_value'"
54
+ end
55
+ r
56
+ end
57
+
58
+ def through?
59
+ false
60
+ end
61
+ end
62
+
63
+ class ContentNodeField
64
+ eval Drupal.common
65
+ storage_names[:drupal] = 'content_node_field'
66
+
67
+ property :type, String,
68
+ :length => 127
69
+ property :field_name, String,
70
+ :length => 32
71
+ end
72
+ end
73
+
74
+ Node.send :include, CCK::NodeMethods
75
+ end
76
+
77
+ require 'drupal/cck/fields.rb'
78
+ require 'drupal/cck/builder.rb'
@@ -0,0 +1,49 @@
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
+ after :save, :write_node_revision
13
+
14
+ def write_node_revision
15
+ find_or_init_node_revision.attributes = {
16
+ :nid => nid,
17
+ :vid => vid,
18
+ :uid => uid,
19
+ :title => title,
20
+ :body => '',
21
+ :teaser => '',
22
+ :log => ''
23
+ }
24
+ node_revision.save!
25
+ end
26
+
27
+ def find_or_init_node_revision
28
+ return node_revision if node_revision
29
+ @node_revision = Drupal::NodeRevision.new
30
+ end
31
+
32
+ def node_revision
33
+ @node_revision ||= Drupal::NodeRevision.get nid
34
+ end
35
+ end
36
+
37
+ class NodeRevision
38
+ eval Drupal.common
39
+ storage_names[:drupal] = 'node_revisions'
40
+
41
+ property :nid, Serial
42
+ property :vid, Integer
43
+ property :uid, Integer
44
+ property :title, String
45
+ property :body, Text
46
+ property :teaser, Text
47
+ property :log, Text
48
+ end
49
+ end
@@ -0,0 +1,18 @@
1
+ module Drupal
2
+ class User
3
+ eval Drupal.common
4
+ storage_names[:drupal] = 'users'
5
+
6
+ property :uid, Serial
7
+ property :name, String,
8
+ :length => 60,
9
+ :nullable => false
10
+ has n, :nodes,
11
+ :child_key => [:uid],
12
+ :repository => Drupal::Repository
13
+
14
+ def profile
15
+ Drupal::Node.first :type => 'profile', :uid => uid
16
+ end
17
+ end
18
+ end
data/lib/drupal.rb ADDED
@@ -0,0 +1,19 @@
1
+ module Drupal
2
+ RepositoryName = :drupal
3
+ Repository = repository(Drupal::RepositoryName)
4
+
5
+ def self.common
6
+ "
7
+ include DataMapper::Resource
8
+
9
+ def self.default_repository_name
10
+ Drupal::RepositoryName
11
+ end
12
+ "
13
+ end
14
+ end
15
+
16
+ $:<< File.expand_path(Pathname.new(__FILE__).dirname)
17
+ require 'drupal/user'
18
+ require 'drupal/node'
19
+ require 'drupal/cck'
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-drupal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - quinn shanahan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-16 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: q.shanahan@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/drupal/cck/builder.rb
26
+ - lib/drupal/cck/fields.rb
27
+ - lib/drupal/cck.rb
28
+ - lib/drupal/node.rb
29
+ - lib/drupal/user.rb
30
+ - lib/drupal.rb
31
+ has_rdoc: true
32
+ homepage:
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.5
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: drupal database wrapper for datamapper
59
+ test_files: []
60
+