Class: PhTools::PhFile

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/phtools/ph_file.rb

Overview

phtools file name operations

Constant Summary collapse

NICKNAME_MIN_SIZE =

filename constants

3
NICKNAME_MAX_SIZE =
6
NICKNAME_SIZE =

should be in range of MIN and MAX

3
ZERO_DATE =
DateTime.new(0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ PhFile

Returns a new instance of PhFile.



55
56
57
# File 'lib/phtools/ph_file.rb', line 55

def initialize(filename)
  set_state(filename)
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



52
53
54
# File 'lib/phtools/ph_file.rb', line 52

def author
  @author
end

#basenameObject (readonly)

Returns the value of attribute basename.



52
53
54
# File 'lib/phtools/ph_file.rb', line 52

def basename
  @basename
end

#basename_cleanObject (readonly)

Returns the value of attribute basename_clean.



52
53
54
# File 'lib/phtools/ph_file.rb', line 52

def basename_clean
  @basename_clean
end

#basename_partObject (readonly)

Returns the value of attribute basename_part.



52
53
54
# File 'lib/phtools/ph_file.rb', line 52

def basename_part
  @basename_part
end

#date_timeObject (readonly)

Returns the value of attribute date_time.



52
53
54
# File 'lib/phtools/ph_file.rb', line 52

def date_time
  @date_time
end

#dirnameObject

Returns the value of attribute dirname.



52
53
54
# File 'lib/phtools/ph_file.rb', line 52

def dirname
  @dirname
end

#extnameObject (readonly)

Returns the value of attribute extname.



52
53
54
# File 'lib/phtools/ph_file.rb', line 52

def extname
  @extname
end

#filenameObject (readonly)

Returns the value of attribute filename.



52
53
54
# File 'lib/phtools/ph_file.rb', line 52

def filename
  @filename
end

Class Method Details

.validate_author(author) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/phtools/ph_file.rb', line 38

def self.validate_author(author)
  case
  when author.size != NICKNAME_SIZE
    return [false, "'#{author}' wrong author size, should be #{NICKNAME_SIZE} chars long"]
  when /[-_\s]/.match(author)
    return [false, "'#{author}' author should not contain spaces [_- ]"]
  when /[\d]/.match(author)
    return [false, "'#{author}' author should not contain digits"]
  when /[\W]/.match(author)
    return [false, "'#{author}' author should contain only ASCII chars"]
  end
  [true, '']
end

.validate_file!(filename, file_type) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/phtools/ph_file.rb', line 28

def self.validate_file!(filename, file_type)
  fail PhTools::Error, 'does not exist'  unless
    filename && File.exist?(filename)
  fail PhTools::Error, 'not a file' if File.directory?(filename)
  fail PhTools::Error, 'no permission to write' unless
    File.writable_real?(filename)
  fail PhTools::Error, 'unsupported type' unless
    file_type.include?(File.extname(filename).slice(1..-1).downcase)
end

Instance Method Details

#<=>(other) ⇒ Object



63
64
65
# File 'lib/phtools/ph_file.rb', line 63

def <=>(other)
  @filename <=> other.filename
end

#basename_is_standard?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/phtools/ph_file.rb', line 73

def basename_is_standard?
  @basename == basename_standard
end

#basename_standard(basename_clean: @basename_clean, date_time: @date_time, author: @author) ⇒ Object



67
68
69
70
71
# File 'lib/phtools/ph_file.rb', line 67

def basename_standard(basename_clean: @basename_clean,
                      date_time: @date_time,
                      author: @author)
  %Q{#{date_time.strftime('%Y%m%d-%H%M%S')}_#{(author.upcase + "XXXXXX")[0, NICKNAME_SIZE]} #{basename_clean}}
end

#cleanse(dirname: @dirname, basename_clean: @basename_clean, extname: @extname) ⇒ Object



97
98
99
100
# File 'lib/phtools/ph_file.rb', line 97

def cleanse(dirname: @dirname, basename_clean: @basename_clean,
            extname: @extname)
  File.join(dirname, basename_clean + extname)
end

#cleanse!(dirname: @dirname, basename_clean: @basename_clean, extname: @extname) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/phtools/ph_file.rb', line 102

def cleanse!(dirname: @dirname, basename_clean: @basename_clean,
           extname: @extname)
  filename = cleanse(dirname: dirname, basename_clean: basename_clean,
                     extname: extname)
  set_state(filename)
  filename
end

#date_time_ok?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/phtools/ph_file.rb', line 115

def date_time_ok?
  @date_time != ZERO_DATE
end

#date_time_to_timeObject



119
120
121
122
123
124
# File 'lib/phtools/ph_file.rb', line 119

def date_time_to_time
  Time.new(@date_time.year, @date_time.month, @date_time.day,
           @date_time.hour, @date_time.min, @date_time.sec)
           # no use of @date_time.zone - assuming file's timezone always
           # equals to photografer's computer timezone
end

#standardize(dirname: @dirname, basename_clean: @basename_clean, extname: @extname, date_time: @date_time, author: @author) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/phtools/ph_file.rb', line 77

def standardize(dirname: @dirname, basename_clean: @basename_clean,
                extname: @extname, date_time: @date_time,
                author: @author)
  File.join(dirname,
            basename_standard(basename_clean: basename_clean,
                              date_time: date_time,
                              author: author) + extname)
end

#standardize!(dirname: @dirname, basename_clean: @basename_clean, extname: @extname, date_time: @date_time, author: @author) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/phtools/ph_file.rb', line 86

def standardize!(dirname: @dirname, basename_clean: @basename_clean,
                 extname: @extname, date_time: @date_time,
                 author: @author)

  filename = standardize(dirname: dirname, basename_clean: basename_clean,
                         extname: extname, date_time: date_time,
                         author: author)
  set_state(filename)
  filename
end

#to_sObject



59
60
61
# File 'lib/phtools/ph_file.rb', line 59

def to_s
  "#{@filename}"
end