jrun-couchrest 0.2.1.1 → 0.12.6
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.
- data/README.md +6 -31
- data/Rakefile +1 -4
- data/examples/model/example.rb +13 -19
- data/lib/couchrest/core/database.rb +6 -8
- data/lib/couchrest/core/document.rb +45 -40
- data/lib/couchrest/core/model.rb +615 -0
- data/lib/couchrest/core/server.rb +1 -1
- data/lib/couchrest/monkeypatches.rb +48 -68
- data/lib/couchrest.rb +8 -54
- data/spec/couchrest/core/database_spec.rb +26 -31
- data/spec/couchrest/core/document_spec.rb +1 -1
- data/spec/couchrest/core/model_spec.rb +855 -0
- data/spec/spec_helper.rb +1 -6
- metadata +3 -56
- data/lib/couchrest/core/response.rb +0 -16
- data/lib/couchrest/mixins/attachments.rb +0 -31
- data/lib/couchrest/mixins/callbacks.rb +0 -483
- data/lib/couchrest/mixins/design_doc.rb +0 -64
- data/lib/couchrest/mixins/document_queries.rb +0 -48
- data/lib/couchrest/mixins/extended_attachments.rb +0 -68
- data/lib/couchrest/mixins/extended_document_mixins.rb +0 -6
- data/lib/couchrest/mixins/properties.rb +0 -125
- data/lib/couchrest/mixins/validation.rb +0 -234
- data/lib/couchrest/mixins/views.rb +0 -168
- data/lib/couchrest/mixins.rb +0 -4
- data/lib/couchrest/more/casted_model.rb +0 -28
- data/lib/couchrest/more/extended_document.rb +0 -217
- data/lib/couchrest/more/property.rb +0 -40
- data/lib/couchrest/support/blank.rb +0 -42
- data/lib/couchrest/support/class.rb +0 -191
- data/lib/couchrest/validation/auto_validate.rb +0 -163
- data/lib/couchrest/validation/contextual_validators.rb +0 -78
- data/lib/couchrest/validation/validation_errors.rb +0 -118
- data/lib/couchrest/validation/validators/absent_field_validator.rb +0 -74
- data/lib/couchrest/validation/validators/confirmation_validator.rb +0 -99
- data/lib/couchrest/validation/validators/format_validator.rb +0 -117
- data/lib/couchrest/validation/validators/formats/email.rb +0 -66
- data/lib/couchrest/validation/validators/formats/url.rb +0 -43
- data/lib/couchrest/validation/validators/generic_validator.rb +0 -120
- data/lib/couchrest/validation/validators/length_validator.rb +0 -134
- data/lib/couchrest/validation/validators/method_validator.rb +0 -89
- data/lib/couchrest/validation/validators/numeric_validator.rb +0 -104
- data/lib/couchrest/validation/validators/required_field_validator.rb +0 -109
- data/spec/couchrest/core/server_spec.rb +0 -35
- data/spec/couchrest/more/casted_extended_doc_spec.rb +0 -40
- data/spec/couchrest/more/casted_model_spec.rb +0 -98
- data/spec/couchrest/more/extended_doc_attachment_spec.rb +0 -130
- data/spec/couchrest/more/extended_doc_spec.rb +0 -509
- data/spec/couchrest/more/extended_doc_view_spec.rb +0 -207
- data/spec/couchrest/more/property_spec.rb +0 -130
- data/spec/couchrest/support/class_spec.rb +0 -59
- data/spec/fixtures/more/article.rb +0 -34
- data/spec/fixtures/more/card.rb +0 -20
- data/spec/fixtures/more/course.rb +0 -14
- data/spec/fixtures/more/event.rb +0 -6
- data/spec/fixtures/more/invoice.rb +0 -17
- data/spec/fixtures/more/person.rb +0 -8
- data/spec/fixtures/more/question.rb +0 -6
- data/spec/fixtures/more/service.rb +0 -12
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
module CouchRest
|
|
2
|
-
module Mixins
|
|
3
|
-
module Views
|
|
4
|
-
|
|
5
|
-
def self.included(base)
|
|
6
|
-
base.extend(ClassMethods)
|
|
7
|
-
base.send(:class_inheritable_accessor, :design_doc)
|
|
8
|
-
base.send(:class_inheritable_accessor, :design_doc_slug_cache)
|
|
9
|
-
base.send(:class_inheritable_accessor, :design_doc_fresh)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
module ClassMethods
|
|
13
|
-
|
|
14
|
-
# Define a CouchDB view. The name of the view will be the concatenation
|
|
15
|
-
# of <tt>by</tt> and the keys joined by <tt>_and_</tt>
|
|
16
|
-
#
|
|
17
|
-
# ==== Example views:
|
|
18
|
-
#
|
|
19
|
-
# class Post
|
|
20
|
-
# # view with default options
|
|
21
|
-
# # query with Post.by_date
|
|
22
|
-
# view_by :date, :descending => true
|
|
23
|
-
#
|
|
24
|
-
# # view with compound sort-keys
|
|
25
|
-
# # query with Post.by_user_id_and_date
|
|
26
|
-
# view_by :user_id, :date
|
|
27
|
-
#
|
|
28
|
-
# # view with custom map/reduce functions
|
|
29
|
-
# # query with Post.by_tags :reduce => true
|
|
30
|
-
# view_by :tags,
|
|
31
|
-
# :map =>
|
|
32
|
-
# "function(doc) {
|
|
33
|
-
# if (doc['couchrest-type'] == 'Post' && doc.tags) {
|
|
34
|
-
# doc.tags.forEach(function(tag){
|
|
35
|
-
# emit(doc.tag, 1);
|
|
36
|
-
# });
|
|
37
|
-
# }
|
|
38
|
-
# }",
|
|
39
|
-
# :reduce =>
|
|
40
|
-
# "function(keys, values, rereduce) {
|
|
41
|
-
# return sum(values);
|
|
42
|
-
# }"
|
|
43
|
-
# end
|
|
44
|
-
#
|
|
45
|
-
# <tt>view_by :date</tt> will create a view defined by this Javascript
|
|
46
|
-
# function:
|
|
47
|
-
#
|
|
48
|
-
# function(doc) {
|
|
49
|
-
# if (doc['couchrest-type'] == 'Post' && doc.date) {
|
|
50
|
-
# emit(doc.date, null);
|
|
51
|
-
# }
|
|
52
|
-
# }
|
|
53
|
-
#
|
|
54
|
-
# It can be queried by calling <tt>Post.by_date</tt> which accepts all
|
|
55
|
-
# valid options for CouchRest::Database#view. In addition, calling with
|
|
56
|
-
# the <tt>:raw => true</tt> option will return the view rows
|
|
57
|
-
# themselves. By default <tt>Post.by_date</tt> will return the
|
|
58
|
-
# documents included in the generated view.
|
|
59
|
-
#
|
|
60
|
-
# CouchRest::Database#view options can be applied at view definition
|
|
61
|
-
# time as defaults, and they will be curried and used at view query
|
|
62
|
-
# time. Or they can be overridden at query time.
|
|
63
|
-
#
|
|
64
|
-
# Custom views can be queried with <tt>:reduce => true</tt> to return
|
|
65
|
-
# reduce results. The default for custom views is to query with
|
|
66
|
-
# <tt>:reduce => false</tt>.
|
|
67
|
-
#
|
|
68
|
-
# Views are generated (on a per-model basis) lazily on first-access.
|
|
69
|
-
# This means that if you are deploying changes to a view, the views for
|
|
70
|
-
# that model won't be available until generation is complete. This can
|
|
71
|
-
# take some time with large databases. Strategies are in the works.
|
|
72
|
-
#
|
|
73
|
-
# To understand the capabilities of this view system more compeletly,
|
|
74
|
-
# it is recommended that you read the RSpec file at
|
|
75
|
-
# <tt>spec/core/model_spec.rb</tt>.
|
|
76
|
-
|
|
77
|
-
def view_by(*keys)
|
|
78
|
-
self.design_doc ||= Design.new(default_design_doc)
|
|
79
|
-
opts = keys.pop if keys.last.is_a?(Hash)
|
|
80
|
-
opts ||= {}
|
|
81
|
-
ducktype = opts.delete(:ducktype)
|
|
82
|
-
unless ducktype || opts[:map]
|
|
83
|
-
opts[:guards] ||= []
|
|
84
|
-
opts[:guards].push "(doc['couchrest-type'] == '#{self.to_s}')"
|
|
85
|
-
end
|
|
86
|
-
keys.push opts
|
|
87
|
-
self.design_doc.view_by(*keys)
|
|
88
|
-
self.design_doc_fresh = false
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
# returns stored defaults if the there is a view named this in the design doc
|
|
92
|
-
def has_view?(view)
|
|
93
|
-
view = view.to_s
|
|
94
|
-
design_doc && design_doc['views'] && design_doc['views'][view]
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
# Dispatches to any named view.
|
|
98
|
-
def view(name, query={}, &block)
|
|
99
|
-
unless design_doc_fresh
|
|
100
|
-
refresh_design_doc
|
|
101
|
-
end
|
|
102
|
-
query[:raw] = true if query[:reduce]
|
|
103
|
-
raw = query.delete(:raw)
|
|
104
|
-
fetch_view_with_docs(name, query, raw, &block)
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
def all_design_doc_versions
|
|
108
|
-
database.documents :startkey => "_design/#{self.to_s}-",
|
|
109
|
-
:endkey => "_design/#{self.to_s}-\u9999"
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
# Deletes any non-current design docs that were created by this class.
|
|
113
|
-
# Running this when you're deployed version of your application is steadily
|
|
114
|
-
# and consistently using the latest code, is the way to clear out old design
|
|
115
|
-
# docs. Running it to early could mean that live code has to regenerate
|
|
116
|
-
# potentially large indexes.
|
|
117
|
-
def cleanup_design_docs!
|
|
118
|
-
ddocs = all_design_doc_versions
|
|
119
|
-
ddocs["rows"].each do |row|
|
|
120
|
-
if (row['id'] != design_doc_id)
|
|
121
|
-
database.delete_doc({
|
|
122
|
-
"_id" => row['id'],
|
|
123
|
-
"_rev" => row['value']['rev']
|
|
124
|
-
})
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
private
|
|
130
|
-
|
|
131
|
-
def fetch_view_with_docs(name, opts, raw=false, &block)
|
|
132
|
-
if raw || (opts.has_key?(:include_docs) && opts[:include_docs] == false)
|
|
133
|
-
fetch_view(name, opts, &block)
|
|
134
|
-
else
|
|
135
|
-
begin
|
|
136
|
-
view = fetch_view name, opts.merge({:include_docs => true}), &block
|
|
137
|
-
view['rows'].collect{|r|new(r['doc'])} if view['rows']
|
|
138
|
-
rescue
|
|
139
|
-
# fallback for old versions of couchdb that don't
|
|
140
|
-
# have include_docs support
|
|
141
|
-
view = fetch_view(name, opts, &block)
|
|
142
|
-
view['rows'].collect{|r|new(database.get(r['id']))} if view['rows']
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
def fetch_view view_name, opts, &block
|
|
148
|
-
retryable = true
|
|
149
|
-
begin
|
|
150
|
-
design_doc.view(view_name, opts, &block)
|
|
151
|
-
# the design doc could have been deleted by a rouge process
|
|
152
|
-
rescue RestClient::ResourceNotFound => e
|
|
153
|
-
if retryable
|
|
154
|
-
refresh_design_doc
|
|
155
|
-
retryable = false
|
|
156
|
-
retry
|
|
157
|
-
else
|
|
158
|
-
raise e
|
|
159
|
-
end
|
|
160
|
-
end
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
end # module ClassMethods
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
end
|
|
167
|
-
end
|
|
168
|
-
end
|
data/lib/couchrest/mixins.rb
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), '..', 'mixins', 'properties')
|
|
2
|
-
|
|
3
|
-
module CouchRest
|
|
4
|
-
module CastedModel
|
|
5
|
-
|
|
6
|
-
def self.included(base)
|
|
7
|
-
base.send(:include, CouchRest::Mixins::Properties)
|
|
8
|
-
base.send(:attr_accessor, :casted_by)
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
def initialize(keys={})
|
|
12
|
-
super
|
|
13
|
-
keys.each do |k,v|
|
|
14
|
-
self[k.to_s] = v
|
|
15
|
-
end if keys
|
|
16
|
-
apply_defaults # defined in CouchRest::Mixins::Properties
|
|
17
|
-
# cast_keys # defined in CouchRest::Mixins::Properties
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def []= key, value
|
|
21
|
-
super(key.to_s, value)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def [] key
|
|
25
|
-
super(key.to_s)
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
begin
|
|
2
|
-
# still required for Time parsing and pluralization in the validation
|
|
3
|
-
require 'extlib'
|
|
4
|
-
rescue
|
|
5
|
-
puts "CouchRest::ExtendedDocument still requires extlib (not for much longer). This is left out of the gemspec on purpose."
|
|
6
|
-
raise
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
require 'mime/types'
|
|
10
|
-
require File.join(File.dirname(__FILE__), "property")
|
|
11
|
-
require File.join(File.dirname(__FILE__), '..', 'mixins', 'extended_document_mixins')
|
|
12
|
-
|
|
13
|
-
module CouchRest
|
|
14
|
-
|
|
15
|
-
# Same as CouchRest::Document but with properties and validations
|
|
16
|
-
class ExtendedDocument < Document
|
|
17
|
-
include CouchRest::Callbacks
|
|
18
|
-
include CouchRest::Mixins::DocumentQueries
|
|
19
|
-
include CouchRest::Mixins::Views
|
|
20
|
-
include CouchRest::Mixins::DesignDoc
|
|
21
|
-
include CouchRest::Mixins::ExtendedAttachments
|
|
22
|
-
|
|
23
|
-
def self.inherited(subklass)
|
|
24
|
-
subklass.send(:include, CouchRest::Mixins::Properties)
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
# Accessors
|
|
28
|
-
attr_accessor :casted_by
|
|
29
|
-
|
|
30
|
-
# Callbacks
|
|
31
|
-
define_callbacks :create
|
|
32
|
-
define_callbacks :save
|
|
33
|
-
define_callbacks :update
|
|
34
|
-
define_callbacks :destroy
|
|
35
|
-
|
|
36
|
-
def initialize(passed_keys={})
|
|
37
|
-
apply_defaults # defined in CouchRest::Mixins::Properties
|
|
38
|
-
super
|
|
39
|
-
cast_keys # defined in CouchRest::Mixins::Properties
|
|
40
|
-
unless self['_id'] && self['_rev']
|
|
41
|
-
self['couchrest-type'] = self.class.to_s
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
# Automatically set <tt>updated_at</tt> and <tt>created_at</tt> fields
|
|
47
|
-
# on the document whenever saving occurs. CouchRest uses a pretty
|
|
48
|
-
# decent time format by default. See Time#to_json
|
|
49
|
-
def self.timestamps!
|
|
50
|
-
class_eval <<-EOS, __FILE__, __LINE__
|
|
51
|
-
property(:updated_at, :read_only => true, :cast_as => 'Time', :auto_validation => false)
|
|
52
|
-
property(:created_at, :read_only => true, :cast_as => 'Time', :auto_validation => false)
|
|
53
|
-
|
|
54
|
-
save_callback :before do |object|
|
|
55
|
-
object['updated_at'] = Time.now
|
|
56
|
-
object['created_at'] = object['updated_at'] if object.new_document?
|
|
57
|
-
end
|
|
58
|
-
EOS
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
# Name a method that will be called before the document is first saved,
|
|
62
|
-
# which returns a string to be used for the document's <tt>_id</tt>.
|
|
63
|
-
# Because CouchDB enforces a constraint that each id must be unique,
|
|
64
|
-
# this can be used to enforce eg: uniq usernames. Note that this id
|
|
65
|
-
# must be globally unique across all document types which share a
|
|
66
|
-
# database, so if you'd like to scope uniqueness to this class, you
|
|
67
|
-
# should use the class name as part of the unique id.
|
|
68
|
-
def self.unique_id method = nil, &block
|
|
69
|
-
if method
|
|
70
|
-
define_method :set_unique_id do
|
|
71
|
-
self['_id'] ||= self.send(method)
|
|
72
|
-
end
|
|
73
|
-
elsif block
|
|
74
|
-
define_method :set_unique_id do
|
|
75
|
-
uniqid = block.call(self)
|
|
76
|
-
raise ArgumentError, "unique_id block must not return nil" if uniqid.nil?
|
|
77
|
-
self['_id'] ||= uniqid
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
# Temp solution to make the view_by methods available
|
|
83
|
-
def self.method_missing(m, *args)
|
|
84
|
-
if has_view?(m)
|
|
85
|
-
query = args.shift || {}
|
|
86
|
-
view(m, query, *args)
|
|
87
|
-
else
|
|
88
|
-
super
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
### instance methods
|
|
93
|
-
|
|
94
|
-
# Returns the Class properties
|
|
95
|
-
#
|
|
96
|
-
# ==== Returns
|
|
97
|
-
# Array:: the list of properties for the instance
|
|
98
|
-
def properties
|
|
99
|
-
self.class.properties
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
# Takes a hash as argument, and applies the values by using writer methods
|
|
103
|
-
# for each key. It doesn't save the document at the end. Raises a NoMethodError if the corresponding methods are
|
|
104
|
-
# missing. In case of error, no attributes are changed.
|
|
105
|
-
def update_attributes_without_saving(hash)
|
|
106
|
-
hash.each do |k, v|
|
|
107
|
-
raise NoMethodError, "#{k}= method not available, use property :#{k}" unless self.respond_to?("#{k}=")
|
|
108
|
-
end
|
|
109
|
-
hash.each do |k, v|
|
|
110
|
-
self.send("#{k}=",v)
|
|
111
|
-
end
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
# Takes a hash as argument, and applies the values by using writer methods
|
|
115
|
-
# for each key. Raises a NoMethodError if the corresponding methods are
|
|
116
|
-
# missing. In case of error, no attributes are changed.
|
|
117
|
-
def update_attributes(hash)
|
|
118
|
-
update_attributes_without_saving hash
|
|
119
|
-
save
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
# for compatibility with old-school frameworks
|
|
123
|
-
alias :new_record? :new_document?
|
|
124
|
-
|
|
125
|
-
# Trigger the callbacks (before, after, around)
|
|
126
|
-
# and create the document
|
|
127
|
-
# It's important to have a create callback since you can't check if a document
|
|
128
|
-
# was new after you saved it
|
|
129
|
-
#
|
|
130
|
-
# When creating a document, both the create and the save callbacks will be triggered.
|
|
131
|
-
def create(bulk = false)
|
|
132
|
-
caught = catch(:halt) do
|
|
133
|
-
_run_create_callbacks do
|
|
134
|
-
_run_save_callbacks do
|
|
135
|
-
create_without_callbacks(bulk)
|
|
136
|
-
end
|
|
137
|
-
end
|
|
138
|
-
end
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
# unlike save, create returns the newly created document
|
|
142
|
-
def create_without_callbacks(bulk =false)
|
|
143
|
-
raise ArgumentError, "a document requires a database to be created to (The document or the #{self.class} default database were not set)" unless database
|
|
144
|
-
set_unique_id if new_document? && self.respond_to?(:set_unique_id)
|
|
145
|
-
result = database.save_doc(self, bulk)
|
|
146
|
-
(result["ok"] == true) ? self : false
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
# Creates the document in the db. Raises an exception
|
|
150
|
-
# if the document is not created properly.
|
|
151
|
-
def create!
|
|
152
|
-
raise "#{self.inspect} failed to save" unless self.create
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
# Trigger the callbacks (before, after, around)
|
|
156
|
-
# only if the document isn't new
|
|
157
|
-
def update(bulk = false)
|
|
158
|
-
caught = catch(:halt) do
|
|
159
|
-
if self.new_document?
|
|
160
|
-
save(bulk)
|
|
161
|
-
else
|
|
162
|
-
_run_update_callbacks do
|
|
163
|
-
_run_save_callbacks do
|
|
164
|
-
save_without_callbacks(bulk)
|
|
165
|
-
end
|
|
166
|
-
end
|
|
167
|
-
end
|
|
168
|
-
end
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
# Trigger the callbacks (before, after, around)
|
|
172
|
-
# and save the document
|
|
173
|
-
def save(bulk = false)
|
|
174
|
-
caught = catch(:halt) do
|
|
175
|
-
if self.new_document?
|
|
176
|
-
_run_save_callbacks do
|
|
177
|
-
save_without_callbacks(bulk)
|
|
178
|
-
end
|
|
179
|
-
else
|
|
180
|
-
update(bulk)
|
|
181
|
-
end
|
|
182
|
-
end
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
# Overridden to set the unique ID.
|
|
186
|
-
# Returns a boolean value
|
|
187
|
-
def save_without_callbacks(bulk = false)
|
|
188
|
-
raise ArgumentError, "a document requires a database to be saved to (The document or the #{self.class} default database were not set)" unless database
|
|
189
|
-
set_unique_id if new_document? && self.respond_to?(:set_unique_id)
|
|
190
|
-
result = database.save_doc(self, bulk)
|
|
191
|
-
result["ok"] == true
|
|
192
|
-
end
|
|
193
|
-
|
|
194
|
-
# Saves the document to the db using save. Raises an exception
|
|
195
|
-
# if the document is not saved properly.
|
|
196
|
-
def save!
|
|
197
|
-
raise "#{self.inspect} failed to save" unless self.save
|
|
198
|
-
end
|
|
199
|
-
|
|
200
|
-
# Deletes the document from the database. Runs the :destroy callbacks.
|
|
201
|
-
# Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the
|
|
202
|
-
# document to be saved to a new <tt>_id</tt>.
|
|
203
|
-
def destroy(bulk=false)
|
|
204
|
-
caught = catch(:halt) do
|
|
205
|
-
_run_destroy_callbacks do
|
|
206
|
-
result = database.delete_doc(self, bulk)
|
|
207
|
-
if result['ok']
|
|
208
|
-
self['_rev'] = nil
|
|
209
|
-
self['_id'] = nil
|
|
210
|
-
end
|
|
211
|
-
result['ok']
|
|
212
|
-
end
|
|
213
|
-
end
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
end
|
|
217
|
-
end
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
module CouchRest
|
|
2
|
-
|
|
3
|
-
# Basic attribute support for adding getter/setter + validation
|
|
4
|
-
class Property
|
|
5
|
-
attr_reader :name, :type, :read_only, :alias, :default, :casted, :init_method, :options
|
|
6
|
-
|
|
7
|
-
# attribute to define
|
|
8
|
-
def initialize(name, type = nil, options = {})
|
|
9
|
-
@name = name.to_s
|
|
10
|
-
parse_type(type)
|
|
11
|
-
parse_options(options)
|
|
12
|
-
self
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
private
|
|
17
|
-
|
|
18
|
-
def parse_type(type)
|
|
19
|
-
if type.nil?
|
|
20
|
-
@type = 'String'
|
|
21
|
-
elsif type.is_a?(Array) && type.empty?
|
|
22
|
-
@type = 'Array'
|
|
23
|
-
else
|
|
24
|
-
@type = type.is_a?(Array) ? [type.first.to_s] : type.to_s
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def parse_options(options)
|
|
29
|
-
return if options.empty?
|
|
30
|
-
@validation_format = options.delete(:format) if options[:format]
|
|
31
|
-
@read_only = options.delete(:read_only) if options[:read_only]
|
|
32
|
-
@alias = options.delete(:alias) if options[:alias]
|
|
33
|
-
@default = options.delete(:default) if options[:default]
|
|
34
|
-
@casted = options[:casted] ? true : false
|
|
35
|
-
@init_method = options[:send] ? options.delete(:send) : 'new'
|
|
36
|
-
@options = options
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
end
|
|
40
|
-
end
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
# blank? methods for several different class types
|
|
2
|
-
class Object
|
|
3
|
-
# Returns true if the object is nil or empty (if applicable)
|
|
4
|
-
def blank?
|
|
5
|
-
nil? || (respond_to?(:empty?) && empty?)
|
|
6
|
-
end
|
|
7
|
-
end # class Object
|
|
8
|
-
|
|
9
|
-
class Numeric
|
|
10
|
-
# Numerics can't be blank
|
|
11
|
-
def blank?
|
|
12
|
-
false
|
|
13
|
-
end
|
|
14
|
-
end # class Numeric
|
|
15
|
-
|
|
16
|
-
class NilClass
|
|
17
|
-
# Nils are always blank
|
|
18
|
-
def blank?
|
|
19
|
-
true
|
|
20
|
-
end
|
|
21
|
-
end # class NilClass
|
|
22
|
-
|
|
23
|
-
class TrueClass
|
|
24
|
-
# True is not blank.
|
|
25
|
-
def blank?
|
|
26
|
-
false
|
|
27
|
-
end
|
|
28
|
-
end # class TrueClass
|
|
29
|
-
|
|
30
|
-
class FalseClass
|
|
31
|
-
# False is always blank.
|
|
32
|
-
def blank?
|
|
33
|
-
true
|
|
34
|
-
end
|
|
35
|
-
end # class FalseClass
|
|
36
|
-
|
|
37
|
-
class String
|
|
38
|
-
# Strips out whitespace then tests if the string is empty.
|
|
39
|
-
def blank?
|
|
40
|
-
strip.empty?
|
|
41
|
-
end
|
|
42
|
-
end # class String
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2004-2008 David Heinemeier Hansson
|
|
2
|
-
#
|
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
-
# a copy of this software and associated documentation files (the
|
|
5
|
-
# "Software"), to deal in the Software without restriction, including
|
|
6
|
-
# without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
-
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
-
# permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
-
# the following conditions:
|
|
10
|
-
#
|
|
11
|
-
# The above copyright notice and this permission notice shall be
|
|
12
|
-
# included in all copies or substantial portions of the Software.
|
|
13
|
-
#
|
|
14
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
-
|
|
22
|
-
# Allows attributes to be shared within an inheritance hierarchy, but where
|
|
23
|
-
# each descendant gets a copy of their parents' attributes, instead of just a
|
|
24
|
-
# pointer to the same. This means that the child can add elements to, for
|
|
25
|
-
# example, an array without those additions being shared with either their
|
|
26
|
-
# parent, siblings, or children, which is unlike the regular class-level
|
|
27
|
-
# attributes that are shared across the entire hierarchy.
|
|
28
|
-
module CouchRest
|
|
29
|
-
module ClassExtension
|
|
30
|
-
def self.included(base)
|
|
31
|
-
if CouchRest::ClassExtension::InstanceMethods.instance_methods.all? {|m| base.respond_to?(m)}
|
|
32
|
-
# do nothing
|
|
33
|
-
elsif CouchRest::ClassExtension::InstanceMethods.instance_methods.any? {|m| base.respond_to?(m)}
|
|
34
|
-
raise RuntimeError, "Conflicting extentions to Class, work it out"
|
|
35
|
-
else
|
|
36
|
-
base.send(:include, CouchRest::ClassExtension::InstanceMethods)
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
module InstanceMethods
|
|
41
|
-
# Defines class-level and instance-level attribute reader.
|
|
42
|
-
#
|
|
43
|
-
# @param *syms<Array> Array of attributes to define reader for.
|
|
44
|
-
# @return <Array[#to_s]> List of attributes that were made into cattr_readers
|
|
45
|
-
#
|
|
46
|
-
# @api public
|
|
47
|
-
#
|
|
48
|
-
# @todo Is this inconsistent in that it does not allow you to prevent
|
|
49
|
-
# an instance_reader via :instance_reader => false
|
|
50
|
-
def cattr_reader(*syms)
|
|
51
|
-
syms.flatten.each do |sym|
|
|
52
|
-
next if sym.is_a?(Hash)
|
|
53
|
-
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
|
54
|
-
unless defined? @@#{sym}
|
|
55
|
-
@@#{sym} = nil
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def self.#{sym}
|
|
59
|
-
@@#{sym}
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def #{sym}
|
|
63
|
-
@@#{sym}
|
|
64
|
-
end
|
|
65
|
-
RUBY
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
# Defines class-level (and optionally instance-level) attribute writer.
|
|
70
|
-
#
|
|
71
|
-
# @param <Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to define writer for.
|
|
72
|
-
# @option syms :instance_writer<Boolean> if true, instance-level attribute writer is defined.
|
|
73
|
-
# @return <Array[#to_s]> List of attributes that were made into cattr_writers
|
|
74
|
-
#
|
|
75
|
-
# @api public
|
|
76
|
-
def cattr_writer(*syms)
|
|
77
|
-
options = syms.last.is_a?(Hash) ? syms.pop : {}
|
|
78
|
-
syms.flatten.each do |sym|
|
|
79
|
-
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
|
80
|
-
unless defined? @@#{sym}
|
|
81
|
-
@@#{sym} = nil
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def self.#{sym}=(obj)
|
|
85
|
-
@@#{sym} = obj
|
|
86
|
-
end
|
|
87
|
-
RUBY
|
|
88
|
-
|
|
89
|
-
unless options[:instance_writer] == false
|
|
90
|
-
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
|
91
|
-
def #{sym}=(obj)
|
|
92
|
-
@@#{sym} = obj
|
|
93
|
-
end
|
|
94
|
-
RUBY
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
# Defines class-level (and optionally instance-level) attribute accessor.
|
|
100
|
-
#
|
|
101
|
-
# @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to define accessor for.
|
|
102
|
-
# @option syms :instance_writer<Boolean> if true, instance-level attribute writer is defined.
|
|
103
|
-
# @return <Array[#to_s]> List of attributes that were made into accessors
|
|
104
|
-
#
|
|
105
|
-
# @api public
|
|
106
|
-
def cattr_accessor(*syms)
|
|
107
|
-
cattr_reader(*syms)
|
|
108
|
-
cattr_writer(*syms)
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
# Defines class-level inheritable attribute reader. Attributes are available to subclasses,
|
|
112
|
-
# each subclass has a copy of parent's attribute.
|
|
113
|
-
#
|
|
114
|
-
# @param *syms<Array[#to_s]> Array of attributes to define inheritable reader for.
|
|
115
|
-
# @return <Array[#to_s]> Array of attributes converted into inheritable_readers.
|
|
116
|
-
#
|
|
117
|
-
# @api public
|
|
118
|
-
#
|
|
119
|
-
# @todo Do we want to block instance_reader via :instance_reader => false
|
|
120
|
-
# @todo It would be preferable that we do something with a Hash passed in
|
|
121
|
-
# (error out or do the same as other methods above) instead of silently
|
|
122
|
-
# moving on). In particular, this makes the return value of this function
|
|
123
|
-
# less useful.
|
|
124
|
-
def class_inheritable_reader(*ivars)
|
|
125
|
-
instance_reader = ivars.pop[:reader] if ivars.last.is_a?(Hash)
|
|
126
|
-
|
|
127
|
-
ivars.each do |ivar|
|
|
128
|
-
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
129
|
-
def self.#{ivar}
|
|
130
|
-
return @#{ivar} if self.object_id == #{self.object_id} || defined?(@#{ivar})
|
|
131
|
-
ivar = superclass.#{ivar}
|
|
132
|
-
return nil if ivar.nil? && !#{self}.instance_variable_defined?("@#{ivar}")
|
|
133
|
-
@#{ivar} = ivar && !ivar.is_a?(Module) && !ivar.is_a?(Numeric) && !ivar.is_a?(TrueClass) && !ivar.is_a?(FalseClass) && !ivar.is_a?(Symbol) ? ivar.dup : ivar
|
|
134
|
-
end
|
|
135
|
-
RUBY
|
|
136
|
-
unless instance_reader == false
|
|
137
|
-
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
138
|
-
def #{ivar}
|
|
139
|
-
self.class.#{ivar}
|
|
140
|
-
end
|
|
141
|
-
RUBY
|
|
142
|
-
end
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
# Defines class-level inheritable attribute writer. Attributes are available to subclasses,
|
|
147
|
-
# each subclass has a copy of parent's attribute.
|
|
148
|
-
#
|
|
149
|
-
# @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to
|
|
150
|
-
# define inheritable writer for.
|
|
151
|
-
# @option syms :instance_writer<Boolean> if true, instance-level inheritable attribute writer is defined.
|
|
152
|
-
# @return <Array[#to_s]> An Array of the attributes that were made into inheritable writers.
|
|
153
|
-
#
|
|
154
|
-
# @api public
|
|
155
|
-
#
|
|
156
|
-
# @todo We need a style for class_eval <<-HEREDOC. I'd like to make it
|
|
157
|
-
# class_eval(<<-RUBY, __FILE__, __LINE__), but we should codify it somewhere.
|
|
158
|
-
def class_inheritable_writer(*ivars)
|
|
159
|
-
instance_writer = ivars.pop[:instance_writer] if ivars.last.is_a?(Hash)
|
|
160
|
-
ivars.each do |ivar|
|
|
161
|
-
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
162
|
-
def self.#{ivar}=(obj)
|
|
163
|
-
@#{ivar} = obj
|
|
164
|
-
end
|
|
165
|
-
RUBY
|
|
166
|
-
unless instance_writer == false
|
|
167
|
-
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
168
|
-
def #{ivar}=(obj) self.class.#{ivar} = obj end
|
|
169
|
-
RUBY
|
|
170
|
-
end
|
|
171
|
-
end
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
# Defines class-level inheritable attribute accessor. Attributes are available to subclasses,
|
|
175
|
-
# each subclass has a copy of parent's attribute.
|
|
176
|
-
#
|
|
177
|
-
# @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to
|
|
178
|
-
# define inheritable accessor for.
|
|
179
|
-
# @option syms :instance_writer<Boolean> if true, instance-level inheritable attribute writer is defined.
|
|
180
|
-
# @return <Array[#to_s]> An Array of attributes turned into inheritable accessors.
|
|
181
|
-
#
|
|
182
|
-
# @api public
|
|
183
|
-
def class_inheritable_accessor(*syms)
|
|
184
|
-
class_inheritable_reader(*syms)
|
|
185
|
-
class_inheritable_writer(*syms)
|
|
186
|
-
end
|
|
187
|
-
end
|
|
188
|
-
end
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
Class.send(:include, CouchRest::ClassExtension)
|