Class: Beatport::Item

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/beatport/item.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Item

Returns a new instance of Item.

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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)

  # OpenStruct doesn't like id or type, so we store them after the call to super
  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|
    @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

.associationsObject



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



74
75
76
# File 'lib/beatport/item.rb', line 74

def [](key)
  send(key) if respond_to?(key)
end

#associate(data, var, collection = false, klass = Item) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/beatport/item.rb', line 78

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
    # In some cases, when there's no data returned for an association it'll be an empty array instead of a hash
    a = nil
  else
    raise ArgumentError, "Invalid data for association: '#{var}' = #{a.inspect}"
  end

  instance_variable_set(:"@#{var}", a)
end

#idObject



65
66
67
# File 'lib/beatport/item.rb', line 65

def id
  @table['id']
end

#typeObject



69
70
71
# File 'lib/beatport/item.rb', line 69

def type
  @table['type']
end