Method: Multilang.register
- Defined in:
- lib/multilang.rb
.register(mod, options = {}) { ... } ⇒ Object
Support the class or the module to multilingualization.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/multilang.rb', line 17 def self.register(mod, = {}, &first) type = mod.class.name.downcase raise TypeError, "can't convert #{mod.class} into #{Module}" unless mod.is_a?(Module) raise ArgumentError, "anonymous #{type} can't register" if mod.name.nil? namespace = mod.name.split('::') class_name = namespace.pop language_spec = [:as] || class_name if [:with] path = [:with] first = if block_given? f = first lambda { require path; f.call } else lambda { require path } end end raise ArgumentError, "can't permit top-level #{type}" if namespace.empty? parent = namespace.inject(Object) { |parent, name| parent.const_get(name) } slot = if parent.instance_variable_defined?(SLOT_KEY) parent.instance_variable_get(SLOT_KEY) else parent.extend(Slot::Access) parent.instance_variable_set(SLOT_KEY, Slot.new) end slot.register(language_spec, mod, &first) end |