Skip to content

Commit

Permalink
Correctly handle keyword arguments in method_missing
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed May 26, 2020
1 parent fdaf7b0 commit 742408a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/mobility/plugins/fallthrough_accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ def self.apply(attributes, option)
def initialize(*attributes)
method_name_regex = /\A(#{attributes.join('|')})_([a-z]{2}(_[a-z]{2})?)(=?|\??)\z/.freeze

define_method :method_missing do |method_name, *arguments, **options, &block|
define_method :method_missing do |method_name, *args, &block|
if method_name =~ method_name_regex
attribute = $1.to_sym
attribute_method = "#{$1}#{$4}"
locale, suffix = $2.split('_')
locale = "#{locale}-#{suffix.upcase}" if suffix
public_send("#{attribute}#{$4}", *arguments, **options, locale: locale.to_sym)
if $4 == '=' # writer
public_send(attribute_method, args[0], **(args[1] || {}), locale: locale.to_sym)
else # reader
public_send(attribute_method, **(args[0] || {}), locale: locale.to_sym)
end
else
super(method_name, *arguments, **options, &block)
super(method_name, *args, &block)
end
end

Expand Down

0 comments on commit 742408a

Please sign in to comment.