Class: Lux::Cell

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

Overview

Cells can be called in few ways Cell.call path Cell.action action_name, path Cell.new.action_name *args

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCell

Returns a new instance of Cell.



54
55
56
57
58
# File 'lib/lux/cell/cell.rb', line 54

def initialize
  # before and after should be exected only once
  @executed_filters = {}
  @base_template = self.class.to_s.include?('::') ? self.class.to_s.sub(/Cell$/,'').underscore : self.class.to_s.sub(/Cell$/,'').downcase
end

Instance Attribute Details

#cell_actionObject (readonly)

INSTANCE METHODS



52
53
54
# File 'lib/lux/cell/cell.rb', line 52

def cell_action
  @cell_action
end

Class Method Details

.action(*args) ⇒ Object

simple shortcut allows direct call to action, bypasing call



45
46
47
# File 'lib/lux/cell/cell.rb', line 45

def action *args
  new.action(*args)
end

.callObject

class call method, should not be overridden



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lux/cell/cell.rb', line 21

def call
  Lux.current.files_in_use.push "app/cells/#{self.to_s.underscore}.rb"

  cell = new
  cell.filter :before
  cell.call

  return if Lux.current.response.body

  # we want to exec filter after the call
  cell.filter :before_action
end

.mock(*args) ⇒ Object

create mock function, to enable template rendering mock :index, :login



36
37
38
39
40
41
42
# File 'lib/lux/cell/cell.rb', line 36

def mock *args
  args.each do |el|
    define_method el do
      true
    end
  end
end

Instance Method Details

#action(method_name, *args) ⇒ Object

action(:show, 2) action(:select’, [‘users’])

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/lux/cell/cell.rb', line 82

def action method_name, *args
  raise ArgumentError.new('Cell action called with blank action name argument') if method_name.blank?

  # maybe before filter rendered page
  return if response.body

  method_name = method_name.to_s.gsub('-', '_').gsub(/[^\w]/, '')

  Lux.log " #{self.class.to_s}(:#{method_name})".light_blue
  Lux.current.files_in_use.push "app/cells/#{self.class.to_s.underscore}.rb"

  @cell_action = method_name

  unless respond_to? method_name
    raise NotFoundError.new('Method %s not found' % method_name) unless Lux.config(:show_server_errors)

    list = methods - Lux::Cell.instance_methods
    err = [%[No instance method "#{method_name}" found in class "#{self.class.to_s}"]]
    err.push ["Expected so see def show(id) ..."] if method_name == 'show!'
    err.push %[You have defined \n- #{(list).join("\n- ")}]
    return Lux.error(err.join("\n\n"))
  end

  filter :before
  return if response.body

  filter :before_action
  return if response.body

  send method_name, *args
  filter :after

  return if response.body
  render
end

#cache(*args, &block) ⇒ Object



76
77
78
# File 'lib/lux/cell/cell.rb', line 76

def cache *args, &block
  Lux.cache.fetch *args, &block
end

#callObject

default call method, should be overitten expects arguments as flat array usually called by router



63
64
65
# File 'lib/lux/cell/cell.rb', line 63

def call
  action(:index)
end

#filter(fiter_name) ⇒ Object

execute before and after filters, only once



68
69
70
71
72
73
74
# File 'lib/lux/cell/cell.rb', line 68

def filter fiter_name
  # move this to ClassCallbacks class?
  return if @executed_filters[fiter_name]
  @executed_filters[fiter_name] = true

  ClassCallbacks.execute(self, fiter_name)
end

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

render :show, id render :index render ‘main/root/index’ render :profile, name:‘Dux’ render text: ‘ok’



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/lux/cell/cell.rb', line 123

def render name=nil, opts={}
  return if response.body
  return if @no_render

  filter :before_render

  if name.is_hash?
    opts = name
    name = nil
  end

  opts[:template] = name if name

  render_resolve_body opts

  Lux.cache.set(opts[:cache], response.body) if opts[:cache]
end

#render_partObject

renders template to string



142
143
144
# File 'lib/lux/cell/cell.rb', line 142

def render_part
  Lux::Template.render_part("#{@base_template}/#{@cell_action}", instance_variables_hash, namespace)
end

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



146
147
148
149
# File 'lib/lux/cell/cell.rb', line 146

def render_to_string name=nil, opts={}
  opts[:set_page_body] = false
  render name, opts
end

#send_fileObject



151
152
153
# File 'lib/lux/cell/cell.rb', line 151

def send_file

end