Class: Makura::Layout

Inherits:
Object
  • Object
show all
Defined in:
lib/makura/layout.rb

Constant Summary collapse

PATH =
[
  './couch',
  File.join(Makura::ROOT, '../couch')
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, design) ⇒ Layout

Returns a new instance of Layout.



10
11
12
13
14
15
# File 'lib/makura/layout.rb', line 10

def initialize(name, design)
  @name, @design = name, design
  @design[name] = self
  @map = @reduce = nil
  @options = {}
end

Instance Attribute Details

#designObject

Returns the value of attribute design.



3
4
5
# File 'lib/makura/layout.rb', line 3

def design
  @design
end

#mapObject

Returns the value of attribute map.



3
4
5
# File 'lib/makura/layout.rb', line 3

def map
  @map
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/makura/layout.rb', line 3

def name
  @name
end

#reduceObject

Returns the value of attribute reduce.



3
4
5
# File 'lib/makura/layout.rb', line 3

def reduce
  @reduce
end

Instance Method Details

#common_load(type, file_or_function) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/makura/layout.rb', line 37

def common_load(type, file_or_function)
  return unless file_or_function

  case file_or_function
  when /function\(.*\)/, /^_(sum|count|stats)$/
    function = file_or_function
  else
    parts = file_or_function.to_s.split('::')
    file = parts.pop
    filename = File.join(parts, type.to_s, "#{file}.js")

    if pathname = PATH.find{|pa| File.file?(File.join(pa, filename)) }
      function = File.read(File.join(pathname, filename))
    end
  end

  instance_variable_set("@#{type}", function) if function
end

#load_map(file_or_function) ⇒ Object



29
30
31
# File 'lib/makura/layout.rb', line 29

def load_map(file_or_function)
  common_load(:map, file_or_function)
end

#load_proto_map(file_or_function, replace = {}) ⇒ Object



17
18
19
20
21
# File 'lib/makura/layout.rb', line 17

def load_proto_map(file_or_function, replace = {})
  return unless common_load(:proto_map, file_or_function)
  replace.each{|from, to| @proto_map.gsub!(/"\{\{#{from}\}\}"/, to) }
  @map = @proto_map
end

#load_proto_reduce(file_or_function, replace = {}) ⇒ Object



23
24
25
26
27
# File 'lib/makura/layout.rb', line 23

def load_proto_reduce(file_or_function, replace = {})
  return unless common_load(:proto_reduce, file_or_function)
  replace.each{|from, to| @proto_reduce.gsub!(/"\{\{#{from}\}\}"/, to) }
  @reduce = @proto_reduce
end

#load_reduce(file_or_function) ⇒ Object



33
34
35
# File 'lib/makura/layout.rb', line 33

def load_reduce(file_or_function)
  common_load(:reduce, file_or_function)
end

#saveObject



56
57
58
59
# File 'lib/makura/layout.rb', line 56

def save
  @design[@name] = self.to_hash
  @design.save
end

#to_hashObject



61
62
63
# File 'lib/makura/layout.rb', line 61

def to_hash
  {:map => @map, :reduce => @reduce, :makura_options => @options}
end