# This code is free software; you can redistribute it and/or modify it under
# the terms of the new BSD License.
#
# Copyright (c) 2012-2013, Sebastian Staudt

class Formula

  include Mongoid::Document
  include Mongoid::Timestamps::Updated

  field :_id, type: String
  field :aliases, type: Array
  field :date, type: Time
  field :keg_only, type: Boolean, default: false
  field :removed, type: Boolean, default: false
  field :name, type: String
  field :homepage, type: String
  field :path, type: String
  field :version, type: String

  after_build :set_id

  alias_method :to_param, :name

  belongs_to :repository
  has_and_belongs_to_many :revisions, inverse_of: nil

  has_and_belongs_to_many :deps, class_name: 'Formula', inverse_of: :revdeps
  has_and_belongs_to_many :revdeps, class_name: 'Formula', inverse_of: :deps

  scope :letter, ->(letter) { where(name: /^#{letter.downcase}/) }

  def path
    path = repository.full? ? File.join('Library', 'Formula') : self[:path]
    (path.nil? ? name : File.join(path, name)) + '.rb'
  end

  def raw_url
    "https://raw.github.com/#{repository.name}/HEAD/#{path}"
  end

  private

  def set_id
    self._id = "#{repository.name}/#{name}"
  end

end

# This code is free software; you can redistribute it and/or modify it under
# the terms of the new BSD License.
#
# Copyright (c) 2012-2013, Sebastian Staudt

class Formula

  include Mongoid::Document
  include Mongoid::Timestamps::Updated

  field :_id, type: String
  field :aliases, type: Array
  field :date, type: Time
  field :keg_only, type: Boolean, default: false
  field :removed, type: Boolean, default: false
  field :name, type: String
  field :homepage, type: String
  field :path, type: String
  field :version, type: String

  after_build :set_id

  alias_method :to_param, :name

  belongs_to :repository
  has_and_belongs_to_many :revisions, inverse_of: nil

  has_and_belongs_to_many :deps, class_name: self.to_s, inverse_of: :revdeps
  has_and_belongs_to_many :revdeps, class_name: self.to_s, inverse_of: :deps

  scope :letter, ->(letter) { where(name: /^#{letter.downcase}/) }

  def path
    path = repository.full? ? File.join('Library', 'Formula') : self[:path]
    (path.nil? ? name : File.join(path, name)) + '.rb'
  end

  def raw_url
    "https://raw.github.com/#{repository.name}/HEAD/#{path}"
  end

  private

  def set_id
    self._id = "#{repository.name}/#{name}"
  end

end

Use self.to_s in Mongoid class_name in Formula
Use self.to_s in Mongoid class_name in Formula
