Class: Webby::Resources::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/webby/resources/db.rb

Overview

A rudimentary “database” for holding resource objects and finding them. The database is held in a Ruby hash keyed by the directories in the content folder.

Instance Method Summary collapse

Constructor Details

#initializeDB

call-seq:

DB.new

Create a new resources database object. This is used to store resources and to find them by their attributes.



15
16
17
# File 'lib/webby/resources/db.rb', line 15

def initialize
  @db = Hash.new {|h,k| h[k] = []}
end

Instance Method Details

#add(page) ⇒ Object Also known as: <<

call-seq:

add( resource )    => resource

Add the given resource to the database. It will not be added a second time if it already exists in the database.



25
26
27
28
29
30
31
32
33
# File 'lib/webby/resources/db.rb', line 25

def add( page )
  ary = @db[page.directory]

  # make sure we don't duplicate pages
  ary.delete page if ary.include? page
  ary << page

  page
end

#children(page, opts = {}) ⇒ Object

call-seq:

children( page, opts = {} )    => array

Returns an array of resources that are children of the given page resource. A child is any resource that exists in a subdirectory of the page’s directory.

Options

:sorty_by<Symbol>

The attribute to sort by

:reverse<Boolean>

Reverse the order of the results



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/webby/resources/db.rb', line 209

def children( page, opts = {} )
  rgxp = Regexp.new "\\A#{page.directory}/[^/]+"

  keys = @db.keys.find_all {|k| rgxp =~ k}
  ary  = keys.map {|k| @db[k]}
  ary.flatten!

  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

#clearObject

call-seq:

clear    => self

Removes all resources from the database.



41
42
43
44
# File 'lib/webby/resources/db.rb', line 41

def clear
  @db.clear
  self
end

#each(&block) ⇒ Object

call-seq:

each {|resource| block}

Iterate over each resource in the database and pass it to the given block.



52
53
54
55
56
57
58
# File 'lib/webby/resources/db.rb', line 52

def each( &block )
  keys = @db.keys.sort
  keys.each do |k|
    @db[k].sort.each(&block)
  end
  self
end

#find(*args, &block) ⇒ Object

call-seq:

find( limit = nil, opts = {} )                       => resource or nil
find( limit = nil, opts = {} ) {|resource| block}    => resource or nil

Find a specific resource or collection of resources in the pages database. The first resource found will be returned if the limit is nil. If the limit is an integer, then up to that number of resources will be returned as an array. If the limit is :all, then all resources matching the given attributes will be returned.

Resources can be found using any combination of attributes by passing them in as options to the find method. This will used simple equality comparison to find the resource or resources.

If the :include option is given as :all then all resources that match the finder criteria will be returned in an array. If none are found, an empty array will be returned. If the :include option is given as an integer then the first n resources found will be returned. Otherwise, or if the :include option is not given, the first resource found will be returned

For more complex finders, a block should be supplied. The usage follows that of of the Enumerable#find or Enumerable#find_all methods, depending on the limit. The method will return the first resource or all resources, respectively, for which the block returns true.

Options

:in_directory<String>

The directory to search.

:recursive<Boolean>

Whether or not to recurse into subdirectories

:sort_by<Symbol>

Sort results using the given attribute

:reverse<Boolean>

Reverse the order of the search results

Examples

# find the "index" resource in the "foo/bar" directory
@pages.find( :filename => 'index', :in_directory => 'foo/bar' )

# find all resources under the "foo/bar" directory recursively
@pages.find( :all, :in_directory => 'foo/bar', :recursive => true )

# find the resource named "widgets" whose color is "blue"
@pages.find( :name => 'widgets', :color => 'blue' )

# find all resources created in the past week
@pages.find( :all ) do |resource|
  resource.created_at > Time.now - (7 * 24 * 3600)
end


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/webby/resources/db.rb', line 111

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 directories to search through and whether to recurse
  # into directories or not
  search = if (dir = opts.delete(:in_directory))
    dir = dir.sub(%r/^\//, '')
    strategy = if opts.delete(:recursive)
      rgxp = dir.empty? ? '.*' : Regexp.escape(dir)
      lambda { |key| key =~ %r/^#{rgxp}(\/.*)?$/ }
    else
      lambda { |key| key == dir }
    end
    matching_keys = @db.keys.select(&strategy)
    raise RuntimeError, "unknown directory '#{dir}'" if matching_keys.empty?
    matching_keys.map { |key| @db[key] }.flatten
  else
    self
  end

  # construct a search block if one was not supplied by the user
  block ||= lambda do |page|
    found = true
    opts.each do |key, value|
      found &&= page.__send__(key.to_sym) == value
      break if not found
    end
    found
  end
  
  # search through the directories for the desired pages
  ary = []
  search.each do |page|
    ary << page if block.call(page)
  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

#parent_of(page) ⇒ Object

call-seq:

  parent_of( resource )    => resource or nil

Returns the parent page of the given resource or nil if the resource is
at the root of the directory structure. The parent is the "index" page
of the current directory or the next directory up the chain.


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/webby/resources/db.rb', line 231

def parent_of( page )
  dir = page.directory

  loop do
    if @db.has_key? dir
      parent = @db[dir].find {|p| p.filename == 'index'}
      return parent unless parent.nil? or parent == page
    end

    break if dir.empty?
    dir = ::File.dirname(dir)
    dir = '' if dir == '.'
  end

  return
end

#siblings(page, opts = {}) ⇒ Object

call-seq:

siblings( page, opts = {} )    => array

Returns an array of resources that are siblings of the given page resource. A sibling is any resource that is in the same directory as the page.

Options

:sorty_by<Symbol>

The attribute to sort by

:reverse<Boolean>

Reverse the order of the results



185
186
187
188
189
190
191
192
193
194
# File 'lib/webby/resources/db.rb', line 185

def siblings( page, opts = {} )
  ary = @db[page.directory].dup
  ary.delete page
  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