Class: CouchDocs::DesignDirectory

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_docs/design_directory.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ DesignDirectory

Returns a new instance of DesignDirectory.



23
24
25
26
# File 'lib/couch_docs/design_directory.rb', line 23

def initialize(path)
  Dir.new(path) # Just checkin'
  @couch_view_dir = path
end

Instance Attribute Details

#couch_view_dirObject

Returns the value of attribute couch_view_dir.



12
13
14
# File 'lib/couch_docs/design_directory.rb', line 12

def couch_view_dir
  @couch_view_dir
end

Class Method Details

.a_to_hash(a) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/couch_docs/design_directory.rb', line 14

def self.a_to_hash(a)
  key = a.first
  if (a.length > 2)
    { key => a_to_hash(a[1,a.length]) }
  else
    { key => a.last }
  end
end

Instance Method Details

#expand_file(filename) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/couch_docs/design_directory.rb', line 38

def expand_file(filename)
  File.dirname(filename).
    gsub(/#{couch_view_dir}\/?/, '').
    split(/\//) +
  [
   File.basename(filename, '.js').gsub(/%2F/, '/'),
   read_value(filename)
  ]
end

#process_code_macro(line) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/couch_docs/design_directory.rb', line 55

def process_code_macro(line)
  if line =~ %r{\s*//\s*!code\s*(\S+)\s*}
    "// !begin code #{$1}\n" +
    read_from_lib($1) +
    "// !end code #{$1}\n"
  else
    line
  end
end

#read_from_lib(path) ⇒ Object



65
66
67
# File 'lib/couch_docs/design_directory.rb', line 65

def read_from_lib(path)
  File.read("#{couch_view_dir}/__lib/#{path}")
end

#read_value(filename) ⇒ Object



48
49
50
51
52
53
# File 'lib/couch_docs/design_directory.rb', line 48

def read_value(filename)
  File.
    readlines(filename).
    map { |line| process_code_macro(line) }.
    join
end

#remove_code_macros(js) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/couch_docs/design_directory.rb', line 93

def remove_code_macros(js)
  js =~ %r{// !begin code ([.\w]+)$}m
  lib = $1
  if lib and js =~ %r{// !end code #{lib}$}m
    remove_code_macros(js.sub(%r{// !begin code #{lib}.+// !end code #{lib}}m, "// !code #{lib}"))
  else
    js
  end
end

#save_js(rel_path, key, value) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/couch_docs/design_directory.rb', line 76

def save_js(rel_path, key, value)
  if value.is_a? Hash
    value.each_pair do |k, v|
      next if k == '_id'
      self.save_js([rel_path, key].compact.join('/'), k, v)

    end
  else
    path = couch_view_dir + '/' + rel_path
    FileUtils.mkdir_p(path)

    file = File.new("#{path}/#{key.gsub(/\//, '%2F')}.js", "w+")
    file.write(remove_code_macros(value))
    file.close
  end
end

#store_document(doc) ⇒ Object

Store



71
72
73
74
# File 'lib/couch_docs/design_directory.rb', line 71

def store_document(doc)
  id = doc['_id']
  self.save_js(nil, id, doc)
end

#to_hashObject

Load



30
31
32
33
34
35
36
# File 'lib/couch_docs/design_directory.rb', line 30

def to_hash
  Dir["#{couch_view_dir}/**/*.js"].inject({}) do |memo, filename|
    DesignDirectory.
      a_to_hash(expand_file(filename)).
      deep_merge(memo)
  end
end