File: activerecord.rb

package info (click to toggle)
libgettext-ruby 1.7.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,892 kB
  • ctags: 1,198
  • sloc: ruby: 6,738; ansic: 67; makefile: 38; sql: 14; sh: 6
file content (155 lines) | stat: -rw-r--r-- 4,395 bytes parent folder | download
1
2
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/ruby
=begin
  parser/activerecord.rb - parser for ActiveRecord

  Copyright (C) 2005, 2006  Masao Mutoh
 
  You may redistribute it and/or modify it under the same
  license terms as Ruby.

  $Id: activerecord.rb,v 1.11 2006/07/13 18:22:09 mutoh Exp $
=end

require 'gettext'
require 'gettext/parser/ruby'

include GetText

module GetText
  module ActiveRecordParser
    extend GetText
    include GetText
    GetText.bindtextdomain("rgettext")

    @config = {
      :db_yml => "config/database.yml",
      :db_mode => "development",
      :activerecord_classes => ["ActiveRecord::Base"],
      :untranslate_columns => ["id"],
      :use_classname => true,
    }

    @ar_re = nil

    module_function
    def require_rails(file) # :nodoc:
      begin
	require file
      rescue MissingSourceFile
	$stderr.puts _("'%{file}' is not found.") % {:file => file}
      end
    end

    # Sets some preferences to parse ActiveRecord files.
    #
    # * config: a Hash of the config. It can takes some values below:
    #   * :use_classname - If true, the msgids of ActiveRecord become "ClassName|FieldName" (e.g. "Article|Title"). Otherwise the ClassName is not used (e.g. "Title"). Default is true.
    #   * :db_yml - the path of database.yml. Default is "config/database.yml".
    #   * :db_mode - the mode of the database. Default is "development"
    #   * :activerecord_classes - an Array of the superclass of the models. The classes should be String value. Default is ["ActiveRecord::Base"]
    #   * :untranslate_columns - an Array of the column names which is ignored as the msgid.
    #
    # "ClassName|FieldName" uses GetText.sgettext. So you don't need to translate the left-side of "|". 
    # See <Documents for Translators for more details(http://www.yotabanana.com/hiki/ruby-gettext-translate.html)>.
    def init(config)
      if config
	config.each{|k, v|
	  @config[k] = v
	}
      end
      @ar_re = /class.*(#{@config[:activerecord_classes].join("|")})/
    end

    def untranslate_column?(klass, columnname)
      klass.untranslate?(columnname) || @config[:untranslate_columns].include?(columnname)
    end

    def parse(file, targets = []) # :nodoc:
      old_constants = constants
      begin
        eval(open(file).read)
      rescue
        $stderr.puts _("Ignored '%{file}'. Solve dependencies first.") % {:file => file}
        $stderr.puts $! 
      end
      loaded_constants = constants - old_constants
      loaded_constants.each do |classname|
	klass = eval(classname)
	if klass < ActiveRecord::Base
	  unless klass.untranslate_all?
	    add_target(targets, file, ::Inflector.singularize(klass.table_name.gsub(/_/, " ")))
	    tablename = klass.class_name
	    begin
	      klass.columns.each do |column|
		unless untranslate_column?(klass, column.name)
		  if @config[:use_classname]
		    msgid = tablename + "|" +  klass.human_attribute_name(column.name)
		  else
		    msgid = klass.human_attribute_name(column.name)
		  end
		  add_target(targets, file, msgid)
		end
	      end
	    rescue
	      $stderr.puts _("No database is available.")
	      $stderr.puts $!
	    end
	  end
	end
      end
      if RubyParser.target?(file)
	targets = RubyParser.parse(file, targets)
      end
      targets.uniq!
      targets
    end

    def add_target(targets, file, msgid) # :nodoc:
      key_existed = targets.assoc(msgid)
      if key_existed 
	targets[targets.index(key_existed)] = key_existed << "#{file}:-"
      else
	targets << [msgid, "#{file}:-"]
      end
      targets
    end

    def target?(file) # :nodoc:
      init(nil) unless @ar_re
      data = IO.readlines(file)
      data.each do |v|
	if @ar_re =~ v
	  unless @db
	    begin
	      require 'rubygems'
	    rescue LoadError
	      $stderr.puts _("rubygems are not found.") if $DEBUG
	    end
	    require 'active_record'
	    begin
	      yml = YAML.load(ERB.new(IO.read(@config[:db_yml])).result)
	    rescue
	      return false
	    end
	    ENV["RAILS_ENV"] = @config[:db_mode]
	    require_rails 'config/boot.rb'
	    require_rails 'config/environment.rb'
	    require_rails 'app/controllers/application.rb'
	  end
	  return true
	end
      end
      false
    end
  end
end

if __FILE__ == $0
  # ex) ruby model1.rb model2.rb 
  ARGV.each do |file|
    if GetText::ActiveRecordParser.target?(file)
      p file
      p GetText::ActiveRecordParser.parse(file)
    end
  end
end