Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/coaster/core_ext/memory_size.rb,
lib/coaster/core_ext/object_translation.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#_translate(*args) ⇒ Object
Foo::Bar.new._translate #=> return translation ‘class.Foo.Bar.self’ Foo::Bar.new._translate(‘.title’) #=> return translation ‘class.Foo.Bar.title’ Foo::Bar.new._translate(‘title’) #=> return translation ‘title’ Foo::Bar.new._translate(:force) #=> ignore ‘message’ even if message exists.
- #_translate_params ⇒ Object
- #memory_size(depth: 2, object_ids: []) ⇒ Object
- #memory_size_total(object_ids: []) ⇒ Object
Class Method Details
._translate(*args) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/coaster/core_ext/object_translation.rb', line 5 def _translate(*args) = args.last.is_a?(Hash) ? args.pop : {} = _translate_params.merge() = .to_hash.symbolize_keys! key = args.shift subkey = nil if key.is_a?(String) if key.start_with?('.') subkey = key else return I18n.t(key, *args, **) end elsif key.is_a?(Symbol) subkey = ".#{key.to_s}" elsif key.nil? # do nothing else return I18n.t(key, *args, **) end key_class = .delete(:class) || self subkey = '.self' unless subkey key = key_class.name.gsub(/::/, '.') key = 'class.' + key + subkey unless .key?(:original_throw) [:original_throw] = .delete(:throw) end [:tkey] ||= key .merge!(throw: true) result = catch(:exception) do I18n.t(key, *args, **) end if result.is_a?(I18n::MissingTranslation) unless .key?(:original_missing) .merge!(original_missing: result) end if key_class.superclass == Object || key_class == Object return [:description] if [:description].present? case [:fallback] when Proc then return [:fallback].call(self) when Symbol then return self.send([:fallback]) when String then return [:fallback] end if Coaster.logger Coaster.logger.info([:original_missing]) Coaster.logger.debug(caller.join("\n")) end throw :exception, result if [:original_throw] missing = [:original_missing] || result msg = missing. msg = msg.dup msg.instance_variable_set(:@missing, missing) msg.instance_variable_set(:@tkey, [:tkey]) msg else [:class] = key_class.superclass _translate(subkey, *args, ) end else result = result.dup if result.frozen? result.instance_variable_set(:@translated, true) result.instance_variable_set(:@tkey, [:tkey]) result.instance_variable_set(:@missing, [:original_missing]) result end end |
._translate_params ⇒ Object
77 78 79 |
# File 'lib/coaster/core_ext/object_translation.rb', line 77 def _translate_params {} end |
Instance Method Details
#_translate(*args) ⇒ Object
Foo::Bar.new._translate #=> return translation ‘class.Foo.Bar.self’ Foo::Bar.new._translate(‘.title’) #=> return translation ‘class.Foo.Bar.title’ Foo::Bar.new._translate(‘title’) #=> return translation ‘title’ Foo::Bar.new._translate(:force) #=> ignore ‘message’ even if message exists
87 88 89 90 91 92 |
# File 'lib/coaster/core_ext/object_translation.rb', line 87 def _translate(*args) = (args.last.is_a?(Hash) ? args.pop : {}).with_indifferent_access key = args.shift || (respond_to?(:tkey) ? tkey : nil) = _translate_params.merge() self.class._translate(key, *args, ) end |
#_translate_params ⇒ Object
94 95 96 |
# File 'lib/coaster/core_ext/object_translation.rb', line 94 def _translate_params {} end |
#memory_size(depth: 2, object_ids: []) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/coaster/core_ext/memory_size.rb', line 4 def memory_size(depth: 2, object_ids: []) res = {nil => ObjectSpace.memsize_of(self)} instance_variables.each do |var| iv = instance_variable_get(var) if object_ids.include?(iv.object_id) res[var] = nil else object_ids << iv.object_id if depth > 0 res[var] = iv.memory_size(depth: depth - 1, object_ids:) else res[var] = iv.memory_size_total(object_ids:) end end end res end |
#memory_size_total(object_ids: []) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/coaster/core_ext/memory_size.rb', line 22 def memory_size_total(object_ids: []) sum = 0 memory_size(depth: 0, object_ids:).each do |k, v| case v when Hash, Array then sum += v._memory_size_total when nil then next else sum += v end end sum end |