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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ BaseAttribute

Returns a new instance of BaseAttribute.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/easytag/attributes/base.rb', line 15

def initialize(args)
  @name = args[:name]
  @default = args[:default]
  @type = args[:type] || Type::STRING
  @options = args[:options] || {}
  @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
  # normalizes key (if hash) (handler)
  @options[:normalize]  ||= false
  # cast key (if hash) to symbol (handler)
  @options[:to_sym]     ||= false

end

Class Method Details

.can_clone?(obj) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/easytag/attributes/base.rb', line 39

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

.deep_copy(obj) ⇒ Object



43
44
45
# File 'lib/easytag/attributes/base.rb', line 43

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

.name_to_ivar(name) ⇒ Object



96
97
98
99
100
101
# File 'lib/easytag/attributes/base.rb', line 96

def self.name_to_ivar(name)
  name = name.to_s if name.class == Symbol
  name.gsub!(/\?/, '')
  name.insert(0, '@')
  name.to_sym
end

.obj_or_nil(o) ⇒ Object

avoid returing empty objects



88
89
90
91
92
93
94
# File 'lib/easytag/attributes/base.rb', line 88

def self.obj_or_nil(o)
  if o.class == String
    ret = o.empty? ? nil : o
  else
    o
  end
end

Instance Method Details

#call(iface) ⇒ Object



52
53
54
55
56
57
# File 'lib/easytag/attributes/base.rb', line 52

def call(iface)
  #puts 'entered call()'
  data = @handler.call(iface)
  data = type_cast(data)
  post_process(data)
end

#defaultObject



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

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

#post_process(data) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/easytag/attributes/base.rb', line 70

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 = BaseAttribute.obj_or_nil(data) || default

  # run obj_or_nil on each item in array
  data.map! { |item| BaseAttribute.obj_or_nil(item) } if data.is_a?(Array)

  if @options[:compact] && data.respond_to?(:compact!)
    data.compact!
  end

  data
end

#type_cast(data) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/easytag/attributes/base.rb', line 59

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