Skip to content

Commit

Permalink
move commen helpers into helper module
Browse files Browse the repository at this point in the history
  • Loading branch information
jweiss committed Feb 10, 2010
1 parent a8055cb commit 4d32666
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
1 change: 1 addition & 0 deletions lib/fakecouch.rb
Expand Up @@ -5,6 +5,7 @@
require "couchrest"


require "fakecouch/helper"
require "fakecouch/error"
require "fakecouch/view"
require "fakecouch/database"
Expand Down
12 changes: 5 additions & 7 deletions lib/fakecouch/database.rb
Expand Up @@ -132,6 +132,8 @@ def all_documents(options = {})
'include_docs' => false
}.update(options)
options.assert_valid_keys('descending', 'startkey', 'endkey', 'limit', 'include_docs')
Fakecouch::Helper.jsonfy_options(options, 'startkey', 'endkey')

keys = (options['descending'].to_s == 'true') ? storage.keys.sort{|x,y| y <=> x } : storage.keys.sort{|x,y| x <=> y }

keys, offset = filter_by_startkey(keys, options)
Expand Down Expand Up @@ -192,13 +194,13 @@ def design_doc?(doc)

def matching_revision?(existing_record, rev)
document = JSON.parse(existing_record)
attribute_access('_rev', document) == rev
Fakecouch::Helper.access('_rev', document) == rev
end

def filter_by_startkey(keys, options)
offset = 0
if options['startkey']
options['startkey'] = options['startkey'].gsub(/\A"/, '').gsub(/"\Z/, '')
options['startkey'] = options['startkey']
startkey_found = false
keys = keys.map do |key|
if startkey_found || key == options['startkey']
Expand All @@ -215,7 +217,7 @@ def filter_by_startkey(keys, options)

def filter_by_endkey(keys, options)
if options['endkey']
options['endkey'] = options['endkey'].gsub(/\A"/, '').gsub(/"\Z/, '')
options['endkey'] = options['endkey']
endkey_found = false
keys = keys.map do |key|
if key == options['endkey']
Expand All @@ -237,10 +239,6 @@ def filter_by_limit(keys, options)
end
keys
end

def attribute_access(attr_name, doc)
doc.respond_to?(:_document) ? doc._document[attr_name] : doc[attr_name]
end

end
end
15 changes: 15 additions & 0 deletions lib/fakecouch/helper.rb
@@ -0,0 +1,15 @@
module Fakecouch
module Helper

def self.jsonfy_options(options, *keys)
keys.each do |key|
options[key] = ActiveSupport::JSON.decode(options[key]) if options[key]
end
end

def self.access(attr_name, doc)
doc.respond_to?(:_document) ? doc._document[attr_name] : doc[attr_name]
end

end
end
36 changes: 13 additions & 23 deletions lib/fakecouch/view.rb
Expand Up @@ -33,7 +33,7 @@ def initialize(database, design_document_name, view_name, options = {})
'startkey_docid' => nil
}.update(options)
@options.assert_valid_keys('reduce', 'limit', 'key', 'descending', 'include_docs', 'without_deleted', 'endkey', 'startkey', 'endkey_docid', 'startkey_docid')
jsonfy_options(@options, 'key', 'startkey', 'endkey', 'startkey_docid', 'endkey_docid')
Fakecouch::Helper.jsonfy_options(@options, 'key', 'startkey', 'endkey', 'startkey_docid', 'endkey_docid')

