Class: EasyTag::Attributes::BaseAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/easytag/attributes/base.rb

Direct Known Subclasses

MP3Attribute, MP4Attribute

Constant Summary collapse

Utilities =
EasyTag::Utilities

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ BaseAttribute

Returns a new instance of BaseAttribute.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/easytag/attributes/base.rb', line 18

def initialize(args)
  @name         = args[:name]
  @type         = args[:type]
  @default      = args[:default] || self.class.default_for_type(@type)
  @options      = args[:options] || {}
  @handler_opts = args[:handler_opts] || {}
  @aliases      = args[:aliases] || []
  @ivar         = BaseAttribute.name_to_ivar(@name)

  if args[:handler].is_a?(Symbol)
    @handler = method(args[:handler])
  elsif args[:handler].is_a?(Proc)
    @handler = args[:handler]
  end

  # fill default options
  
  # Remove nil objects from array (post process)
  @options[:compact]        ||= false
  # Delete empty objects in array (post process)
  @options[:delete_empty]   ||= false
  # normalizes key (if hash) (handler or post process)
  @options[:normalize]      ||= false
  # cast key (if hash) to symbol (handler or post process)
  @options[:to_sym]         ||= false

end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



16
17
18
# File 'lib/easytag/attributes/base.rb', line 16

def aliases
  @aliases
end

#ivarObject (readonly)

Returns the value of attribute ivar.



16
17
18
# File 'lib/easytag/attributes/base.rb', line 16

def ivar
  @ivar
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/easytag/attributes/base.rb', line 16

def name
  @name
end

Class Method Details

.can_clone?(obj) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/easytag/attributes/base.rb', line 46

def self.can_clone?(obj)
  obj.is_a?(String) || obj.is_a?(Array) || obj.is_a?(Hash)
end

.deep_copy(obj) ⇒ Object



50
51
52
# File 'lib/easytag/attributes/base.rb', line 50

def self.deep_copy(obj)
  Marshal.load(Marshal.dump(obj))
end

.default_for_type(type) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/easytag/attributes/base.rb', line 54

def self.default_for_type(type)
  case type
  when Type::STRING
    ''
  when Type::DATETIME
    nil
  when Type::INT
    0
  when Type::INT_LIST
    [0, 0] # TODO: don't assume an INT_LIST is always an int pair
  when Type::BOOLEAN
    false
  when Type::LIST
    []
  end
end

.name_to_ivar(name) ⇒ Object



121
122
123
# File 'lib/easytag/attributes/base.rb', line 121

def self.name_to_ivar(name)
  name.to_s.gsub(/\?/, '').insert(0, '@').to_sym
end

Instance Method Details

#call(iface) ⇒ Object



76
77
78
79
80
# File 'lib/easytag/attributes/base.rb', line 76

def call(iface)
  data = @handler.call(iface)
  data = type_cast(data)
  post_process(data)
end

#defaultObject



71
72
73
74
# File 'lib/easytag/attributes/base.rb', line 71

def default
  BaseAttribute.can_clone?(@default) ?
    BaseAttribute.deep_copy(@default) : @default
end

#post_process(data) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/easytag/attributes/base.rb', line 95

def post_process(data)
  if @options[:is_flag]
    data = data.to_i == 1 ? true : false
  end

  # fall back to default if data is nil
  data ||= default

  # REVIEW: the compact option may not be needed anymore
  #   since we've done away with casting empty strings to nil
  data.compact! if @options[:compact] && data.respond_to?(:compact!)

  if @options[:delete_empty]
    data.select! { |item| !item.empty? if item.respond_to?(:empty?) }
  end

  data = Utilities.normalize_object(data) if @options[:normalize]

  # TODO: roll this out to a method that supports more than just array
  if @options[:to_sym] && data.is_a?(Array)
    data.map! { |item| item.to_sym if item.respond_to?(:to_sym) }
  end

  data
end

#read_audio_property(iface) ⇒ Object



131
132
133
134
135
# File 'lib/easytag/attributes/base.rb', line 131

def read_audio_property(iface)
  key = @handler_opts[:key]
  warn "@handler_opts[:key] doesn't exist" if key.nil?
  iface.info.audio_properties.send(key)
end

#read_default(iface) ⇒ Object

read handlers



127
128
129
# File 'lib/easytag/attributes/base.rb', line 127

def read_default(iface)
  default
end

#type_cast(data) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/easytag/attributes/base.rb', line 82

def type_cast(data)
  case @type
  when Type::INT
    data = data.to_i
  when Type::DATETIME
    data = Utilities.get_datetime(data.to_s)
  when Type::LIST
    data = Array(data)
  end
  
  data
end

#user_info_lookup(iface) ⇒ Object



137
138
139
140
141
# File 'lib/easytag/attributes/base.rb', line 137

def (iface)
  key = @handler_opts[:key]
  warn "@handler_opts[:key] doesn't exist" if key.nil?
  iface.[key]
end