Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

lots of work for real-world application (blog)

  • Loading branch information...
commit c33f4e85823f0cc45d482731a1eb0dc53bf85556 1 parent 332eb4a
Lachie Cox authored
27 examples/blog.rb
... ... @@ -1,16 +1,18 @@
1 1 require File.dirname(__FILE__)+'/../lib/dirdb'
  2 +require 'ostruct'
2 3
3   -class Article
  4 +class Article < OpenStruct
4 5 include DirDB::Resource
5 6
6 7 # index_class DirDB::Index::YAML
7   - # glob '*.haml'
  8 + glob '*.haml'
8 9
9 10 index :by_ctime do |article|
10 11 File.ctime(article.path)
11 12 end
12   - index :by_reverse_length do |article|
13   - -article.title.length
  13 +
  14 + lookup :by_slug do |article|
  15 + article.slug
14 16 end
15 17
16 18 attr_reader :title
@@ -24,18 +26,21 @@ def self.scan_file(path)
24 26 # end
25 27 # end
26 28
27   - # title
28   - File.basename(path,'haml').sub(/^\d+_/,'').gsub('_',' ')
29   - end
30   -
31   - def initialize(title)
32   - @title = title || ''
  29 + info = {
  30 + :title => File.basename(path,'haml').sub(/^\d+_/,'').gsub('_',' '),
  31 + :slug => File.basename(path,'haml').sub(/\.$/,'')
  32 + }
  33 + pp info
  34 + [ info ]
33 35 end
  36 +
34 37 end
35 38
36 39 if __FILE__ == $0
37 40 require 'pp'
38 41 Article.path = File.dirname(__FILE__) + '/blog'
39 42
40   - pp Article.all(:by_ctime)
  43 + p = Article.all(:by_ctime).first
  44 + puts p.path
  45 + puts p.read.class
41 46 end
1  lib/dirdb.rb
@@ -5,6 +5,5 @@ module DirDB
5 5
6 6 require "#{__DIR__}/dirdb/resource"
7 7
8   -require "#{__DIR__}/dirdb/index"
9 8 require "#{__DIR__}/dirdb/index/basic"
10 9 require "#{__DIR__}/dirdb/index/yaml"
14 lib/dirdb/index.rb
... ... @@ -1,14 +0,0 @@
1   -module DirDB
2   - module Index
3   -
4   - def initialize
5   - end
6   -
7   - def get(resource_class,basename)
8   - end
9   -
10   - def all(resource_class)
11   - end
12   -
13   - end
14   -end
30 lib/dirdb/index/basic.rb
@@ -11,6 +11,11 @@ def indexes
11 11 @indexes
12 12 end
13 13
  14 + def lookups
  15 + prepare! unless @lookups
  16 + @lookups
  17 + end
  18 +
14 19 # access resources
15 20 def resources
16 21 prepare! unless @resources
@@ -19,13 +24,17 @@ def resources
19 24
20 25 # prepare the lazy variables indexes & resources.
21 26 def prepare!
  27 + @lookups = {}
22 28 scan_resources!
23 29 build_index!
  30 + build_lookups!
  31 +
  32 + pp @lookups
24 33 end
25 34
26 35 # scan all the resources resource_class knows about
27 36 def scan_resources!
28   - @resources = {}
  37 + @resources = @lookups[:_default] = {}
29 38 @resource_arguments = {}
30 39
31 40 @default_order = []
@@ -33,7 +42,7 @@ def scan_resources!
33 42 @default_order << basename = File.basename(path)
34 43
35 44 @resource_arguments[basename] = resource_arguments = @resource_class.scan_file(path)
36   -
  45 +
37 46 instantiate_resource!(basename,resource_arguments)
38 47 end
39 48 end
@@ -46,15 +55,23 @@ def instantiate_resource!(basename,resource_arguments)
46 55
47 56 # build resources using the resource_class
48 57 def build_index!
  58 + puts "build_index!"
