Class: Homebrew::Formula

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/homebrew/formula.rb,
app/models/homebrew/formula/description.rb

Overview

A Homebrew formula

Author:

  • guillaumeh

Defined Under Namespace

Classes: Description

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object


145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/homebrew/formula.rb', line 145

def as_json(options = {})
  {
    formula: name.downcase,
    description: description.to_s,
    reference: reference.to_s,
    homepage: homepage.to_s,
    version: version.to_s,
    dependencies: dependencies.map(&:name).sort,
    dependents: formula_dependents.map(&:formula).map(&:name).sort
  }
end

#dependent_namesArray

Names of the formulas which are dependent on the current formula.

Returns:

  • (Array)

    with the names of the fromulas


102
103
104
# File 'app/models/homebrew/formula.rb', line 102

def dependent_names
  dependents.map(&:name)
end

#descriptionString

Get the description of the formula

Returns:

  • (String)

    description of the formula or “No description available”


71
72
73
# File 'app/models/homebrew/formula.rb', line 71

def description
  self[:description] || 'No description available'
end

#description?Boolean

Determine if the formula has a description

Returns:

  • (Boolean)

    true if the formula has a description in DB otherwise false


93
94
95
# File 'app/models/homebrew/formula.rb', line 93

def description?
  self[:description].present?
end

#detect_serviceObject


106
107
108
109
# File 'app/models/homebrew/formula.rb', line 106

def detect_service
  service_detection = ServiceDetection.new(homepage)
  service_detection.detected_service
end

#new?Boolean

Get if the formula is new

Returns:

  • (Boolean)

    return true if new otherwise false


141
142
143
# File 'app/models/homebrew/formula.rb', line 141

def new?
  created_at.to_date == Time.now.utc.to_date
end

#referenceObject


133
134
135
# File 'app/models/homebrew/formula.rb', line 133

def reference
  "Extracted automatically from #{name} homepage" if description_automatic?
end

#touchObject

Set the touched_on field

The touched_on field is used in order to detect deleted formula. When this field is older than the day of today, it means the formula wasn't touched when the background worker ran, so the formula wasn't present from the Homebrew git repo.


84
85
86
# File 'app/models/homebrew/formula.rb', line 84

def touch
  self.touched_on = Time.now.utc.to_date
end

#update_description_from!(html) ⇒ Object


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/homebrew/formula.rb', line 111

def update_description_from!(html)
  # Update the detected_service
  self.detected_service = detect_service

  # Initialize a new Homebrew::Formula::Description
  # which will be responsible to extract the formula
  # description from the readed homepage content.
  description = Homebrew::Formula::Description.new(self)
  description.lookup_from(html)

  # In the case a description has been found
  if description.found?
    unless update_attributes(
      description: description.text,
      description_automatic: true
    )
      Rails.logger.warn 'Unable to update description with text ' \
                        "#{description.text}"
    end
  end
end