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.



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

def initialize(filename)
  set_state(filename)
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



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

def author
  @author
end

#basenameObject (readonly)

Returns the value of attribute basename.



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

def basename
  @basename
end

#basename_cleanObject (readonly)

Returns the value of attribute basename_clean.



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

def basename_clean
  @basename_clean
end

#basename_partObject (readonly)

Returns the value of attribute basename_part.



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

def basename_part
  @basename_part
end

#date_timeObject (readonly)

Returns the value of attribute date_time.



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

def date_time
  @date_time
end

#dirnameObject

Returns the value of attribute dirname.



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

def dirname
  @dirname
end

#extnameObject (readonly)

Returns the value of attribute extname.



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

def extname
  @extname
end

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.get_date_time(date_string) ⇒ Object



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

def self.get_date_time(date_string)
  /^(?<date>\d{8})-(?<time>\d{6})/ =~ date_string
  DateTime.parse("#{Regexp.last_match(:date)}T#{Regexp.last_match(:time)}")
rescue ArgumentError
  PhFile::ZERO_DATE
end

.validate_author(author) ⇒ Object



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

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



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

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



71
72
73
# File 'lib/phtools/ph_file.rb', line 71

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

#audio?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/phtools/ph_file.rb', line 101

def audio?
  FILE_TYPE_AUDIO.include?(@type)
end

#basename_is_standard?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/phtools/ph_file.rb', line 81

def basename_is_standard?
  @basename == basename_standard
end

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



75
76
77
78
79
# File 'lib/phtools/ph_file.rb', line 75

def basename_standard(basename_clean: @basename_clean,
                      date_time: @date_time,
                      author: @author)
  %(#{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



125
126
127
128
# File 'lib/phtools/ph_file.rb', line 125

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



130
131
132
133
134
135
136
# File 'lib/phtools/ph_file.rb', line 130

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)


143
144
145
# File 'lib/phtools/ph_file.rb', line 143

def date_time_ok?
  @date_time != ZERO_DATE
end

#date_time_to_timeObject



147
148
149
150
151
152
# File 'lib/phtools/ph_file.rb', line 147

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

#image?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/phtools/ph_file.rb', line 85

def image?
  FILE_TYPE_IMAGE.include?(@type)
end

#image_normal?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/phtools/ph_file.rb', line 89

def image_normal?
  FILE_TYPE_IMAGE_NORMAL.include?(@type)
end

#image_raw?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/phtools/ph_file.rb', line 93

def image_raw?
  FILE_TYPE_IMAGE_RAW.include?(@type)
end

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



105
106
107
108
109
110
111
112
# File 'lib/phtools/ph_file.rb', line 105

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



114
115
116
117
118
119
120
121
122
123
# File 'lib/phtools/ph_file.rb', line 114

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



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

def to_s
  @filename.to_s
end

#video?Boolean

Returns:

  • (Boolean)


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

def video?
  FILE_TYPE_VIDEO.include?(@type)
end