Class: ViewCell

Inherits:
Object
  • Object
show all
Defined in:
lib/view-cell/class.rb,
lib/view-cell/proxy.rb,
lib/view-cell/instance.rb

Defined Under Namespace

Modules: ProxyMethod Classes: Proxy

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, vars = {}) ⇒ ViewCell

Returns a new instance of ViewCell.



6
7
8
9
10
# File 'lib/view-cell/instance.rb', line 6

def initialize parent=nil, vars={}
  @_parent = parent
  vars.each { |k,v| instance_variable_set "@#{k}", v}
  before
end

Class Method Details

.cell(parent, *args) ⇒ Object

cell.user.profile cell(:user, user: @user).profile



20
21
22
23
24
25
26
# File 'lib/view-cell/class.rb', line 20

def cell parent, *args
  if args.first
    ViewCell.get(parent, *args)
  else
    ViewCell::Proxy.new(parent)
  end
end

.delegate(*list) ⇒ Object

delegate current scope methods to parent binding



12
13
14
15
16
# File 'lib/view-cell/class.rb', line 12

def delegate *list
  list.each do |el|
    define_method(el) { |*args, &block| parent.send(el, *args, &block) }
  end
end

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

load cell based on a name, pass context and optional vars ViewCell.get(:user, self) -> UserCell.new(self)



5
6
7
8
9
# File 'lib/view-cell/class.rb', line 5

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

.template_root(name = nil) ⇒ Object

set or get template root directory



29
30
31
32
33
34
35
# File 'lib/view-cell/class.rb', line 29

def template_root name=nil
  if name
    self.class.instance_variable_set :@template_root, name
  else
    self.class.instance_variable_get :@template_root
  end
end

Instance Method Details

#beforeObject

called every time for every method in a class



13
14
# File 'lib/view-cell/instance.rb', line 13

def before
end

#cell(*args) ⇒ Object

call



26
27
28
# File 'lib/view-cell/instance.rb', line 26

def cell *args
  ViewCell.get @_parent, *args
end

#parent(&block) ⇒ Object

access parent scope



17
18
19
20
21
22
23
# File 'lib/view-cell/instance.rb', line 17

def parent &block
  if block
    @_parent.instance_exec self, &block
  else
    @_parent
  end
end

#render(name) ⇒ Object

render template by name



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/view-cell/instance.rb', line 31

def render name
  template_root = self.class.template_root
  class_part    = self.class.to_s.underscore.sub(/_cell$/, '')

  if template_root
    name = [template_root, name].join '/'
  elsif name.is_a?(Symbol)
    name = './app/views/cells/%s/%s' % [class_part, name]
  elsif name.to_s =~ /^\w/
    name = './app/views/cells/%s' % name
  end

  name = name % class_part if name.include?('%s')

  RENDER_CACHE.delete(name) if _development?

  RENDER_CACHE[name] ||= proc do
    # find extension if one not provided
    file_name = name

    unless name =~ /\.\w{2,4}$/
      if (file = Dir['%s*' % name].first)
        file_name = file
      end
    end

    unless File.exist?(file_name)
      raise ArgumentError, 'Template "%s.*" not found' % name
    end

    Tilt.new(file_name)
  end.call

  out = RENDER_CACHE[name].render(self)
  out.respond_to?(:html_safe) ? out.html_safe : out
end