Module: Babylonia::VirtualAttributes

Defined in:
lib/babylonia/class_methods.rb

Overview

Method missing implementation for virtual attributes

Author:

  • Beat Richartz

Since:

  • 0.0.1

Version:

  • 0.0.2

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Note:

Since the virtual attribute is called directly, there is no fallback on this unless you set it to true

Define method missing to be able to access a language directly Enables to call a language virtual attribute directly

Examples:

Call a getter directly

object.field_de #=> 'DEUTSCH'

Call a setter directly

object.field_de = 'DEUTSCH'

Call an untranslated field

object.field_it #=> nil

Call a field with fallback

object.field_it(fallback: true)

Since:

  • 0.0.1



103
104
105
106
107
108
109
# 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