Class: Stratus::Resources::HashDB
- Inherits:
-
Object
- Object
- Stratus::Resources::HashDB
- Defined in:
- lib/stratus/resources/hash_db.rb
Overview
A rudimentary “database” for holding resource objects and finding them. The database is held in a Ruby hash keyed by the collection/slug Based on Webby’s DB class…
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
- #add(content) ⇒ Object (also: #<<)
- #clear ⇒ Object
- #each(&block) ⇒ Object
-
#find(*args, &block) ⇒ Object
FIXME: Need to add support for: collection/* /.
-
#initialize ⇒ HashDB
constructor
A new instance of HashDB.
- #siblings(content, opts = {}) ⇒ Object
Constructor Details
#initialize ⇒ HashDB
Returns a new instance of HashDB.
9 10 11 |
# File 'lib/stratus/resources/hash_db.rb', line 9 def initialize @db = Hash.new {|h,k| h[k] = []} end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
7 8 9 |
# File 'lib/stratus/resources/hash_db.rb', line 7 def db @db end |
Instance Method Details
#add(content) ⇒ Object Also known as: <<
13 14 15 16 17 18 19 20 21 |
# File 'lib/stratus/resources/hash_db.rb', line 13 def add( content ) ary = @db[content.content_path] # make sure we don't duplicate contents ary.delete content if ary.include? content ary << content content end |
#clear ⇒ Object
24 25 26 27 |
# File 'lib/stratus/resources/hash_db.rb', line 24 def clear @db.clear self end |
#each(&block) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/stratus/resources/hash_db.rb', line 29 def each( &block ) keys = @db.keys.sort keys.each do |k| @db[k].sort.each(&block) end self end |
#find(*args, &block) ⇒ Object
FIXME: Need to add support for:
collection/*
*/*
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/stratus/resources/hash_db.rb', line 40 def find( *args, &block ) opts = Hash === args.last ? args.pop : {} limit = args.shift limit = opts.delete(:limit) if opts.has_key?(:limit) sort_by = opts.delete(:sort_by) reverse = opts.delete(:reverse) # figure out which collections to search through search = self # construct a search block if one was not supplied by the user block ||= lambda do |content| found = true opts.each do |key, value| if key == :content_path found &&= content.__send__(key.to_sym).match( Regexp.new( value.gsub('*', '.*'), 'g' ) ) else found &&= content.__send__(key.to_sym) == value end break if not found end found end # search through the collections for the desired content objects ary = [] search.each do |content| ary << content if block.call(content) end # sort the search results if the user gave an attribute to sort by if sort_by m = sort_by.to_sym ary.delete_if {|p| p.__send__(m).nil?} reverse ? ary.sort! {|a,b| b.__send__(m) <=> a.__send__(m)} : ary.sort! {|a,b| a.__send__(m) <=> b.__send__(m)} end # limit the search results case limit when :all, 'all' ary when Integer ary.slice(0,limit) else ary.first end end |
#siblings(content, opts = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/stratus/resources/hash_db.rb', line 91 def siblings( content, opts = {} ) ary = @db[content.content_path].dup ary.delete content return ary unless opts.has_key? :sort_by m = opts[:sort_by] ary.sort! {|a,b| a.__send__(m) <=> b.__send__(m)} ary.reverse! if opts[:reverse] ary end |