Class: Rodauth::Feature

Inherits:
Module
  • Object
show all
Defined in:
lib/rodauth.rb

Constant Summary collapse

DEPRECATED_ARGS =

:nocov:

[]
DEFAULT_REDIRECT_BLOCK =
proc{default_redirect}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#allowed_undefined_configuration_methodsObject

Returns the value of attribute allowed_undefined_configuration_methods.



148
149
150
# File 'lib/rodauth.rb', line 148

def allowed_undefined_configuration_methods
  @allowed_undefined_configuration_methods
end

#configurationObject

Returns the value of attribute configuration.



147
148
149
# File 'lib/rodauth.rb', line 147

def configuration
  @configuration
end

#dependenciesObject

Returns the value of attribute dependencies.



145
146
147
# File 'lib/rodauth.rb', line 145

def dependencies
  @dependencies
end

#feature_nameObject

Returns the value of attribute feature_name.



144
145
146
# File 'lib/rodauth.rb', line 144

def feature_name
  @feature_name
end

#instance_variables_usedObject (readonly)

Returns the value of attribute instance_variables_used.



150
151
152
# File 'lib/rodauth.rb', line 150

def instance_variables_used
  @instance_variables_used
end

#internal_request_methodsObject (readonly)

Returns the value of attribute internal_request_methods.



149
150
151
# File 'lib/rodauth.rb', line 149

def internal_request_methods
  @internal_request_methods
end

#routesObject

Returns the value of attribute routes.



146
147
148
# File 'lib/rodauth.rb', line 146

def routes
  @routes
end

Class Method Details

.define(name, constant = nil, &block) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rodauth.rb', line 178

def self.define(name, constant=nil, &block)
  feature = new
  feature.dependencies = []
  feature.routes = []
  feature.feature_name = name
  configuration = feature.configuration = FeatureConfiguration.new
  feature.module_eval(&block)
  configuration.def_configuration_methods(feature)

  # :nocov:
  if constant
  # :nocov:
    Rodauth.const_set(constant, feature)
    Rodauth::FeatureConfiguration.const_set(constant, configuration)
  end

  FEATURES[name] = feature
end

Instance Method Details

#additional_form_tags(name = feature_name) ⇒ Object



311
312
313
# File 'lib/rodauth.rb', line 311

def additional_form_tags(name=feature_name)
  auth_value_method(:"#{name}_additional_form_tags", nil)
end

#auth_cached_method(meth, iv = :"@#{meth}") ⇒ Object

:nocov:



350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/rodauth.rb', line 350

def auth_cached_method(meth, iv=:"@#{meth}") # :nodoc:
  # Non-shape friendly historical method.
  # RODAUTH3: Remove
  umeth = :"_#{meth}"
  define_method(meth) do
    if instance_variable_defined?(iv)
      instance_variable_get(iv)
    else
      instance_variable_set(iv, send(umeth))
    end
  end
  alias_method(meth, meth)
  auth_private_methods(meth)
end

#auth_value_method(meth, value) ⇒ Object



325
326
327
328
# File 'lib/rodauth.rb', line 325

def auth_value_method(meth, value)
  define_method(meth){value}
  auth_value_methods(meth)
end

#cached_auth_method(meth, iv = :"@#{meth}") ⇒ Object

Auth caching method that treats a nil instance variable as not being cached. If nil is a valid value for the instance variable, do not use this method, use a regular auth_method and handle caching manually.



339
340
341
342
343
344
345
346
347
# File 'lib/rodauth.rb', line 339

def cached_auth_method(meth, iv=:"@#{meth}")
  umeth = :"_#{meth}"
  define_method(meth) do
    v = instance_variable_get(iv)
    v.nil? ? instance_variable_set(iv, send(umeth)) : v
  end
  alias_method(meth, meth)
  auth_private_methods(meth)
end

#configuration_module_eval(&block) ⇒ Object



205
206
207
# File 'lib/rodauth.rb', line 205

def configuration_module_eval(&block)
  configuration.module_eval(&block)
end

#def_deprecated_alias(new, old) ⇒ Object

:nocov:



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/rodauth.rb', line 216

def def_deprecated_alias(new, old)
  configuration_module_eval do
    define_method(old) do |*a, &block|
      warn("Deprecated #{old} method used during configuration, switch to using #{new}", *DEPRECATED_ARGS)
      send(new, *a, &block)
    end
  end
  define_method(old) do
    warn("Deprecated #{old} method called at runtime, switch to using #{new}", *DEPRECATED_ARGS)
    send(new)
  end
