Class: Translatable::TranslationClassGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/translatable/translation_class_generator.rb

Class Method Summary collapse

Class Method Details

.generate_for(model_class_name) ⇒ Object



3
4
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
76
77
78
79
80
81
# File 'lib/translatable/translation_class_generator.rb', line 3

def self.generate_for(model_class_name)
  class_name = "#{model_class_name}Translation"
  
  return Translatable.const_get(class_name) if Translatable.const_defined?(class_name)
  
  model_class = Object.const_get(model_class_name)
  translatable_fields = model_class.translatable_fields
  
  translation_class = Class.new do
    include Enumerable

    translatable_fields.each do |field|
      define_method(field) do
        @attributes[field]
      end
    end
    
    def initialize(attributes = {})
      @attributes = attributes.dup
      @locale = attributes[:locale]
    end
    
    def locale
      @locale
    end
    
    def [](key)
      @attributes[key.to_sym] 
    end
    
    def to_h
      @attributes.dup
    end

    def each(&block)
      return enum_for(:each) unless block_given?
      
      @attributes.each do |key, value|
        next if key == :locale
        yield(key, value)
      end
    end

    def keys
      @attributes.keys.reject { |k| k == :locale }
    end
    
    def values
      keys.map { |key| @attributes[key] }
    end
    
    def empty?
      keys.empty? || values.all?(&:blank?)
    end
    
    def size
      keys.size
    end
    
    alias_method :length, :size
    
    def inspect
      fields = @attributes.reject { |k, _| k == :locale }
                          .map { |field, value| "#{field}: #{value.inspect.to_s[0, 30]}" }
                          .append("locale: #{@locale.inspect}")
                          .join(', ')
      "#<#{self.class} #{fields}>"
    end
    
    def to_s
      inspect
    end
  end
  
  translation_class.define_singleton_method(:name) { class_name }
  
  Translatable.const_set(class_name, translation_class)
  translation_class
end