Module: Rabbit::Source::Base

Included in:
ARGF, File, Memory, URI
Defined in:
lib/rabbit/source/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#baseObject

Returns the value of attribute base.


14
15
16
# File 'lib/rabbit/source/base.rb', line 14

def base
  @base
end

#encodingObject

Returns the value of attribute encoding.


15
16
17
# File 'lib/rabbit/source/base.rb', line 15

def encoding
  @encoding
end

#force_modifiedObject

Returns the value of attribute force_modified.


15
16
17
# File 'lib/rabbit/source/base.rb', line 15

def force_modified
  @force_modified
end

#tmp_baseObject (readonly)

Returns the value of attribute tmp_base.


14
15
16
# File 'lib/rabbit/source/base.rb', line 14

def tmp_base
  @tmp_base
end

Class Method Details

.append_features(klass) ⇒ Object


9
10
11
12
# File 'lib/rabbit/source/base.rb', line 9

def self.append_features(klass)
  super
  klass.send(:include, GetText)
end

Instance Method Details

#extensionObject


98
99
100
# File 'lib/rabbit/source/base.rb', line 98

def extension
  nil
end

#full_path(path) ⇒ Object


63
64
65
66
67
68
69
70
71
# File 'lib/rabbit/source/base.rb', line 63

def full_path(path)
  if @base_uri.nil? or @base_uri.relative?
    ::File.join(@base, path)
  else
    uri = @base_uri.dup
    uri.path = @base_uri.path + "/" unless /\/$/ =~ @base_uri.path
    (uri + path).to_s
  end
end

#initialize(encoding, logger) ⇒ Object


17
18
19
20
21
22
23
# File 'lib/rabbit/source/base.rb', line 17

def initialize(encoding, logger)
  @encoding = encoding
  @logger = logger
  @source = nil
  @force_modified = false
  init_base
end

#modified?Boolean

Returns:

  • (Boolean)

55
56
57
# File 'lib/rabbit/source/base.rb', line 55

def modified?
  @force_modified or need_read?
end

#need_read?Boolean

Returns:

  • (Boolean)

59
60
61
# File 'lib/rabbit/source/base.rb', line 59

def need_read?
  @source.nil?
end

#old?(current, get_latest_method_name) ⇒ Boolean

Returns:

  • (Boolean)

79
80
81
82
# File 'lib/rabbit/source/base.rb', line 79

def old?(current, get_latest_method_name)
  current.nil? or
    (current and __send__(get_latest_method_name) > current)
end

#open_full_path(path, mode = "rb") ⇒ Object


73
74
75
76
77
# File 'lib/rabbit/source/base.rb', line 73

def open_full_path(path, mode="rb")
  open(full_path(path), mode) do |f|
    yield f
  end
end

#readObject


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rabbit/source/base.rb', line 33

def read
  if need_read?
    @source = _read
    case @encoding
    when nil
      enc = guess_encoding(@source)
    when Encoding
      enc = @encoding
    else
      enc = Encoding.find(@encoding)
    end

    case enc
    when Encoding::UTF_8, Encoding::ASCII_8BIT
      @source.force_encoding(enc)
    else
      @source = @source.encode(Encoding::UTF_8, enc)
    end
  end
  @source
end

#resetObject


30
31
# File 'lib/rabbit/source/base.rb', line 30

def reset
end

#source=(new_source) ⇒ Object


25
26
27
28
# File 'lib/rabbit/source/base.rb', line 25

def source=(new_source)
  source_type = self.class.name.split("::").last.downcase
  raise ImmutableSourceTypeError.new(source_type)
end

#tmp_dir_nameObject


84
85
86
87
88
# File 'lib/rabbit/source/base.rb', line 84

def tmp_dir_name
  dir = ::File.join(@tmp_base, TMP_DIR_NAME)
  FileUtils.mkdir_p(dir) unless ::File.exist?(dir)
  dir
end