ruby-wpdb 1.2 → 1.2.1

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.
@@ -9,8 +9,8 @@ module WPDB
9
9
  class Comment < Sequel::Model(:"#{WPDB.prefix}comments")
10
10
  plugin :validation_helpers
11
11
 
12
- many_to_one :post, :key => :comment_post_ID, :class => :'WPDB::Post'
13
- one_to_many :commentmeta, :class => :'WPDB::CommentMeta'
12
+ many_to_one :post, key: :comment_post_ID, class: 'WPDB::Post'
13
+ one_to_many :commentmeta, class: 'WPDB::CommentMeta'
14
14
 
15
15
  def validate
16
16
  super
@@ -25,8 +25,4 @@ module WPDB
25
25
  super
26
26
  end
27
27
  end
28
-
29
- class CommentMeta < Sequel::Model(:"#{WPDB.prefix}commentmeta")
30
- many_to_one :comment, :class => :'WPDB::Comment'
31
- end
32
28
  end
@@ -0,0 +1,5 @@
1
+ module WPDB
2
+ class CommentMeta < Sequel::Model(:"#{WPDB.prefix}commentmeta")
3
+ many_to_one :comment, class: :'WPDB::Comment'
4
+ end
5
+ end
@@ -1,6 +1,6 @@
1
1
  module WPDB
2
2
  module Config
3
- class YAML
3
+ module ConfigFormat
4
4
  def initialize(file)
5
5
  if file.respond_to? :read
6
6
  @contents = file.read
@@ -11,10 +11,6 @@ module WPDB
11
11
  parse
12
12
  end
13
13
 
14
- def parse
15
- @config = ::YAML::load(@contents)
16
- end
17
-
18
14
  def config
19
15
  uri = 'mysql2://'
20
16
  uri += "#{@config['username']}:#{@config['password']}"
@@ -26,29 +22,64 @@ module WPDB
26
22
  end
27
23
  end
28
24
 
29
- class WPConfig
30
- def initialize(file)
31
- if file.respond_to? :read
32
- @contents = file.read
33
- else
34
- @contents = File.read(file)
35
- end
25
+ class YAML
26
+ include ConfigFormat
36
27
 
37
- parse
28
+ def parse
29
+ @config = ::YAML::load(ERB.new(@contents).result)
38
30
  end
31
+ end
32
+
33
+ class WPConfig
34
+ include ConfigFormat
39
35
 
40
36
  def parse
