Class: CouchDesignDocs::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_design_docs/directory.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Directory



25
26
27
28
# File 'lib/couch_design_docs/directory.rb', line 25

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.



14
15
16
# File 'lib/couch_design_docs/directory.rb', line 14

def couch_view_dir
  @couch_view_dir
end

Class Method Details

.a_to_hash(a) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/couch_design_docs/directory.rb', line 16

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_design_docs/directory.rb', line 38

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

#to_hashObject



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

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