class Beatport::Item

Public Class Methods

associations() click to toggle source
# File lib/beatport/item.rb, line 5
def associations
  @associations ||= {}
end
find_by_name(name, *args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/beatport/item.rb, line 19
def lazy_accessor(var)
  return if respond_to?(var)
  class_eval "def #{var}; @#{var}; end"
end
new(data = {}) click to toggle source
Calls superclass method
# 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|
    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

Public Instance Methods

[](key) click to toggle source

Allow treating the item as a hash

# File lib/beatport/item.rb, line 75
def [](key)
  send(key) if respond_to?(key)
end
associate(data, var, collection = false, klass = Item) click to toggle source
# 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
    # 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
id() click to toggle source
# File lib/beatport/item.rb, line 66
def id
  @table['id']
end
type() click to toggle source
# File lib/beatport/item.rb, line 70
def type
  @table['type']
end