41
- @config = Hash[@contents.scan(/define\((?:'|")(.+)(?:'|"), *(?:'|")(.+)(?:'|")\)/)]
42
- @config['DB_PREFIX'] ||= 'wp_'
37
+ config = Hash[@contents.scan(/define\((?:'|")(.+)(?:'|"), *(?:'|")(.+)(?:'|")\)/)]
38
+ @config = {
39
+ "username" => config["DB_USER"],
40
+ "password" => config["DB_PASSWORD"],
41
+ "hostname" => config["DB_HOST"],
42
+ "database" => config["DB_NAME"],
43
+ "prefix" => config["DB_PREFIX"] || "wp_"
44
+ }
43
45
  end
46
+ end
44
47
 
45
- def config
46
- uri = 'mysql2://'
47
- uri += "#{@config['DB_USER']}:#{@config['DB_PASSWORD']}"
48
- uri += "@#{@config['DB_HOST']}"
49
- uri += "/#{@config['DB_NAME']}"
48
+ class AutoDiscover
49
+ def initialize(directory = Dir.pwd)
50
+ @directory = Pathname(directory)
51
+ end
52
+
53
+ def file
54
+ @file ||= files.map { |file| @directory + file }.find { |file| File.exist?(file) }
55
+ end
56
+
57
+ private
58
+ def files
59
+ ["wp-config.php", "../wp-config.php", "wp/wp-config.php", "wordpress/wp-config.php", "config.yml"]
60
+ end
61
+ end
50
62
 
51
- { uri: uri, prefix: @config['DB_PREFIX'] }
63
+ class AutoFormat
64
+ attr_reader :filename
65
+
66
+ def initialize(filename)
67
+ @filename = Pathname(filename)
68
+ end
69
+
70
+ def format
71
+ case filename.extname
72
+ when ".yml", ".yaml"
73
+ Config::YAML
74
+ when ".php"
75
+ Config::WPConfig
76
+ else
77
+ nil
78
+ end
79
+ end
80
+
81
+ def config
82
+ format.new(filename).config
52
83
  end
53
84
  end
54
85
  end
@@ -1,4 +1,5 @@
1
1
  require 'php_serialize'
2
+ require 'json'
2
3
  require 'csv'
3
4
 
4
5
  module WPDB
@@ -9,7 +10,11 @@ module WPDB
9
10
  one_to_one :meta, :class => :'WPDB::GravityForms::FormMeta'
10
11
 
11
12
  def fields
12
- display_meta = PHP.unserialize(meta.display_meta)
13
+ begin
14
+ display_meta = PHP.unserialize(meta.display_meta)
15
+ rescue TypeError
16
+ display_meta = JSON.parse(meta.display_meta)
17
+ end
13
18
  display_meta['fields']
14
19
  end
15
20
 
@@ -117,8 +122,10 @@ module WPDB
117
122
  end
118
123
 
119
124
  def sanitise_label(original_label)
125
+ @labels ||= []
126
+
120
127
  original_label = original_label.to_s
121
- return unless original_label.length > 0
128
+ return "" unless original_label.length > 0
122
129
 
123
130
  i = 1
124
131
 
@@ -159,7 +166,7 @@ module WPDB
159
166
 
160
167
  field_name = sanitise_label(field['label'])
161
168
 
162
- next if field_name.blank?
169
+ next if field_name.empty?
163
170
 
164
171
  dataset = dataset.select_append(:"ld#{i}__value___#{field_name}")
165
172
  end
@@ -202,6 +209,7 @@ module WPDB
202
209
  # use in a Ruby variable name/symbol.
203
210
  def self.underscoreize(string)
204
211
  string.downcase
212
+ .gsub(/ +/, ' ')
205
213
  .gsub(' ', '_')
206
214
  .gsub(/[^a-z0-9_]/, '')
207
215
  end
@@ -0,0 +1,16 @@
1
+ module WPDB
2
+ class Link < Sequel::Model(:"#{WPDB.prefix}links")
3
+ include Termable
4
+
5
+ one_to_many :termrelationships,
6
+ key: :object_id,
7
+ key_method: :obj_id,
8
+ class: 'WPDB::TermRelationship'
9
+
10
+ many_to_many :termtaxonomy,
11
+ left_key: :object_id,
12
+ right_key: :term_taxonomy_id,
13
+ join_table: "#{WPDB.prefix}term_relationships",
14
+ class: 'WPDB::TermTaxonomy'
15
+ end
16
+ end
@@ -18,7 +18,7 @@ module WPDB
18
18
  # @param [String] name The option_name to fetch
19
19
  # @return String
20
20
  def self.get_option(name)
21
- Option.where(:option_name => name).get(:option_value)
21
+ Option.where(option_name: name).get(:option_value)
22
22
  end
23
23
  end
24
24
  end
@@ -0,0 +1,66 @@
1
+ Sequel.inflections do |inflect|
2
+ # Unless we tell Sequel otherwise, it will try to inflect the singular
3
+ # of "postmeta" using the "data" -> "datum" rule, leaving us with the
4
+ # bizarre "postmetum".
5
+ inflect.uncountable 'postmetas'
6
+ end
7
+
8
+ module WPDB
9
+ class Post < Sequel::Model(:"#{WPDB.prefix}posts")
10
+ include Termable
11
+
12
+ plugin :validation_helpers
13
+ plugin :sluggable, source: :post_title, target: :post_name
14
+
15
+ one_to_many :children,
16
+ key: :post_parent,
17
+ class: self do |ds|
18
+ ds.where(post_type: ['attachment', 'revision']).invert
19
+ .where(post_parent: self.ID)
20
+ end
21
+ one_to_many :revisions,
22
+ key: :post_parent,
23
+ class: self,
24
+ conditions: { post_type: 'revision' }
25
+
26
+ one_to_many :attachments,
27
+ key: :post_parent,
28
+ class: self,
29
+ conditions: { post_type: 'attachment' }
30
+
31
+ # In order to use Sequel add_postmeta function.
32
+ one_to_many :postmetas, class: 'WPDB::PostMeta'
33
+ one_to_many :comments, key: :comment_post_ID, class: 'WPDB::Comment'
34
+ one_to_many :termrelationships,
35
+ key: :object_id,
36
+ key_method: :obj_id,
37
+ class: 'WPDB::TermRelationship'
38
+
39
+ many_to_one :parent, class: self, key: :post_parent
40
+ many_to_one :author, key: :post_author, class: 'WPDB::User'
41
+
42
+ many_to_many :termtaxonomy,
43
+ left_key: :object_id,
44
+ right_key: :term_taxonomy_id,
45
+ join_table: "#{WPDB.prefix}term_relationships",
46
+ class: 'WPDB::TermTaxonomy'
47
+
48
+ def validate
49
+ super
50
+ validates_presence [:post_title, :post_type, :post_status]
51
+ validates_unique :post_name
52
+ end
53
+
54
+ def before_validation
55
+ self.post_type ||= "post"
56
+ self.post_status ||= "draft"
57
+ self.post_parent ||= 0
58
+ self.menu_order ||= 0
59
+ self.comment_status ||= "open"
60
+ self.ping_status ||= WPDB::Option.get_option("default_ping_status")
61
+ self.post_date ||= Time.now
62
+ self.post_date_gmt ||= Time.now.utc
63
+ super
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,5 @@
1
+ module WPDB
2
+ class PostMeta < Sequel::Model(:"#{WPDB.prefix}postmeta")
3
+ many_to_one :post, class: 'WPDB::Post'
4
+ end
5
+ end
@@ -0,0 +1,41 @@
1
+ module WPDB
2
+ class Term < Sequel::Model(:"#{WPDB.prefix}terms")
3
+ plugin :validation_helpers
4
+ plugin :sluggable, source: :name, target: :slug
5
+
6
+ one_to_many :termtaxonomies, class: 'WPDB::TermTaxonomy'
7
+
8
+ def validate
9
+ super
10
+ validates_presence :name
11
+ end
12
+ end
13
+
14
+ module Termable
15
+ # For objects that have a relationship with termtaxonomies, this
16
+ # module can be mixed in and gives the ability to add a term
17
+ # directly to the model, rather than creating the relationship
18
+ # yourself. Used by Post and Link.
19
+ def add_term(term, taxonomy, description, count)
20
+ if term.respond_to?(:term_id)
21
+ term_id = term.term_id
22
+ else
23
+ term_id = term.to_i
24
+ end
25
+
26
+ term_taxonomy = WPDB::TermTaxonomy.where(term_id: term_id, taxonomy: taxonomy).first
27
+ unless term_taxonomy
28
+ term_taxonomy = WPDB::TermTaxonomy.create(
29
+ term_id: term_id,
30
+ taxonomy: taxonomy,
31
+ description: description,
32
+ count: count
33
+ )
34
+ else
35
+ term_taxonomy.count += count
36
+ end
37
+
38
+ add_termtaxonomy(term_taxonomy)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,12 @@
1
+ module WPDB
2
+ class TermRelationship < Sequel::Model(:"#{WPDB.prefix}term_relationships")
3
+ def_column_alias(:obj_id, :object_id)
4
+
5
+ many_to_one :post, class: 'WPDB::Post', key: :object_id
6
+ many_to_one :link, class: 'WPDB::Link', key: :object_id
7
+
8
+ many_to_one :termtaxonomy,
9
+ class: 'WPDB::TermTaxonomy',
10
+ key: :term_taxonomy_id
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module WPDB
2
+ class TermTaxonomy < Sequel::Model(:"#{WPDB.prefix}term_taxonomy")
3
+ one_to_many :termrelationships, class: 'WPDB::TermRelationship'
4
+
5
+ many_to_one :term, class: 'WPDB::Term'
6
+
7
+ many_to_many :posts,
8
+ left_key: :term_taxonomy_id,
9
+ right_key: :object_id,
10
+ join_table: "#{WPDB.prefix}term_relationships",
11
+ class: 'WPDB::Post'
12
+
13
+ many_to_many :links,
14
+ left_key: :term_taxonomy_id,
15
+ right_key: :object_id,
16
+ join_table: "#{WPDB.prefix}term_relationships",
17
+ class: 'WPDB::Link'
18
+ end
19
+ end
@@ -11,8 +11,8 @@ module WPDB
11
11
  class User < Sequel::Model(:"#{WPDB.user_prefix}users")
12
12
  plugin :validation_helpers
13
13
 
14
- one_to_many :usermeta, :class => :'WPDB::UserMeta'
15
- one_to_many :posts, :key => :post_author, :class => :'WPDB::Post'
14
+ one_to_many :usermeta, class: 'WPDB::UserMeta'
15
+ one_to_many :posts, key: :post_author, class: :'WPDB::Post'
16
16
 
17
17
  def validate
18
18
  super
@@ -33,7 +33,4 @@ module WPDB
33
33
  super
34
34
  end
35
35
  end
36
-
37
- class UserMeta < Sequel::Model(:"#{WPDB.user_prefix}usermeta")
38
- end
39
36
  end
@@ -0,0 +1,5 @@
1
+ module WPDB
2
+ class UserMeta < Sequel::Model(:"#{WPDB.user_prefix}usermeta")
3
+ many_to_one :user, class: "WPDB::User"
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module WPDB
2
- VERSION = "1.2"
2
+ VERSION = "1.2.1"
3
3
  end
data/lib/ruby-wpdb.rb CHANGED
@@ -7,25 +7,29 @@ require 'pathname'
7
7
  require_relative 'ruby-wpdb/config'
8
8
 
9
9
  module WPDB
10
+ class ConfigFileError < StandardError; end
11
+
10
12
  class << self
11
13
  attr_accessor :db, :prefix, :user_prefix, :initialized
12
14
 
13
15
  # Given the path to a YAML file, will initialise WPDB using the
14
16
  # config files found in that file.
15
17
  def from_config(file = nil)
16
- file ||= File.dirname(__FILE__) + '/../config.yml'
17
- file = Pathname(file)
18
-
19
- case file.extname
20
- when ".yml"
21
- config_file = Config::YAML.new(file)
22
- when ".php"
23
- config_file = Config::WPConfig.new(file)
24
- end
18
+ config_file = config_file(file)
25
19
 
26
20
  init(config_file.config[:uri], config_file.config[:prefix])
27
21
  end
28
22
 
23
+ def config_file(file = nil)
24
+ file = Config::AutoDiscover.new.file unless file
25
+ raise ConfigFileError, "No config file specified, and none found" unless file
26
+
27
+ file = Config::AutoFormat.new(file)
28
+ raise ConfigFileError, "Unknown config file format for file #{file}" unless file.format
29
+
30
+ file
31
+ end
32
+
29
33
  # Initialises Sequel, sets up some necessary variables (like
30
34
  # WordPress's table prefix), and then includes our models.
31
35
  #
@@ -40,16 +44,20 @@ module WPDB
40
44
  WPDB.prefix = prefix || 'wp_'
41
45
  WPDB.user_prefix = user_prefix || WPDB.prefix
42
46
 
43
- require_relative 'ruby-wpdb/options'
44
- require_relative 'ruby-wpdb/users'
45
- require_relative 'ruby-wpdb/terms'
46
- require_relative 'ruby-wpdb/posts'
47
- require_relative 'ruby-wpdb/comments'
48
- require_relative 'ruby-wpdb/links'
47
+ require_relative 'ruby-wpdb/option'
48
+ require_relative 'ruby-wpdb/user'
49
+ require_relative 'ruby-wpdb/usermeta'
50
+ require_relative 'ruby-wpdb/term'
51
+ require_relative 'ruby-wpdb/term_relationship'
52
+ require_relative 'ruby-wpdb/term_taxonomy'
53
+ require_relative 'ruby-wpdb/post'
54
+ require_relative 'ruby-wpdb/postmeta'
55
+ require_relative 'ruby-wpdb/comment'
56
+ require_relative 'ruby-wpdb/commentmeta'
57
+ require_relative 'ruby-wpdb/link'
49
58
  require_relative 'ruby-wpdb/gravityforms'
50
59
 
51
60
  WPDB.initialized = true
52
61
  end
53
62
  end
54
63
  end
55
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-wpdb
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.2'
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Miller
@@ -109,33 +109,117 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '10.1'
111
111
  - !ruby/object:Gem::Dependency
112
- name: letters
112
+ name: rubygems-tasks
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '0.4'
117
+ version: '0.2'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '0.4'
124
+ version: '0.2'
125
125
  - !ruby/object:Gem::Dependency
126
- name: rubygems-tasks
126
+ name: rspec
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - "~>"
130
130
  - !ruby/object:Gem::Version
131
- version: '0.2'
131
+ version: '2.14'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
- version: '0.2'
138
+ version: '2.14'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rack-test
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.6'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.6'
153
+ - !ruby/object:Gem::Dependency
154
+ name: guard
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '2.6'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '2.6'
167
+ - !ruby/object:Gem::Dependency
168
+ name: guard-rspec
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '4.2'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '4.2'
181
+ - !ruby/object:Gem::Dependency
182
+ name: guard-bundler
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '2.0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '2.0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: mutant
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: '0.5'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: '0.5'
209
+ - !ruby/object:Gem::Dependency
210
+ name: mutant-rspec
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: '0.5'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: '0.5'
139
223
  description: ruby-wpdb gives you a painless way to access and interact with WordPress
140
224
  from Ruby, accessing posts, tags, and all other WordPress concepts as plain-old
141
225
  Ruby objects.
@@ -152,25 +236,20 @@ files:
152
236
  - data/query.log
153
237
  - data/wp-config-sample.php
154
238
  - lib/ruby-wpdb.rb
155
- - lib/ruby-wpdb/comments.rb
239
+ - lib/ruby-wpdb/comment.rb
240
+ - lib/ruby-wpdb/commentmeta.rb
156
241
  - lib/ruby-wpdb/config.rb
157
242
  - lib/ruby-wpdb/gravityforms.rb
158
- - lib/ruby-wpdb/links.rb
159
- - lib/ruby-wpdb/options.rb
160
- - lib/ruby-wpdb/posts.rb
161
- - lib/ruby-wpdb/terms.rb
162
- - lib/ruby-wpdb/users.rb
243
+ - lib/ruby-wpdb/link.rb
244
+ - lib/ruby-wpdb/option.rb
245
+ - lib/ruby-wpdb/post.rb
246
+ - lib/ruby-wpdb/postmeta.rb
247
+ - lib/ruby-wpdb/term.rb
248
+ - lib/ruby-wpdb/term_relationship.rb
249
+ - lib/ruby-wpdb/term_taxonomy.rb
250
+ - lib/ruby-wpdb/user.rb
251
+ - lib/ruby-wpdb/usermeta.rb
163
252
  - lib/ruby-wpdb/version.rb
164
- - test/comments_test.rb
165
- - test/config_test.rb
166
- - test/gravityforms_test.rb
167
- - test/links_test.rb
168
- - test/options_test.rb
169
- - test/posts_test.rb
170
- - test/readme_test.rb
171
- - test/terms_test.rb
172
- - test/test_helper.rb
173
- - test/users_test.rb
174
253
  homepage: http://github.com/robmiller/ruby-wpdb
175
254
  licenses:
176
255
  - MIT
@@ -191,7 +270,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
270
  version: '0'
192
271
  requirements: []
193
272
  rubyforge_project:
194
- rubygems_version: 2.2.2
273
+ rubygems_version: 2.4.6
195
274
  signing_key:
196
275
  specification_version: 4
197
276
  summary: A Ruby ORM wrapper for WordPress
@@ -1,8 +0,0 @@
1
- module WPDB
2
- class Link < Sequel::Model(:"#{WPDB.prefix}links")
3
- include Termable
4
-
5
- one_to_many :termrelationships, :key => :object_id, :key_method => :obj_id, :class => 'WPDB::TermRelationship'
6
- many_to_many :termtaxonomy, :left_key => :object_id, :right_key => :term_taxonomy_id, :join_table => :"#{WPDB.prefix}term_relationships", :class => 'WPDB::TermTaxonomy'
7
- end
8
- end