Module: WPDB

Defined in:
lib/ruby-wpdb.rb,
lib/ruby-wpdb/link.rb,
lib/ruby-wpdb/post.rb,
lib/ruby-wpdb/term.rb,
lib/ruby-wpdb/user.rb,
lib/ruby-wpdb/config.rb,
lib/ruby-wpdb/option.rb,
lib/ruby-wpdb/comment.rb,
lib/ruby-wpdb/version.rb,
lib/ruby-wpdb/postmeta.rb,
lib/ruby-wpdb/usermeta.rb,
lib/ruby-wpdb/commentmeta.rb,
lib/ruby-wpdb/gravityforms.rb,
lib/ruby-wpdb/term_taxonomy.rb,
lib/ruby-wpdb/term_relationship.rb

Defined Under Namespace

Modules: Config, GravityForms, Termable Classes: Comment, CommentMeta, ConfigFileError, Link, Option, Post, PostMeta, Term, TermRelationship, TermTaxonomy, User, UserMeta

Constant Summary collapse

VERSION =
"1.2.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dbObject

Returns the value of attribute db.



13
14
15
# File 'lib/ruby-wpdb.rb', line 13

def db
  @db
end

.initializedObject

Returns the value of attribute initialized.



13
14
15
# File 'lib/ruby-wpdb.rb', line 13

def initialized
  @initialized
end

.prefixObject

Returns the value of attribute prefix.



13
14
15
# File 'lib/ruby-wpdb.rb', line 13

def prefix
  @prefix
end

.user_prefixObject

Returns the value of attribute user_prefix.



13
14
15
# File 'lib/ruby-wpdb.rb', line 13

def user_prefix
  @user_prefix
end

Class Method Details

.camelize(string) ⇒ Object

Given a string, will convert it to a camel case suitable for use in a Ruby constant (which means no non-alphanumeric characters and no leading numbers).



200
201
202
203
204
205
206
# File 'lib/ruby-wpdb/gravityforms.rb', line 200

def self.camelize(string)
  string.gsub(/[^a-z0-9 ]/i, '')
    .gsub(/^[0-9]+/, '')
    .split(/\s+/)
    .map { |t| t.strip.capitalize }
    .join('')
end

.config_file(file = nil) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
# File 'lib/ruby-wpdb.rb', line 23

def config_file(file = nil)
  file = Config::AutoDiscover.new.file unless file
  raise ConfigFileError, "No config file specified, and none found" unless file

  file = Config::AutoFormat.new(file)
  raise ConfigFileError, "Unknown config file format for file #{file}" unless file.format

  file
end

.from_config(file = nil) ⇒ Object

Given the path to a YAML file, will initialise WPDB using the config files found in that file.



17
18
19
20
21
# File 'lib/ruby-wpdb.rb', line 17

def from_config(file = nil)
  config_file = config_file(file)

  init(config_file.config[:uri], config_file.config[:prefix])
end

.init(uri, prefix = nil, user_prefix = nil) ⇒ Object

Initialises Sequel, sets up some necessary variables (like WordPress’s table prefix), and then includes our models.

Parameters:

  • A (String)

    database connection uri, e.g. mysql2://user:pass@host:port/dbname

  • The (String)

    database table prefix used by the install of WordPress you’ll be querying. Defaults to wp_

  • The (String)

    prefix of the users table; if not specified, the general database table prefix will be used.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ruby-wpdb.rb', line 42

def init(uri, prefix = nil, user_prefix = nil)
  WPDB.db          = Sequel.connect(uri)
  WPDB.prefix      = prefix || 'wp_'
  WPDB.user_prefix = user_prefix || WPDB.prefix

  require_relative 'ruby-wpdb/option'
  require_relative 'ruby-wpdb/user'
  require_relative 'ruby-wpdb/usermeta'
  require_relative 'ruby-wpdb/term'
  require_relative 'ruby-wpdb/term_relationship'
  require_relative 'ruby-wpdb/term_taxonomy'
  require_relative 'ruby-wpdb/post'
  require_relative 'ruby-wpdb/postmeta'
  require_relative 'ruby-wpdb/comment'
  require_relative 'ruby-wpdb/commentmeta'
  require_relative 'ruby-wpdb/link'
  require_relative 'ruby-wpdb/gravityforms'

  WPDB.initialized = true
end

.underscoreize(string) ⇒ Object

Given a string, will convert it an_underscored_value suitable for use in a Ruby variable name/symbol.



210
211
212
213
214
215
# File 'lib/ruby-wpdb/gravityforms.rb', line 210

def self.underscoreize(string)
  string.downcase
    .gsub(/ +/, ' ')
    .gsub(' ', '_')
    .gsub(/[^a-z0-9_]/, '')
end