Class: ViewCell

Inherits:
Object
  • Object
show all
Includes:
HtmlTag
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.



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

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

Class Method Details

.before(&block) ⇒ Object

can be called as a block or a method block do … def block; super; …



62
63
64
65
66
67
# File 'lib/view-cell/class.rb', line 62

def before &block
  define_method :before do
    super() if self.class != ViewCell
    instance_exec &block
  end
end

.cell(parent, *args) ⇒ Object

cell @users

cell @user

cell.user.render @user

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



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
# File 'lib/view-cell/class.rb', line 33

def cell parent, *args
  if args.first
    # covert to list of objects
    unless [String, Symbol, Array].include?(args[0].class)
      args[0] = [args.first]
    end

    out =
    if args.first.class == Array
      # cell @users
      args.first.map do |object|
        name = object.class.to_s.underscore.to_sym
        ViewCell.get(parent, name).render object
      end.join('')
    else
      # cell(:user, user: @user).profile
      ViewCell.get parent, *args
    end

    out.respond_to?(:html_safe) ? out.html_safe : out
  else
    # cell.user.profile
    ViewCell::Proxy.new(parent)
  end
end

.css(text = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/view-cell/class.rb', line 69

def css text = nil
  if text      
    require 'sassc' unless Object.const_defined?('SassC')

    unless text.include?('{')
      dir_name = self.template_root || File.dirname(caller[0].split(':')[0])
      class_part = to_s.underscore.sub(/_cell$/, '')
      dir_name = dir_name % class_part if dir_name.include?('%s')
      css_file = Pathname.new(dir_name).join(text)
      text = css_file.read
    end

    key = caller[0].split(':in ')[0]
    DATA[:css][key] = SassC::Engine.new(text, style: :compact).render.gsub(/\n+/, $/).chomp
  else
    DATA[:css].inject([]) do |list, el|
      name = el[0].split('/').last.split('.')[0].gsub('_', '-')
      list.push("/* #{name} */\n" + el[1])
    end.join("\n\n")
  end
end

.delegate(*list) ⇒ Object

delegate current scope methods to parent binding delegate :image_tag, :request, params



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

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)



15
16
17
18
19
# File 'lib/view-cell/class.rb', line 15

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

Instance Method Details

#beforeObject

called every time for every method in a class



11
12
# File 'lib/view-cell/instance.rb', line 11

def before
end

#cell(*args) ⇒ Object

call



24
25
26
# File 'lib/view-cell/instance.rb', line 24

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

#parent(&block) ⇒ Object

access parent scope



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

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

#template(name) ⇒ Object

render template by name



29
30
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
# File 'lib/view-cell/instance.rb', line 29

def template name
  path = if template_root = self.class.template_root
    [template_root, name].join '/'
  else
    [File.dirname(caller[0]), name].join '/'
  end

  class_part = self.class.to_s.underscore.sub(/_cell$/, '')
  path = path % class_part if path.include?('%s')

  RENDER_CACHE.delete(path) if _development?
  RENDER_CACHE[path] ||= begin
    file_name = ''

    # find extension if one not provided
    if path =~ /\.\w{2,4}$/
      file_name = path
    else
      if (file = Dir['%s.*' % path].first)
        file_name = file
      end
    end

    unless File.exist?(file_name)
      raise ArgumentError, 'Template "%s.*" not found' % [path.sub(Dir.pwd, '.')]
    end

    Tilt.new(file_name)
  end
  RENDER_CACHE[path].render(self)
end