module Arduino::Library::Finder::FinderMethods
Public Instance Methods
find_library(model, version: :latest)
click to toggle source
Finds a given model with only partial data by searching in the Arduino
Database
.
model = Arduino::Library::Finder.find({ name: 'AudioZero'} ) # => <Arduino::Library::Model#0x3242gfa2...> model.url # => 'https://github.com/.......'
@param [Model] model with a partial information only, such as the name. @return [Model] a found model with url provided, if found, nil otherwise.
# File lib/arduino/library/finder.rb, line 25 def find_library(model, version: :latest) raise ArgumentError, 'Model argument is required' unless model model = Model.from(model) unless model.is_a?(Model) return model unless model&.partial? query = construct_query(model) return nil if query.empty? get_library_version(query, version: version) end
Also aliased as: find
Private Instance Methods
construct_query(model)
click to toggle source
Given a model with partial information, constructs a query by name and version. @param [Model] model a library model with name, and optionally version @return [Hash] query to be passed to search. (See Arduino::Library::InstanceMethods#search
)
# File lib/arduino/library/finder.rb, line 44 def construct_query(model) query = {} query.merge!(name: model.name) if model.name query.merge!(version: model.version) if model.version query end
get_library_version(query, version: :latest)
click to toggle source
Executes a given query, and if more than one version is returned returns the last most recent version of the library.
@param [Hash] query model attributes to search for, eg, +name: 'AudioZero' @return <Model> search result, or the most recent version if many match
# File lib/arduino/library/finder.rb, line 56 def get_library_version(query, version: :latest) results = if query.key?(:name) Model.find(**query).sort else Model.find(**query) end return nil if results.size == 0 return results.first if results.size == 1 if version == :latest results.last elsif version == :oldest results.last elsif version =~ /^[0-9.]*$/ results.find { |r| r.version == version } else raise ArgumentError, "Invalid version specified in arguments — #{version}." + "Expecting either :latest, :oldest, or a specific version number." end end