normalize_view_name
end
Expand Down Expand Up @@ -68,10 +68,10 @@ def render
rows = keys.map do |key|
document = ruby_store[key]
if options['include_docs'].to_s == 'true'
{'id' => attribute_access('_id', document), 'value' => nil, 'doc' => (document.respond_to?(:_document) ? document._document : document) }.merge(key_description)
{'id' => Fakecouch::Helper.access('_id', document), 'value' => nil, 'doc' => (document.respond_to?(:_document) ? document._document : document) }.merge(key_description)
else
{'id' => attribute_access('_id', document), 'key' => options['key'], 'value' => nil}.merge(key_description)
#{'id' => attribute_access('_id', document), 'key' => options['key'], 'value' => nil}
{'id' => Fakecouch::Helper.access('_id', document), 'key' => options['key'], 'value' => nil}.merge(key_description)
#{'id' => Fakecouch::Helper.access('_id', document), 'key' => options['key'], 'value' => nil}
end
end
{ "total_rows" => total_size, "offset" => offset, "rows" => rows}.to_json
Expand Down Expand Up @@ -111,12 +111,6 @@ def normalize_view_name
@view_name = view_name.gsub(/_withoutdeleted\Z/, '').gsub(/_without_deleted\Z/, '').gsub(/_withdeleted\Z/, '').gsub(/_with_deleted\Z/, '')
end

def jsonfy_options(options, *keys)
keys.each do |key|
options[key] = ActiveSupport::JSON.decode(options[key]) if options[key]
end
end

def initialize_ruby_store
@ruby_store = database.storage.dup
@ruby_store.each{|k,v| ruby_store[k] = JSON.parse(v) }
Expand Down Expand Up @@ -149,38 +143,38 @@ def filter_items_by_range(attributes)
def filter_items_not_in_range(attribute, start_key, end_key)
@keys = keys.select do |key|
document = ruby_store[key]
attribute_access(attribute, document) && (attribute_access(attribute, document) >= start_key) && (attribute_access(attribute, document) <= end_key)
Fakecouch::Helper.access(attribute, document) && (Fakecouch::Helper.access(attribute, document) >= start_key) && (Fakecouch::Helper.access(attribute, document) <= end_key)
end
end

def filter_deleted_items
@keys = keys.delete_if do |key|
document = ruby_store[key]
attribute_access('deleted_at', document).present?
Fakecouch::Helper.access('deleted_at', document).present?
end
end

def sort_by_attribute(attribute)
attribute ||= '_id'
@keys = (options['descending'].to_s == 'true') ?
keys.sort{|x,y| attribute_access(attribute, ruby_store[y]) <=> attribute_access(attribute, ruby_store[x]) } :
keys.sort{|x,y| attribute_access(attribute, ruby_store[x]) <=> attribute_access(attribute, ruby_store[y]) }
keys.sort{|x,y| Fakecouch::Helper.access(attribute, ruby_store[y]) <=> Fakecouch::Helper.access(attribute, ruby_store[x]) } :
keys.sort{|x,y| Fakecouch::Helper.access(attribute, ruby_store[x]) <=> Fakecouch::Helper.access(attribute, ruby_store[y]) }
end

def filter_items_without_attribute_value(attribute, attr_value)
if attr_value
@keys = keys.select do |key|
document = ruby_store[key]
if attribute_access(attribute, document).is_a?(Array)
attribute_access(attribute, document).include?(attr_value)
if Fakecouch::Helper.access(attribute, document).is_a?(Array)
Fakecouch::Helper.access(attribute, document).include?(attr_value)
else
attribute_access(attribute, document) == attr_value
Fakecouch::Helper.access(attribute, document) == attr_value
end
end
else
@keys = keys.select do |key|
document = ruby_store[key]
attribute_access(attribute, document).present?
Fakecouch::Helper.access(attribute, document).present?
end
end
end
Expand All @@ -189,7 +183,7 @@ def filter_items_without_correct_ruby_class
klass_name = design_document_name.classify
@keys = keys.select do |key|
document = ruby_store[key]
attribute_access('ruby_class', document).to_s.classify == klass_name
Fakecouch::Helper.access('ruby_class', document).to_s.classify == klass_name
end
end

Expand All @@ -213,10 +207,6 @@ def filter_by_startkey_docid_and_endkey_docid
end
end

def attribute_access(attr_name, doc)
doc.respond_to?(:_document) ? doc._document[attr_name] : doc[attr_name]
end

def foreign_key_id(name)
name.underscore.gsub('/','__').gsub('::','__') + "_id"
end
Expand Down

0 comments on commit 4d32666

Please sign in to comment.