Class: Lux::View::Cell

Inherits:
Object show all
Defined in:
lib/lux/view/cell.rb

Direct Known Subclasses

ViewCell

Defined Under Namespace

Classes: Loader

Constant Summary collapse

@@cache =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, vars = {}) ⇒ Cell

Returns a new instance of Cell.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lux/view/cell.rb', line 39

def initialize parent, vars={}
  @_parent = parent

  Object.class_callback :before, self

  vars.each { |k,v| instance_variable_set "@#{k}", v}

  # add runtime file reference
  if m = self.class.instance_methods(false).first
    src = method(m).source_location[0].split(':').first
    src = src.sub(Lux.root.to_s+'/', '')
    Lux.log " #{src}" unless Lux.current.files_in_use.include?(src)
    Lux.current.files_in_use src
  end
end

Class Method Details

.base_folderObject

CityCell.folder -> “./app/cells/city”



20
21
22
23
24
# File 'lib/lux/view/cell.rb', line 20

def base_folder
  name = instance_methods(false).first || dir('Can not find method')
  file = instance_method(name).source_location
  File.dirname file.first
end

.get(name, parent, vars = {}) ⇒ Object



26
27
28
29
30
# File 'lib/lux/view/cell.rb', line 26

def get name, parent, vars={}
  w = ('%sCell' % name.to_s.classify).constantize
  w = w.new parent, vars
  w
end

Instance Method Details

#cell(name = nil) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/lux/view/cell.rb', line 95

def cell name=nil
  return parent.cell unless name

  w = ('%sCell' % name.to_s.classify).constantize
  w = w.new @_parent
  w
end

#once(id = nil) ⇒ Object

execute block only once per page



90
91
92
93
# File 'lib/lux/view/cell.rb', line 90

def once id=nil
  id ||= self.class
  Lux.current.once('cell-once-%s' % id) { yield }
end

#parent(&block) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/lux/view/cell.rb', line 55

def parent &block
  if block_given?
    @_parent.instance_exec &block
  else
    @_parent
  end
end

#tag(name = nil, opts = {}, data = nil) ⇒ Object

tag :div, { ‘class’=>‘iform’ } do



82
83
84
85
86
87
# File 'lib/lux/view/cell.rb', line 82

def tag name=nil, opts={}, data=nil
  return HtmlTagBuilder unless name

  data = yield(opts) if block_given?
  HtmlTagBuilder.tag name, opts, data
end

#template(name = :cellm, &block) ⇒ Object

if block is passed, template render will be passed as an argument



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lux/view/cell.rb', line 64

def template name=:cellm, &block
  tpl = 'cell-tpl-%s-%s' % [self.class, name]

  tpl = Lux.ram_cache(tpl) do
    file = '%s/%s.haml' % [self.class.base_folder, name]
    file = file.sub(Lux.root.to_s+'/', '')

    Lux.log ' ' + file unless Lux.current.files_in_use(file)

    Tilt[:haml].new { File.read(file) }
  end

  data = tpl.render(self)
  data = block.call(data) if block
  data
end