class ExifTagger::Tag::Tag

Parent class for all tags

Constants

EMPTY
EMPTY_DATE
EXIFTOOL_TAGS
MAX_BYTESIZE

Attributes

errors[R]
force_write[RW]
info[RW]
previous[R]
raw_values[R]
value[R]
value_invalid[R]
warnings[R]
write_script_lines[R]

Public Class Methods

empty?(value) click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 24
def self.empty?(value)
  return true if value.nil?
  return value.empty? if value.is_a?(String)
  return value.join.empty? if value.is_a?(Array)
  return value.values.join.empty? if value.is_a?(Hash)
  false
end
new(value = '', previous = nil) click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 32
def initialize(value = '', previous = nil)
  @raw_values = {}
  if value.class == MiniExiftool
    init_raw_values(value)
    @value = normalize(get_from_raw)
  else
    @value = normalize(value)
  end
  @previous = (previous.is_a?(self.class) ? previous : nil)
  @info = ''
  @force_write = false

  validate
  validate_vs_previous
  freeze_values
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 57
def <=>(other)
  return tag_id <=> other.tag_id if other.respond_to?(:tag_id)
  tag_id <=> other.to_s.to_sym
end
check_for_warnings(original_values: {}) click to toggle source

TODO: remove deprecated method

# File lib/phtools/exif_tagger/tags/_tag.rb, line 72
def check_for_warnings(original_values: {})
  @warnings = []
  self.class::EXIFTOOL_TAGS.each do |tag|
    v = original_values[tag]
    unless v.nil?
      case
      when v.kind_of?(String)
        @warnings << "#{tag_name} has original value: #{tag}='#{v}'" unless v.empty?
      when v.kind_of?(Array)
        @warnings << "#{tag_name} has original value: #{tag}=#{v}" unless v.join.empty?
      else
        @warnings << "#{tag_name} has original value: #{tag}=#{v}"
      end
    end
  end
  @warnings.freeze
end
tag_id() click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 49
def tag_id
  self.class.to_s.demodulize.underscore.to_sym
end
tag_name() click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 53
def tag_name
  self.class.to_s.demodulize
end
to_s() click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 62
def to_s
  return "#{tag_id} is EMPTY" if Tag.empty?(@value)
  "#{tag_id} = #{@value}"
end
to_write_script() click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 90
def to_write_script
  str = +''
  @write_script_lines = []
  generate_write_script_lines unless Tag.empty?(@value)
  unless @write_script_lines.empty?
    str << (info.empty? ? '' : "# INFO: #{@info}\n")
    @warnings.each do |w|
      str << "# WARNING: #{w}\n"
    end
    @write_script_lines.each do |l|
      unless @warnings.empty?
        str << '# ' unless @force_write
      end
      str << "#{l}\n"
    end
  end
  str
end
valid?() click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 67
def valid?
  @errors.empty?
end

Private Instance Methods

freeze_values() click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 157
def freeze_values
  @value.freeze
  @raw_values.freeze
  @errors.freeze
  @value_invalid.freeze
  @warnings.freeze
end
init_raw_values(raw) click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 128
def init_raw_values(raw)
  self.class::EXIFTOOL_TAGS.each do |tag|
    @raw_values[tag] = normalize(raw[tag])
  end
end
normalize(value) click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 111
def normalize(value)
  return EMPTY if value.nil?
  return EMPTY if value.is_a?(TrueClass) || value.is_a?(FalseClass)
  if value.is_a?(String)
    return EMPTY if value.strip.empty?
  elsif value.is_a?(Array)
    return value.flatten.map { |i| normalize(i.to_s) }
  elsif value.is_a?(Hash)
    return value.map { |k, v| [k, normalize(v.to_s)] }.to_h
  elsif value.is_a?(DateTime)
    return EMPTY if value.strftime('%F %T') == EMPTY_DATE
  elsif value.is_a?(Time)
    return normalize(value.to_datetime)
  end
  value
end
validate() click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 134
def validate
  @errors = []
  @value_invalid = []
  return if Tag.empty?(@value)
  validate_type
end
validate_string_size(value) click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 141
def validate_string_size(value)
  bsize = value.bytesize
  return true if bsize <= self.class::MAX_BYTESIZE
  @errors << %(#{tag_name}: '#{value}' is #{bsize - self.class::MAX_BYTESIZE} bytes longer than allowed #{self.class::MAX_BYTESIZE})
  false
end
validate_vs_previous() click to toggle source
# File lib/phtools/exif_tagger/tags/_tag.rb, line 148
def validate_vs_previous
  @warnings = []
  return if @previous.nil?
  @previous.raw_values.each do |tag, val|
    @warnings << "#{tag_name} has original value: #{tag}='#{val}'" unless Tag.empty?(val)
  end
  @warnings.freeze
end