Class: Beatport::Item
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Beatport::Item
show all
- Defined in:
- lib/beatport/item.rb
Direct Known Subclasses
Catalog::AccountType, Catalog::Artist, Catalog::AudioFormat, Catalog::AudioFormatFee, Catalog::Autocomplete, Catalog::Chart, Catalog::ChartOverview, Catalog::Country, Catalog::Currency, Catalog::Djprofile, Catalog::DynamicImage, Catalog::Feature, Catalog::Genre, Catalog::Home, Catalog::Image, Catalog::ItemType, Catalog::Key, Catalog::Keys, Catalog::Label, Catalog::List, Catalog::Mix, Catalog::Part, Catalog::Recommendations, Catalog::Release, Catalog::Slide, Catalog::Slideshow, Catalog::SourceType, Catalog::State, Catalog::Track
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(data = {}) ⇒ Item
Returns a new instance of Item.
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/beatport/item.rb', line 43
def initialize(data = {})
raise ArgumentError, "Invalid data passed to Item.new: #{data.inspect}" unless data.is_a?(Hash)
id = data.delete('id')
type = data.delete('type')
self.class.associations.each do |k, v|
associate(data, k, v[:list], v[:klass])
end
super(data)
data.each do |k, v|
next unless v
@table[k.to_sym] = Date.parse(v) if k =~ /_date$/
@table[k.to_sym] = Regexp.new(v.to_s) if k =~ /_regex$/
end
@table['id'] = id if id
@table['type'] = type if type
end
|
Class Method Details
.associations ⇒ Object
5
6
7
|
# File 'lib/beatport/item.rb', line 5
def associations
@associations ||= {}
end
|
.find_by_name(name, *args) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/beatport/item.rb', line 24
def find_by_name(name, *args)
raise "finding by name is not supported" unless respond_to?(:name_facet)
options = args.last || {}
options[:facets] ||= {}
options[:facets][name_facet] = name
results = find(options)
case results.length
when 0
nil
when 1
results.first
else
raise "find_by_name returned multiple results"
end
end
|
.has_many(var, klass) ⇒ Object
14
15
16
17
|
# File 'lib/beatport/item.rb', line 14
def has_many(var, klass)
lazy_accessor(var)
self.associations[var] = {:list => true, :klass => klass}
end
|
.has_one(var, klass) ⇒ Object
9
10
11
12
|
# File 'lib/beatport/item.rb', line 9
def has_one(var, klass)
lazy_accessor(var)
self.associations[var] = {:list => false, :klass => klass}
end
|
.lazy_accessor(var) ⇒ Object
19
20
21
22
|
# File 'lib/beatport/item.rb', line 19
def lazy_accessor(var)
return if respond_to?(var)
class_eval "def #{var}; @#{var}; end"
end
|
Instance Method Details
#[](key) ⇒ Object
Allow treating the item as a hash
75
76
77
|
# File 'lib/beatport/item.rb', line 75
def [](key)
send(key) if respond_to?(key)
end
|
#associate(data, var, collection = false, klass = Item) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/beatport/item.rb', line 79
def associate(data, var, collection = false, klass = Item)
a = data.delete(var.to_s)
return unless a
if collection && a.is_a?(Array)
a = a.compact.map { |g| klass.new(g) }
elsif !collection && a.is_a?(Hash)
a = klass.new(a)
elsif a == [] a = nil
else
raise ArgumentError, "Invalid data for association: '#{var}' = #{a.inspect}"
end
instance_variable_set(:"@#{var}", a)
end
|
#id ⇒ Object
66
67
68
|
# File 'lib/beatport/item.rb', line 66
def id
@table['id']
end
|
#type ⇒ Object
70
71
72
|
# File 'lib/beatport/item.rb', line 70
def type
@table['type']
end
|