~ubuntu-branches/ubuntu/vivid/rabbit/vivid

« back to all changes in this revision

Viewing changes to lib/rabbit/source/base.rb

  • Committer: Bazaar Package Importer
  • Author(s): Youhei SASAKI
  • Date: 2009-07-22 22:15:37 UTC
  • Revision ID: james.westby@ubuntu.com-20090722221537-iy7foj73p2kyuumi
Tags: upstream-0.6.1
ImportĀ upstreamĀ versionĀ 0.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'fileutils'
 
2
 
 
3
require 'rabbit/rabbit'
 
4
 
 
5
module Rabbit
 
6
  module Source
 
7
 
 
8
    module Base
 
9
 
 
10
      def self.append_features(klass)
 
11
        super
 
12
        klass.extend(GetText)
 
13
      end
 
14
      
 
15
      attr_reader :base, :tmp_base
 
16
      attr_accessor :encoding, :force_modified
 
17
 
 
18
      def initialize(encoding, logger)
 
19
        @encoding = encoding
 
20
        @logger = logger
 
21
        @source = nil
 
22
        @force_modified = false
 
23
        init_base
 
24
      end
 
25
 
 
26
      def source=(new_source)
 
27
        source_type = self.class.name.split("::").last.downcase
 
28
        raise ImmutableSourceTypeError.new(source_type)
 
29
      end
 
30
 
 
31
      def reset
 
32
      end
 
33
      
 
34
      def read
 
35
        if need_read?
 
36
          @source = _read
 
37
          if @encoding.nil?
 
38
            enc = guess_encoding(@source)
 
39
          else
 
40
            enc = @encoding
 
41
          end
 
42
 
 
43
          unless /\Autf-?8\z/i =~ enc
 
44
            require "iconv"
 
45
            @source = Iconv.conv("UTF-8", enc, @source)
 
46
          end
 
47
        end
 
48
        @source
 
49
      end
 
50
      
 
51
      def modified?
 
52
        @force_modified or need_read?
 
53
      end
 
54
      
 
55
      def need_read?
 
56
        @source.nil?
 
57
      end
 
58
      
 
59
      def full_path(path)
 
60
        if @base_uri.nil? or @base_uri.relative?
 
61
          ::File.join(@base, path)
 
62
        else
 
63
          uri = @base_uri.dup
 
64
          uri.path = @base_uri.path + "/" unless /\/$/ =~ @base_uri.path
 
65
          (uri + path).to_s
 
66
        end
 
67
      end
 
68
      
 
69
      def open_full_path(path, mode="rb")
 
70
        open(full_path(path), mode) do |f|
 
71
          yield f
 
72
        end
 
73
      end
 
74
      
 
75
      def old?(current, get_latest_method_name)
 
76
        current.nil? or
 
77
          (current and __send__(get_latest_method_name) > current)
 
78
      end
 
79
      
 
80
      def tmp_dir_name
 
81
        dir = ::File.join(@tmp_base, TMP_DIR_NAME)  
 
82
        FileUtils.mkdir_p(dir) unless ::File.exist?(dir)
 
83
        dir
 
84
      end
 
85
      
 
86
      def base=(new_value)
 
87
        if new_value.nil?
 
88
          init_base
 
89
        else
 
90
          set_base(new_value)
 
91
        end
 
92
      end
 
93
      
 
94
      private
 
95
      def init_base
 
96
        set_base(".")
 
97
      end
 
98
      
 
99
      def set_base(new_value)
 
100
        if ::File::ALT_SEPARATOR
 
101
          new_value = new_value.gsub(::File::ALT_SEPARATOR, ::File::SEPARATOR)
 
102
        end
 
103
        @base = new_value
 
104
        @base_uri = parse_uri(@base)
 
105
        if @base_uri.nil? or @base_uri.scheme.nil?
 
106
          @tmp_base = @base
 
107
        else
 
108
          @tmp_base = "."
 
109
        end
 
110
      end
 
111
 
 
112
      def parse_uri(str)
 
113
        begin
 
114
          ::URI.parse(str)
 
115
        rescue ::URI::InvalidURIError
 
116
          nil
 
117
        end
 
118
      end
 
119
 
 
120
      def guess_encoding(str)
 
121
        require 'nkf'
 
122
        case NKF.guess(str)
 
123
        when NKF::JIS
 
124
          "ISO-2022-JP"
 
125
        when NKF::EUC
 
126
          "eucJP"
 
127
        when NKF::SJIS
 
128
          "CP932"
 
129
        when NKF::UTF16
 
130
          "UTF-16"
 
131
        when NKF::UTF32
 
132
          "UTF-32"
 
133
        else
 
134
          "UTF-8"
 
135
        end
 
136
      end
 
137
    end
 
138
 
 
139
    module LimitAccessInterval
 
140
      MINIMUM_ACCESS_TIME = 60
 
141
 
 
142
      def initialize(*args, &block)
 
143
        update_last_access_time
 
144
        super
 
145
      end
 
146
 
 
147
      def old?(current, get_latest_method_name)
 
148
        result = (can_access? and super)
 
149
        update_last_access_time if result
 
150
        result
 
151
      end
 
152
      
 
153
      private
 
154
      def update_last_access_time
 
155
        @last_access_time = Time.now
 
156
      end
 
157
 
 
158
      def can_access?
 
159
        Time.now - @last_access_time > MINIMUM_ACCESS_TIME
 
160
      end
 
161
    end
 
162
    
 
163
  end
 
164
end