end

#depends(*deps) ⇒ Object



270
271
272
# File 'lib/rodauth.rb', line 270

def depends(*deps)
  dependencies.concat(deps)
end

#email(type, subject, opts = OPTS) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/rodauth.rb', line 284

def email(type, subject, opts = OPTS)
  subject_method = :"#{type}_email_subject"
  body_method = :"#{type}_email_body"
  create_method = :"create_#{type}_email"
  send_method = :"send_#{type}_email"

  translatable_method subject_method, subject
  auth_methods create_method, send_method

  body_template = "#{type.to_s.tr('_', '-')}-email"
  if opts[:translatable]
    auth_value_methods body_method
    define_method(body_method){translate(body_method, render(body_template))}
  else
    auth_methods body_method
    define_method(body_method){render(body_template)}
  end

  define_method(create_method) do
    create_email(send(subject_method), send(body_method))
  end

  define_method(send_method) do
    send_email(send(create_method))
  end
end

#flash_key(meth, value) ⇒ Object



320
321
322
323
# File 'lib/rodauth.rb', line 320

def flash_key(meth, value)
  define_method(meth){normalize_session_or_flash_key(value)}
  auth_value_methods(meth)
end

#internal_request_method(name = feature_name) ⇒ Object



197
198
199
# File 'lib/rodauth.rb', line 197

def internal_request_method(name=feature_name)
  (@internal_request_methods ||= []) << name
end

#loaded_templates(v) ⇒ Object



263
264
265
266
267
268
# File 'lib/rodauth.rb', line 263

def loaded_templates(v)
  define_method(:loaded_templates) do
    super().concat(v)
  end
  private :loaded_templates
end

#redirect(name = feature_name, &block) ⇒ Object



230
231
232
233
234
235
# File 'lib/rodauth.rb', line 230

def redirect(name=feature_name, &block)
  meth = :"#{name}_redirect"
  block ||= DEFAULT_REDIRECT_BLOCK
  define_method(meth, &block)
  auth_value_methods meth
end

#response(name = feature_name) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rodauth.rb', line 247

def response(name=feature_name)
  meth = :"#{name}_response"
  overridable_meth = :"_#{meth}"
  notice_flash_meth = :"#{name}_notice_flash"
  redirect_meth = :"#{name}_redirect"
  define_method(overridable_meth) do
    set_notice_flash send(notice_flash_meth)
    redirect send(redirect_meth)
  end
  define_method(meth) do
    require_response(overridable_meth)
  end
  private overridable_meth, meth
  auth_private_methods meth
end

#route(name = feature_name, default = name.to_s.tr('_', '-'), &block) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rodauth.rb', line 152

def route(name=feature_name, default=name.to_s.tr('_', '-'), &block)
  route_meth = :"#{name}_route"
  auth_value_method route_meth, default

  define_method(:"#{name}_path"){|opts=OPTS| route_path(send(route_meth), opts) if send(route_meth)}
  define_method(:"#{name}_url"){|opts=OPTS| route_url(send(route_meth), opts) if send(route_meth)}

  handle_meth = :"handle_#{name}"
  internal_handle_meth = :"_#{handle_meth}"
  before route_meth
  define_method(internal_handle_meth, &block)

  define_method(handle_meth) do
    request.is send(route_meth) do
      @current_route = name
      check_csrf if check_csrf?
      _around_rodauth do
        before_rodauth
        send(internal_handle_meth, request)
      end
    end
  end

  routes << handle_meth
end

#session_key(meth, value) ⇒ Object



315
316
317
318
# File 'lib/rodauth.rb', line 315

def session_key(meth, value)
  define_method(meth){convert_session_key(value)}
  auth_value_methods(meth)
end

#translatable_method(meth, value) ⇒ Object



330
331
332
333
# File 'lib/rodauth.rb', line 330

def translatable_method(meth, value)
  define_method(meth){translate(meth, value)}
  auth_value_methods(meth)
end

#uses_instance_variables(*ivs) ⇒ Object



201
202
203
# File 'lib/rodauth.rb', line 201

def uses_instance_variables(*ivs)
  @instance_variables_used = ivs.freeze
end

#view(page, title, name = feature_name) ⇒ Object



237
238
239
240
241
242
243
244
245
# File 'lib/rodauth.rb', line 237

def view(page, title, name=feature_name)
  meth = :"#{name}_view"
  title_meth = :"#{name}_page_title"
  translatable_method(title_meth, title)
  define_method(meth) do
    view(page, send(title_meth))
  end
  auth_methods meth
end