Class: Dayvan::View

Inherits:
Object
  • Object
show all
Defined in:
lib/dayvan/view.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ View

Returns a new instance of View.



17
18
19
# File 'lib/dayvan/view.rb', line 17

def initialize(path)
  self.path = path
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/dayvan/view.rb', line 4

def path
  @path
end

Class Method Details

.all(root) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/dayvan/view.rb', line 6

def self.all(root)
  root.exist? && Dir[root.join('**/*.coffee')].
    reject {|f| File.directory?(f)}. # Allows symlinks
    inject({}) {|all, f|
      view = new(f)
      all[view.name] = view
      all
    }
end

Instance Method Details

#compileObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dayvan/view.rb', line 29

def compile
  parse_tree = CoffeeScript::Parser.new.parse(src)

  ['map', 'reduce'].inject({}) do |memo, type|
    # Finds tops level assignemnts whose bound variable is map/reduce.
    assignment_node = parse_tree.children.find {|node|
      node.is_a?(CoffeeScript::AssignNode) && node.variable.base == type
    }

    if assignment_node
      memo[type] = assignment_node.value.compile(:indent => '')
    end
    memo
  end
end

#nameObject



21
22
23
# File 'lib/dayvan/view.rb', line 21

def name
  File.basename(path, '.*')
end

#srcObject



25
26
27
# File 'lib/dayvan/view.rb', line 25

def src
  File.read(path)
end

#to_jsonObject



45
46
47
# File 'lib/dayvan/view.rb', line 45

def to_json
  compile.to_json
end