Class: EasyTag::Attributes::BaseAttribute
- Inherits:
-
Object
- Object
- EasyTag::Attributes::BaseAttribute
show all
- Defined in:
- lib/easytag/attributes/base.rb
Constant Summary
collapse
- Utilities =
EasyTag::Utilities
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
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
@options[:compact] ||= false
@options[:delete_empty] ||= false
@options[:normalize] ||= false
@options[:to_sym] ||= false
end
|
Instance Attribute Details
#aliases ⇒ Object
Returns the value of attribute aliases.
16
17
18
|
# File 'lib/easytag/attributes/base.rb', line 16
def aliases
@aliases
end
|
#ivar ⇒ Object
Returns the value of attribute ivar.
16
17
18
|
# File 'lib/easytag/attributes/base.rb', line 16
def ivar
@ivar
end
|
#name ⇒ Object
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
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
.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
|
#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
data ||= default
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]
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
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 user_info_lookup(iface)
key = @handler_opts[:key]
warn "@handler_opts[:key] doesn't exist" if key.nil?
iface.user_info_normalized[key]
end
|