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

#configurationObject

Returns the value of attribute configuration.



127
128
129
# File 'lib/rodauth.rb', line 127

def configuration
  @configuration
end

#dependenciesObject

Returns the value of attribute dependencies.



125
126
127
# File 'lib/rodauth.rb', line 125

def dependencies
  @dependencies
end

#feature_nameObject

Returns the value of attribute feature_name.



124
125
126
# File 'lib/rodauth.rb', line 124

def feature_name
  @feature_name
end

#instance_variables_usedObject (readonly)

Returns the value of attribute instance_variables_used.



129
130
131
# File 'lib/rodauth.rb', line 129

def instance_variables_used
  @instance_variables_used
end

#internal_request_methodsObject (readonly)

Returns the value of attribute internal_request_methods.



128
129
130
# File 'lib/rodauth.rb', line 128

def internal_request_methods
  @internal_request_methods
end

#routesObject

Returns the value of attribute routes.



126
127
128
# File 'lib/rodauth.rb', line 126

def routes
  @routes
end

Class Method Details

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



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

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



290
291
292
# File 'lib/rodauth.rb', line 290

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

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

:nocov:



329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/rodauth.rb', line 329

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



304
305
306
307
# File 'lib/rodauth.rb', line 304

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.



318
319
320
321
322
323
324
325
326
# File 'lib/rodauth.rb', line 318

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



184
185
186
# File 'lib/rodauth.rb', line 184

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

#def_deprecated_alias(new, old) ⇒ Object

:nocov:



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/rodauth.rb', line 195

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



249
250
251
# File 'lib/rodauth.rb', line 249

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

#email(type, subject, opts = {}) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/rodauth.rb', line 263

def email(type, subject, 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



299
300
301
302
# File 'lib/rodauth.rb', line 299

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



176
177
178
# File 'lib/rodauth.rb', line 176

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

#loaded_templates(v) ⇒ Object



242
243
244
245
246
247
# File 'lib/rodauth.rb', line 242

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

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



209
210
211
212
213
214
# File 'lib/rodauth.rb', line 209

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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/rodauth.rb', line 226

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rodauth.rb', line 131

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={}| route_path(send(route_meth), opts) if send(route_meth)}
  define_method(:"#{name}_url"){|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



294
295
296
297
# File 'lib/rodauth.rb', line 294

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

#translatable_method(meth, value) ⇒ Object



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

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

#uses_instance_variables(*ivs) ⇒ Object



180
181
182
# File 'lib/rodauth.rb', line 180

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

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



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

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