Class: Rivendell::Import::File

Inherits:
Object
  • Object
show all
Defined in:
lib/rivendell/import/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}) ⇒ File

Returns a new instance of File.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rivendell/import/file.rb', line 6

def initialize(name, attributes = {})
  if attributes[:base_directory]
    @path = name
    @name = self.class.relative_filename(name, attributes[:base_directory])
  elsif attributes[:path]
    @name = name
    @path = attributes[:path]
  else
    @name = @path = name
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/rivendell/import/file.rb', line 4

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/rivendell/import/file.rb', line 4

def path
  @path
end

Class Method Details

.relative_filename(path, base_directory) ⇒ Object



18
19
20
# File 'lib/rivendell/import/file.rb', line 18

def self.relative_filename(path, base_directory)
  ::File.expand_path(path).gsub(%r{^#{::File.expand_path(base_directory)}/},"")
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
# File 'lib/rivendell/import/file.rb', line 44

def ==(other)
  other and path == other.path
end

#audio_propertiesObject



100
101
102
# File 'lib/rivendell/import/file.rb', line 100

def audio_properties
  file_ref.audio_properties if file_ref
end

#basenameObject



26
27
28
# File 'lib/rivendell/import/file.rb', line 26

def basename
  ::File.basename(name, ".#{extension}")
end

#closeObject



89
90
91
92
93
94
# File 'lib/rivendell/import/file.rb', line 89

def close
  if @file_ref.respond_to?(:close)
    @file_ref.close
    @file_ref = nil
  end
end

#destroy!Object



104
105
106
107
# File 'lib/rivendell/import/file.rb', line 104

def destroy!
  Rivendell::Import.logger.debug "Delete file #{path}"
  ::File.delete(path) if exists?
end

#directoriesObject



34
35
36
37
38
39
40
41
42
# File 'lib/rivendell/import/file.rb', line 34

def directories
  filename = path
  [].tap do |directories|
    while (parent = ::File.dirname(filename)) != ::File::SEPARATOR
      directories << ::File.basename(parent)
      filename = parent
    end
  end.reverse
end

#exists?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/rivendell/import/file.rb', line 74

def exists?
  ::File.exists? path
end

#extensionObject



30
31
32
# File 'lib/rivendell/import/file.rb', line 30

def extension
  ::File.extname(name).gsub(/^\./,'')
end

#file_refObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/rivendell/import/file.rb', line 78

def file_ref
  @file_ref ||= 
    if exists? and not (file_ref = TagLib::FileRef.new(path)).null?
      file_ref
    else
      :null
    end

  @file_ref unless :null == @file_ref
end

#in(directory, &block) ⇒ Object



68
69
70
71
72
# File 'lib/rivendell/import/file.rb', line 68

def in(directory, &block)
  if match %r{^#{directory}/}
    yield
  end
end

#match(expression) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/rivendell/import/file.rb', line 48

def match(expression)
  name.match(expression).tap do |result|
    verb = result ? "match" : "doesn't match"
    Rivendell::Import.logger.debug "File #{verb} '#{expression}'"
    !!result
  end
end

#modification_ageObject



64
65
66
# File 'lib/rivendell/import/file.rb', line 64

def modification_age
  Time.now - ::File.mtime(path) if exists?
end

#ready?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/rivendell/import/file.rb', line 56

def ready?
  if age = modification_age
    age > 10
  else
    false
  end
end

#tagObject



96
97
98
# File 'lib/rivendell/import/file.rb', line 96

def tag
  file_ref.tag if file_ref
end

#to_sObject



22
23
24
# File 'lib/rivendell/import/file.rb', line 22

def to_s
  name
end