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
47
48
49
50
51
52
53
54
55
56
# File 'lib/couch_docs/design_directory.rb', line 38

def expand_file(filename)
  if filename =~ /\.js$/
    name_value_pair = [
     File.basename(filename, '.js'),
     read_js_value(filename)
    ]
  elsif filename =~ /\.json$/
    name_value_pair = [
     File.basename(filename, '.json'),
     read_json_value(filename)
    ]
  end

  name_value_pair[0].gsub!(/%2F/, '/')

  File.dirname(filename).
    gsub(/#{couch_view_dir}\/?/, '').
    split(/\//) + name_value_pair
end

#process_code_macro(line) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/couch_docs/design_directory.rb', line 69

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



79
80
81
# File 'lib/couch_docs/design_directory.rb', line 79

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

#read_js_value(filename) ⇒ Object



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

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

#read_json_value(filename) ⇒ Object



58
59
60
# File 'lib/couch_docs/design_directory.rb', line 58

def read_json_value(filename)
  JSON.parse(File.new(filename).read)
end

#remove_code_macros(js) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/couch_docs/design_directory.rb', line 98

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



90
91
92
93
94
95
96
# File 'lib/couch_docs/design_directory.rb', line 90

def save_js(rel_path, key, value)
  if value.is_a? Hash
    save_js_hash(rel_path, key, value)
  else
    save_js_value(rel_path, key, value)
  end
end

#store_document(doc) ⇒ Object

Store



85
86
87
88
# File 'lib/couch_docs/design_directory.rb', line 85

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,json}"].inject({}) do |memo, filename|
    DesignDirectory.
      a_to_hash(expand_file(filename)).
      deep_merge(memo)
  end
end