Class: ExifTagger::Tag::Tag

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/phtools/exif_tagger/tags/_tag.rb

Overview

Parent class for all tags

Constant Summary collapse

EMPTY =
''
EMPTY_DATE =
'0000-01-01 00:00:00'
EXIFTOOL_TAGS =
[].freeze
MAX_BYTESIZE =
32

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = '', previous = nil) ⇒ Tag

Returns a new instance of Tag.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 33

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



21
22
23
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 21

def errors
  @errors
end

#force_writeObject

Returns the value of attribute force_write.



23
24
25
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 23

def force_write
  @force_write
end

#infoObject

Returns the value of attribute info.



23
24
25
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 23

def info
  @info
end

#previousObject (readonly)

Returns the value of attribute previous.



22
23
24
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 22

def previous
  @previous
end

#raw_valuesObject (readonly)

Returns the value of attribute raw_values.



21
22
23
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 21

def raw_values
  @raw_values
end

#valueObject (readonly)

Returns the value of attribute value.



21
22
23
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 21

def value
  @value
end

#value_invalidObject (readonly)

Returns the value of attribute value_invalid.



21
22
23
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 21

def value_invalid
  @value_invalid
end

#warningsObject (readonly)

Returns the value of attribute warnings.



21
22
23
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 21

def warnings
  @warnings
end

#write_script_linesObject (readonly)

Returns the value of attribute write_script_lines.



21
22
23
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 21

def write_script_lines
  @write_script_lines
end

Class Method Details

.empty?(value) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 25

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

Instance Method Details

#<=>(other) ⇒ Object



58
59
60
61
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 58

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: {}) ⇒ Object

TODO: remove deprecated method



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 73

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_idObject



50
51
52
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 50

def tag_id
  self.class.to_s.demodulize.underscore.to_sym
end

#tag_nameObject



54
55
56
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 54

def tag_name
  self.class.to_s.demodulize
end

#to_sObject



63
64
65
66
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 63

def to_s
  return "#{tag_id} is EMPTY" if Tag.empty?(@value)
  "#{tag_id} = #{@value}"
end

#to_write_scriptObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 91

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?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/phtools/exif_tagger/tags/_tag.rb', line 68

def valid?
  @errors.empty?
end