Module: Rabbit::Source::Base

Included in:
ARGF, File, Hiki, Memory, SlideShare, 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.



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

def base
  @base
end

#encodingObject

Returns the value of attribute encoding.



17
18
19
# File 'lib/rabbit/source/base.rb', line 17

def encoding
  @encoding
end

#force_modifiedObject

Returns the value of attribute force_modified.



17
18
19
# File 'lib/rabbit/source/base.rb', line 17

def force_modified
  @force_modified
end

#tmp_baseObject (readonly)

Returns the value of attribute tmp_base.



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

def tmp_base
  @tmp_base
end

Class Method Details

.append_features(klass) ⇒ Object



11
12
13
14
# File 'lib/rabbit/source/base.rb', line 11

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

Instance Method Details

#extensionObject



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

def extension
  nil
end

#full_path(path) ⇒ Object



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

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



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

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

#modified?Boolean

Returns:

  • (Boolean)


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

def modified?
  @force_modified or need_read?
end

#need_read?Boolean

Returns:

  • (Boolean)


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

def need_read?
  @source.nil?
end

#old?(current, get_latest_method_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#readObject



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

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

    if /\Autf-?8\z/i =~ enc
      @source.force_encoding(enc) if @source.respond_to?(:force_encoding)
    else
      require "iconv"
      @source = Iconv.conv("UTF-8", enc, @source)
    end
  end
  @source
end

#resetObject



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

def reset
end

#source=(new_source) ⇒ Object



27
28
29
30
# File 'lib/rabbit/source/base.rb', line 27

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

#tmp_dir_nameObject



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

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