Module: WPDB

Defined in:
lib/ruby-wpdb.rb,
lib/ruby-wpdb/links.rb,
lib/ruby-wpdb/posts.rb,
lib/ruby-wpdb/terms.rb,
lib/ruby-wpdb/users.rb,
lib/ruby-wpdb/config.rb,
lib/ruby-wpdb/options.rb,
lib/ruby-wpdb/version.rb,
lib/ruby-wpdb/comments.rb,
lib/ruby-wpdb/gravityforms.rb

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"1.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dbObject

Returns the value of attribute db.



11
12
13
# File 'lib/ruby-wpdb.rb', line 11

def db
  @db
end

.initializedObject

Returns the value of attribute initialized.



11
12
13
# File 'lib/ruby-wpdb.rb', line 11

def initialized
  @initialized
end

.prefixObject

Returns the value of attribute prefix.



11
12
13
# File 'lib/ruby-wpdb.rb', line 11

def prefix
  @prefix
end

.user_prefixObject

Returns the value of attribute user_prefix.



11
12
13
# File 'lib/ruby-wpdb.rb', line 11

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).



193
194
195
196
197
198
199
# File 'lib/ruby-wpdb/gravityforms.rb', line 193

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

.from_config(file = nil) ⇒ Object

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



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby-wpdb.rb', line 15

def from_config(file = nil)
  file ||= File.dirname(__FILE__) + '/../config.yml'
  file = Pathname(file)

  case file.extname
  when ".yml"
    config_file = Config::YAML.new(file)
  when ".php"
    config_file = Config::WPConfig.new(file)
  end

  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.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby-wpdb.rb', line 38

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/options'
  require_relative 'ruby-wpdb/users'
  require_relative 'ruby-wpdb/terms'
  require_relative 'ruby-wpdb/posts'
  require_relative 'ruby-wpdb/comments'
  require_relative 'ruby-wpdb/links'
  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.



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

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