49 59 @indexes = {}
50 60
51 61 @indexes = @resource_class.build_indexes(@resources)
52 62 @indexes[:_default] ||= @default_order
53 63 end
54   -
55   - # get a resource
56   - def get(basename)
57   - resources[basename]
  64 +
  65 + def build_lookups!
  66 + @lookups = @resource_class.build_lookups(@resources)
  67 + end
  68 +
  69 + def find(name,key)
  70 + pp name
  71 + pp key
  72 + pp @lookups
  73 +
  74 + lookups[name][key]
58 75 end
59 76
60 77 # get all resources sorted using +index+
@@ -62,6 +79,7 @@ def all(index_name=nil)
62 79 index_name ||= :_default
63 80 selected_index = indexes[index_name] || raise(ArgumentError,"Index '#{index_name}' doesn't exist.")
64 81
  82 + puts "selected_index: #{index_name}"
65 83 pp selected_index
66 84
67 85 resources.values_at(*selected_index)
2  lib/dirdb/index/yaml.rb
@@ -22,7 +22,7 @@ def read_index!
22 22 @resource_arguments = state[:resource_arguments]
23 23
24 24 @resource_arguments.each do |(basename,args)|
25   - instantiate_resource!(basename,resource_arguments)
  25 + instantiate_resource!(basename,args)
26 26 end
27 27 end
28 28
7 lib/dirdb/info.rb
... ... @@ -1,7 +0,0 @@
1   -module DirDB
2   - class File
3   - def initialize(path)
4   -
5   - end
6   - end
7   -end
35 lib/dirdb/resource.rb
@@ -16,6 +16,10 @@ def index
16 16 self.class.index
17 17 end
18 18
  19 + def read
  20 + File.read(path)
  21 + end
  22 +
19 23 module ClassMethods
20 24 def path=(path)
21 25 @path = path
@@ -54,6 +58,11 @@ def index(name,&block)
54 58 @indexes[name] = block
55 59 end
56 60
  61 + def lookup(name,&block)
  62 + @lookups ||= {}
  63 + @lookups[name] = block
  64 + end
  65 +
57 66 def build_indexes(resources)
58 67 built_index = {}
59 68
@@ -64,6 +73,20 @@ def build_indexes(resources)
64 73 built_index
65 74 end
66 75
  76 + def build_lookups(resources)
  77 + built_lookups = {}
  78 +
  79 + (@lookups || {}).each do |(name,block)|
  80 + built_lookups[name] = lookup = Hash.new {|h,k| h[k] = []}
  81 +
  82 + resources.each do |(_,res)|
  83 + lookup[block[res]] << res
  84 + end
  85 + end
  86 +
  87 + built_lookups
  88 + end
  89 +
67 90 def resource_index
68 91 @resource_index ||= index_class.new(self)
69 92 end
@@ -77,12 +100,22 @@ def glob(glob=nil)
77 100 end
78 101 end
79 102
  103 +
  104 +
80 105 def all(index_name=nil)
81 106 resource_index.all(index_name)
82 107 end
83 108
84 109 def get(basename)
85   - resource_index.get(basename)
  110 + resource_index.find(:_default, basename)
  111 + end
  112 +
  113 + def find(lookup_name,key)
  114 + resource_index.find(lookup_name,key)
  115 + end
  116 +
  117 + def first(lookup_name,key)
  118 + resource_index.find(lookup_name,key).first
86 119 end
87 120 end
88 121 end
12 lib/dirdb/scanner.rb
... ... @@ -1,12 +0,0 @@
1   -module DirDB
2   - module Scanner
3   -
4   - def initialize(path)
5   - @path = path
6   - end
7   -
8   - def get(basename)
9   -
10   - end
11   - end
12   -end

0 comments on commit c33f4e8

Please sign in to comment.
Something went wrong with that request. Please try again.