Class: MultiExiftool::Values

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_exiftool/values.rb

Overview

Representing (tag, value) pairs of metadata. Access via bracket-methods or dynamic method-interpreting via method_missing.

Constant Summary collapse

REGEXP_TIMESTAMP =

Regular expression to determine timestamp values

/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d)(?::((?:\d\d)(?:\.\d+)?))?((?:[-+]\d\d:\d\d)|(?:Z))?(?: *DST)?$/
REGEXP_RATIONAL =

Regular expression to determine rational values

%r(^(\d+)/(\d+)$)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Values

Returns a new instance of Values.



22
23
24
25
26
27
28
29
30
# File 'lib/multi_exiftool/values.rb', line 22

def initialize values
  @values = {}
  values.map do |tag,val|
    unified_tag = Values.unify_tag(tag)
    Values.tag_map[unified_tag] = tag
    val = val.kind_of?(Hash) ? Values.new(val) : val
    @values[unified_tag] = val
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(tag, *args, &block) ⇒ Object (private)



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/multi_exiftool/values.rb', line 107

def method_missing tag, *args, &block
  res = self[Values.unify_tag(tag.to_s)]
  if res && block_given?
    if block.arity > 0
      yield res
    else
      res.instance_eval &block
    end
  end
  res
end

Class Attribute Details

.tag_mapObject (readonly)

Returns the value of attribute tag_map.



99
100
101
# File 'lib/multi_exiftool/values.rb', line 99

def tag_map
  @tag_map
end

Class Method Details

.unify_tag(tag) ⇒ Object



101
102
103
# File 'lib/multi_exiftool/values.rb', line 101

def unify_tag tag
tag.gsub(/[-_]/, '').downcase
end

Instance Method Details

#[](tag) ⇒ Object

Gets the (posible converted) value for a tag (tag will be unified, i.e. FNumber, fnumber or f_number can be used for FNumber)



35
36
37
38
# File 'lib/multi_exiftool/values.rb', line 35

def [](tag)
  unified_tag = Values.unify_tag(tag)
  convert(unified_tag, @values[unified_tag])
end

#convert(tag, val) ⇒ maybe

Converts values on the basis of unified tag name and value. It is called each time a value is fethed from a Values instance.

Returns:

  • (maybe)

    converted value



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/multi_exiftool/values.rb', line 43

def convert tag, val
  return val unless val.kind_of?(String)
  case tag
  when 'partofset', 'track'
    return val
  end
  case val
  when REGEXP_TIMESTAMP
    year, month, day, hour, minute = $~.captures[0,5].map {|cap| cap.to_i}
    if month == 0 || day == 0
      return nil
    end
    second = $6.to_f
    zone = $7
    zone = '+00:00' if zone == 'Z'
    begin
      Time.new(year, month, day, hour, minute, second, zone)
    rescue ArgumentError
      val
    end
  when REGEXP_RATIONAL
    return val if $2.to_i == 0
    Rational($1, $2)
  else
    val
  end
end

#has_tag?(tag) ⇒ Boolean

Checks if a tag is present

Parameters:

  • Tag

    as string or symbol (will be unified)

Returns:

  • (Boolean)


73
74
75
# File 'lib/multi_exiftool/values.rb', line 73

def has_tag? tag
  @values.has_key?(Values.unify_tag(tag.to_s))
end

#tagsObject

Gets the original tag names of this instance



78
79
80
# File 'lib/multi_exiftool/values.rb', line 78

def tags
  @values.keys.map {|tag| Values.tag_map[tag]}
end

#to_hObject Also known as: to_hash

Generates a hash representation of this instance with original tag names es keys and converted values as values



85
86
87
88
89
90
91
# File 'lib/multi_exiftool/values.rb', line 85

def to_h
  @values.inject(Hash.new) do |h, a|
    tag, val = a
    h[Values.tag_map[tag]] = convert(Values.unify_tag(tag), val)
    h
  end
end