module Babylonia::VirtualAttributes

Method missing implementation for virtual attributes @author Beat Richartz @version 0.0.2 @since 0.0.1

Public Instance Methods

method_missing(meth, *args, &block) click to toggle source

Define method missing to be able to access a language directly Enables to call a language virtual attribute directly @note Since the virtual attribute is called directly, there is no fallback on this unless you set it to true @example Call a getter directly

object.field_de #=> 'DEUTSCH'

@example Call a setter directly

object.field_de = 'DEUTSCH'

@example Call an untranslated field

object.field_it #=> nil

@example Call a field with fallback

object.field_it(fallback: true)
Calls superclass method
# File lib/babylonia/class_methods.rb, line 103
def method_missing meth, *args, &block
  if parts = extract_locale_method_parts(meth)
    parts[2] ? send(parts[0] + parts[2].to_s, { parts[1].to_sym => args.first }) : send(parts[0], parts[1].to_sym, args.first || {})
  else
    super(meth, *args, &block)
  end
end

Private Instance Methods

extract_locale_method_parts(meth) click to toggle source
# File lib/babylonia/class_methods.rb, line 113
def extract_locale_method_parts meth
  if (parts = meth.to_s.match(/\A(\w+)_(\w+)(=)?\z/).to_a[1..3]) && localized?(parts[0]) && has_available_locale?(parts[1])
    parts
  end
end