dm-drupal 0.1.0 → 0.1.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,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
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,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_revisions'
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,23 @@
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
+ :nullable => false
11
+
12
+ property :mail, String,
13
+ :length => 64
14
+
15
+ has n, :nodes,
16
+ :child_key => [:uid],
17
+ :repository => Drupal::Repository
18
+
19
+ def profile
20
+ Drupal::Node.first :type => 'profile', :uid => uid
21
+ end
22
+ end
23
+ end
data/lib/drupal.rb ADDED
@@ -0,0 +1,48 @@
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
+
15
+ class PostHook
16
+ attr_accessor :cls, :proc
17
+
18
+ def load
19
+ cls.class_eval &proc
20
+ end
21
+
22
+ def initialize cls, proc
23
+ self.cls = cls
24
+ self.proc = proc
25
+ end
26
+ end
27
+
28
+ def self.hooks
29
+ @hooks||= []
30
+ @hooks
31
+ end
32
+
33
+ def self.hooks= hook
34
+ hooks
35
+ @hooks << hook
36
+ end
37
+ end
38
+
39
+ module DataMapper::Resource::ClassMethods
40
+ def post_drupal &blk
41
+ Drupal.hooks = Drupal::PostHook.new self, blk
42
+ end
43
+ end
44
+
45
+ $:<< File.expand_path(Pathname.new(__FILE__).dirname)
46
+ require 'drupal/user'
47
+ require 'drupal/node'
48
+ require 'drupal/cck'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-drupal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - quinn shanahan
@@ -22,7 +22,12 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
- - README
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
26
31
  has_rdoc: true
27
32
  homepage:
28
33
  licenses: []
data/README DELETED
@@ -1,19 +0,0 @@
1
- == DM Drupal ==
2
-
3
- Its a Datamapper wrapper for a drupal+cck database.
4
-
5
- lots of eval right now. sorry.
6
-
7
- == Using Hooks ==
8
-
9
- this is for associating with dynamic cck fields that don't exist when the app loads.
10
-
11
- class BlogPost
12
- include DataMapper::Resource
13
- post_drupal do
14
- has 1, :drupal_blog_post,
15
- :repository => repository(:drupal),
16
- :class_name => 'Drupal::BlogPost',
17
- :child_key => ['legacy_blog_post_id']
18
- end
19
- end