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.



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

def configuration
  @configuration
end

#dependenciesObject

Returns the value of attribute dependencies.



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

def dependencies
  @dependencies
end

#feature_nameObject

Returns the value of attribute feature_name.



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

def feature_name
  @feature_name
end

#internal_request_methodsObject (readonly)

Returns the value of attribute internal_request_methods.



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

def internal_request_methods
  @internal_request_methods
end

#routesObject

Returns the value of attribute routes.



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

def routes
  @routes
end

Class Method Details

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



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

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



284
285
286
# File 'lib/rodauth.rb', line 284

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

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



308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/rodauth.rb', line 308

def auth_cached_method(meth, iv=:"@#{meth}")
  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



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

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

#configuration_module_eval(&block) ⇒ Object



178
179
180
# File 'lib/rodauth.rb', line 178

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

#def_deprecated_alias(new, old) ⇒ Object

:nocov:



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rodauth.rb', line 189

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



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

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

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



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/rodauth.rb', line 257

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



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

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



174
175
176
# File 'lib/rodauth.rb', line 174

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

#loaded_templates(v) ⇒ Object



236
237
238
239
240
241
# File 'lib/rodauth.rb', line 236

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

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



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

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



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rodauth.rb', line 220

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



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

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



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

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

#translatable_method(meth, value) ⇒ Object



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

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

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



210
211
212
213
214
215
216
217
218
# File 'lib/rodauth.rb', line 210

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