Class: PhTools::PhFile
- Inherits:
-
Object
- Object
- PhTools::PhFile
- 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
-
#author ⇒ Object
readonly
Returns the value of attribute author.
-
#basename ⇒ Object
readonly
Returns the value of attribute basename.
-
#basename_clean ⇒ Object
readonly
Returns the value of attribute basename_clean.
-
#basename_part ⇒ Object
readonly
Returns the value of attribute basename_part.
-
#date_time ⇒ Object
readonly
Returns the value of attribute date_time.
-
#dirname ⇒ Object
Returns the value of attribute dirname.
-
#extname ⇒ Object
readonly
Returns the value of attribute extname.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .get_date_time(date_string) ⇒ Object
- .validate_author(author) ⇒ Object
- .validate_file!(filename, file_type) ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #audio? ⇒ Boolean
- #basename_is_standard? ⇒ Boolean
- #basename_standard(basename_clean: @basename_clean, date_time: @date_time, author: @author) ⇒ Object
- #cleanse(dirname: @dirname, basename_clean: @basename_clean, extname: @extname) ⇒ Object
- #cleanse!(dirname: @dirname, basename_clean: @basename_clean, extname: @extname) ⇒ Object
- #date_time_ok? ⇒ Boolean
- #date_time_to_time ⇒ Object
- #image? ⇒ Boolean
- #image_normal? ⇒ Boolean
- #image_raw? ⇒ Boolean
-
#initialize(filename) ⇒ PhFile
constructor
A new instance of PhFile.
- #standardize(dirname: @dirname, basename_clean: @basename_clean, extname: @extname, date_time: @date_time, author: @author) ⇒ Object
- #standardize!(dirname: @dirname, basename_clean: @basename_clean, extname: @extname, date_time: @date_time, author: @author) ⇒ Object
- #to_s ⇒ Object
- #video? ⇒ Boolean
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
#author ⇒ Object (readonly)
Returns the value of attribute author.
60 61 62 |
# File 'lib/phtools/ph_file.rb', line 60 def end |
#basename ⇒ Object (readonly)
Returns the value of attribute basename.
60 61 62 |
# File 'lib/phtools/ph_file.rb', line 60 def basename @basename end |
#basename_clean ⇒ Object (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_part ⇒ Object (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_time ⇒ Object (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 |
#dirname ⇒ Object
Returns the value of attribute dirname.
60 61 62 |
# File 'lib/phtools/ph_file.rb', line 60 def dirname @dirname end |
#extname ⇒ Object (readonly)
Returns the value of attribute extname.
60 61 62 |
# File 'lib/phtools/ph_file.rb', line 60 def extname @extname end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
60 61 62 |
# File 'lib/phtools/ph_file.rb', line 60 def filename @filename end |
#type ⇒ Object (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.() case when .size != NICKNAME_SIZE return [false, "'#{author}' wrong author size, should be #{NICKNAME_SIZE} chars long"] when /[-_\s]/.match() return [false, "'#{author}' author should not contain spaces [_- ]"] when /[\d]/.match() return [false, "'#{author}' author should not contain digits"] when /[\W]/.match() 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
101 102 103 |
# File 'lib/phtools/ph_file.rb', line 101 def audio? FILE_TYPE_AUDIO.include?(@type) end |
#basename_is_standard? ⇒ 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: ) %(#{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
143 144 145 |
# File 'lib/phtools/ph_file.rb', line 143 def date_time_ok? @date_time != ZERO_DATE end |
#date_time_to_time ⇒ Object
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
85 86 87 |
# File 'lib/phtools/ph_file.rb', line 85 def image? FILE_TYPE_IMAGE.include?(@type) end |
#image_normal? ⇒ 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
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: ) File.join(dirname, basename_standard(basename_clean: basename_clean, date_time: date_time, 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: ) filename = standardize(dirname: dirname, basename_clean: basename_clean, extname: extname, date_time: date_time, author: ) set_state(filename) filename end |
#to_s ⇒ Object
67 68 69 |
# File 'lib/phtools/ph_file.rb', line 67 def to_s @filename.to_s end |
#video? ⇒ Boolean
97 98 99 |
# File 'lib/phtools/ph_file.rb', line 97 def video? FILE_TYPE_VIDEO.include?(@type) end |