ruby-wpdb 1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +8 -0
- data/README.md +97 -0
- data/data/query.log +26431 -0
- data/lib/ruby-wpdb/comments.rb +32 -0
- data/lib/ruby-wpdb/gravityforms.rb +208 -0
- data/lib/ruby-wpdb/links.rb +8 -0
- data/lib/ruby-wpdb/options.rb +24 -0
- data/lib/ruby-wpdb/posts.rb +67 -0
- data/lib/ruby-wpdb/terms.rb +49 -0
- data/lib/ruby-wpdb/users.rb +39 -0
- data/lib/ruby-wpdb/version.rb +3 -0
- data/lib/ruby-wpdb.rb +51 -0
- data/test/comments_test.rb +40 -0
- data/test/gravityforms_test.rb +25 -0
- data/test/links_test.rb +28 -0
- data/test/options_test.rb +26 -0
- data/test/posts_test.rb +50 -0
- data/test/readme_test.rb +43 -0
- data/test/terms_test.rb +42 -0
- data/test/test_helper.rb +17 -0
- data/test/users_test.rb +47 -0
- metadata +180 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7234d071be7925efb8312943dd3a3813ca1ae342
|
4
|
+
data.tar.gz: 6ba546571d37c68fb1a5b68072bf389b408baf43
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 239cf24063e193ba4aece7d3d35db8fd554cc442f97962ca341ef02b0e60e5f77b99048899475d304c3dc79ff989dad8ba48e6fc070e8938874f45e3308c27eb
|
7
|
+
data.tar.gz: 6a08abb6db5e4a0c6331c188e539d4e51b05347103b94c3b4b32c0039ad89608c354b190a8851698e8eed28d47ab2776e2d41468e7941d3106aaeab9d20814ba
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
Copyright (c) 2011 Rob Miller
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
8
|
+
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# ruby-wpdb
|
2
|
+
|
3
|
+
WordPress is enormously popular; it powers some 20% of the web. Rare is
|
4
|
+
the web developer who doesn't come into contact with it, even those who
|
5
|
+
develop primarily in other languages. It's user-friendly, server
|
6
|
+
specification-friendly, and ubiquitous.
|
7
|
+
|
8
|
+
But what if you need to interact with WordPress from the outside? It
|
9
|
+
might be a cron-job, it might be a little command-line tool; you might
|
10
|
+
want to export or import some content, or you might want to clean some
|
11
|
+
things up. You don't have to do that in... *PHP*, do you?
|
12
|
+
|
13
|
+
If you'd like to do it in Ruby instead, then this library might help
|
14
|
+
with some of the boilerplate.
|
15
|
+
|
16
|
+
It's a wrapper for the WordPress database, using
|
17
|
+
[Sequel](http://sequel.rubyforge.org/), that gives you access to the
|
18
|
+
WordPress database and all its content via a nice ORM.
|
19
|
+
|
20
|
+
So, you can do simple things like get the five most recent posts:
|
21
|
+
|
22
|
+
WPDB::Post.reverse_order(:post_date).limit(5).all
|
23
|
+
|
24
|
+
Or more complicated things, like get the value of the custom field with
|
25
|
+
the key "image", but only for the first post whose title consists solely
|
26
|
+
of letters:
|
27
|
+
|
28
|
+
WPDB::Post.first(:post_title => /^[a-z]+$/)
|
29
|
+
.postmeta_dataset.first(:meta_key => 'image')
|
30
|
+
.meta_value
|
31
|
+
|
32
|
+
Of course, you're not limited to retrieving records, you can create them
|
33
|
+
too:
|
34
|
+
|
35
|
+
post = WPDB::Post.create(:post_title => 'Test', :post_content => 'Testing, testing, 123')
|
36
|
+
|
37
|
+
And ruby-wpdb knows all about the relationship between things in
|
38
|
+
WordPress — so if you want to create a new user, a post by that user,
|
39
|
+
and a tag for that post, you can do so by referring to the objects alone
|
40
|
+
without needing to know or care about what the actual relationships are
|
41
|
+
from the perspective of the database:
|
42
|
+
|
43
|
+
author = WPDB::User.create(
|
44
|
+
:user_login => 'fred',
|
45
|
+
:user_email => 'fred@example.com'
|
46
|
+
)
|
47
|
+
|
48
|
+
term = WPDB::Term.create(:name => 'Fred Stuff')
|
49
|
+
|
50
|
+
post = WPDB::Post.create(
|
51
|
+
:post_title => 'Hello from Fred',
|
52
|
+
:post_content => 'Hello, world',
|
53
|
+
:author => author
|
54
|
+
).add_term(term, 'tag')
|
55
|
+
|
56
|
+
## GravityForms
|
57
|
+
|
58
|
+
GravityForms is a great system for easily creating forms to capture
|
59
|
+
data. But its flexibility can make querying and exporting entries
|
60
|
+
difficult; querying even just a single form can often result in having
|
61
|
+
to write hairy SQL queries with many self-joins.
|
62
|
+
|
63
|
+
ruby-wpdb gives you an easier insight into your forms, allowing you to
|
64
|
+
treat them as though they were models of any other kind.
|
65
|
+
|
66
|
+
To make this a bit more concrete, imagine you had a contact form on your
|
67
|
+
site called "Contact Form". It has four fields: "Name", "Email",
|
68
|
+
"Message", and "Enquiry type".
|
69
|
+
|
70
|
+
ruby-wpdb allows you to do things like get the latest five entries to
|
71
|
+
have selected "quote" as their enquiry type:
|
72
|
+
|
73
|
+
WPDB::GravityForms.ContactForm.where(:enquiry_type => 'quote').reverse_order(:date_created).limit(5).all
|
74
|
+
|
75
|
+
Or display the messages that have been sent since the start of 2013:
|
76
|
+
|
77
|
+
WPDB::GravityForms.ContactForm.where(:date_created >= Date.new(2013, 1, 1)).each do |entry|
|
78
|
+
puts "#{entry.enquiry_type} enquiry from #{entry.name} <#{entry.email}>\n"
|
79
|
+
puts entry.message
|
80
|
+
puts "---"
|
81
|
+
end
|
82
|
+
|
83
|
+
Note that you get access to all the fields of the GravityForm as though
|
84
|
+
they were first-class members of an actual model, allowing you to use
|
85
|
+
their values when filtering and ordering.
|
86
|
+
|
87
|
+
## Usage
|
88
|
+
|
89
|
+
Models map exactly to database names without the prefix, and dataset
|
90
|
+
properties map exactly to column names. So, if you're familiar with
|
91
|
+
WordPress's database structure, ruby-wpdb should come fairly easy.
|
92
|
+
|
93
|
+
So a post is `WPDB::Post`; a comment is `WPDB::Comment`; and so on.
|
94
|
+
|
95
|
+
Beyond that, you're limited with what you can do only by the
|
96
|
+
capabilities of Sequel; you can find out more in [their
|
97
|
+
README](http://sequel.rubyforge.org/rdoc/files/README_rdoc.html).
|