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.


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

def base
  @base
end

#encodingObject

Returns the value of attribute encoding.


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

def encoding
  @encoding
end

#force_modifiedObject

Returns the value of attribute force_modified.


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

def force_modified
  @force_modified
end

#tmp_baseObject (readonly)

Returns the value of attribute tmp_base.


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

def tmp_base
  @tmp_base
end

Class Method Details

.append_features(klass) ⇒ Object


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

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

Instance Method Details

#extensionObject


114
115
116
# File 'lib/rabbit/source/base.rb', line 114

def extension
  nil
end

#full_path(path) ⇒ Object


79
80
81
82
83
84
85
86
87
# File 'lib/rabbit/source/base.rb', line 79

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


33
34
35
36
37
38
39
# File 'lib/rabbit/source/base.rb', line 33

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

#modified?Boolean

Returns:

  • (Boolean)

71
72
73
# File 'lib/rabbit/source/base.rb', line 71

def modified?
  @force_modified or need_read?
end

#need_read?Boolean

Returns:

  • (Boolean)

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

def need_read?
  @source.nil?
end

#old?(current, get_latest_method_name) ⇒ Boolean

Returns:

  • (Boolean)

95
96
97
98
# File 'lib/rabbit/source/base.rb', line 95

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


89
90
91
92
93
# File 'lib/rabbit/source/base.rb', line 89

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

#readObject


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rabbit/source/base.rb', line 49

def read
  if need_read?
    @source = _read
    case @encoding
    when nil
      enc = guess_encoding(@source) || Encoding::ASCII_8BIT
    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


46
47
# File 'lib/rabbit/source/base.rb', line 46

def reset
end

#source=(new_source) ⇒ Object


41
42
43
44
# File 'lib/rabbit/source/base.rb', line 41

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

#tmp_dir_nameObject


100
101
102
103
104
# File 'lib/rabbit/source/base.rb', line 100

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