Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use module_eval to define locale accessors #219

Merged
merged 2 commits into from
May 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/mobility/plugins/locale_accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,32 @@ def define_reader(name, locale)
warning_message = "locale passed as option to locale accessor will be ignored"
normalized_locale = Mobility.normalize_locale(locale)

define_method "#{name}_#{normalized_locale}" do |**options|
module_eval <<-EOM, __FILE__, __LINE__ + 1
def #{name}_#{normalized_locale}(options = {})
return super() if options.delete(:super)
warn warning_message if options[:locale]
public_send(name, **options, locale: locale)
warn "#{warning_message}" if options[:locale]
#{name}(**options, locale: :'#{locale}')
end

define_method "#{name}_#{normalized_locale}?" do |**options|
def #{name}_#{normalized_locale}?(options = {})
return super() if options.delete(:super)
warn warning_message if options[:locale]
public_send("#{name}?", **options, locale: locale)
warn "#{warning_message}" if options[:locale]
#{name}?(**options, locale: :'#{locale}')
end
EOM
end

def define_writer(name, locale)
warning_message = "locale passed as option to locale accessor will be ignored"
normalized_locale = Mobility.normalize_locale(locale)

define_method "#{name}_#{normalized_locale}=" do |value, **options|
module_eval <<-EOM, __FILE__, __LINE__ + 1
def #{name}_#{normalized_locale}=(value, options = {})
return super(value) if options.delete(:super)
warn warning_message if options[:locale]
public_send("#{name}=", value, **options, locale: locale)
warn "#{warning_message}" if options[:locale]
public_send(:#{name}=, value, **options, locale: :'#{locale}')
end
EOM
end
end
end
Expand Down