Basic
# The |obj| is argument, for in this example, it will represents after_save(obj).
[:after_save].each do |method|
define_method method do |obj|
@title, @body = obj.title, obj.body
mail(to: @user.email, subject: 'Hello')
end
end
Variation
1. Status Encapsulation
# also, you can do this.
class Mode
[:low, :middle, :high].each do |level|
define_method(level) do
@state = level
end
attr_reader :state
end
end
log = Mode.new
log.low
log.state #=>low
log.middle
log.state #=>middle
log.high
log.state #=>high
2. only the name is different
![n350071 image](https://res.cloudinary.com/practicaldev/image/fetch/s--pbzTu4Iv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--GyggfQgU--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/72353/94a227c6-1417-46e0-9fa1-2d48ffadc37e.png)
organize the authorizations of the banken/pundit gem with define_method
n350071🇯🇵 ・ Sep 25 '19 ・ 1 min read
#rails
#gem
Top comments (0)