Class: Arduino::Library::Model

Inherits:
Dry::Struct
  • Object
show all
Extended by:
InstanceMethods, Utilities
Includes:
Comparable
Defined in:
lib/arduino/library/model.rb

Overview

This class represents a single entry into the library-index.json file, in other words — a ‘library.properties` file.

Constant Summary collapse

SEARCHABLE_FIELDS =
%i(name archiveFileName checksum)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InstanceMethods

db_default, db_from, library_from, search

Methods included from Utilities

backup_previous_library, debug, download, open_plain_or_gzipped, read_file_or_url, short_time

Class Attribute Details

.databaseObject



77
78
79
# File 'lib/arduino/library/model.rb', line 77

def database
  @database ||= DefaultDatabase.instance
end

Class Method Details

.find(**opts) ⇒ Object



81
82
83
# File 'lib/arduino/library/model.rb', line 81

def find(**opts)
  database.search(**opts)
end

.from(source = nil, **opts) ⇒ Model | Array<Model>

## Searching

#### Database

Searching requires a database, which can either be set via

Arduino::Library::Model.database = Database.from(file)

otherwise it defaults to the default database, DefaultDatabase.instance.

#### Query

opts is a Hash that you can use to pass attributes with values, any number of them. All matching results are returned as models from the current database.

name: 'AudioZero'
author: /konstantin/i              - regexp supported
architectures: [ 'avr' ]           - array is matched if it's a subset
version: proc do |value|           — or a proc for max flexibility
  value.start_with?('1.') )
ends

Parameters:

  • source (Object) (defaults to: nil)

    — file name or a URL to JSON or .properties file

  • opts (Hash)

    — search parameters to the current database

Returns:

  • (Model | Array<Model>)

    — array for search, otherwise a model



113
114
115
116
117
118
119
120
# File 'lib/arduino/library/model.rb', line 113

def from(source = nil, **opts)
  model = model_by_class(source, **opts)
  if model&.partial?
    Finder.find_library(model)
  else
    model
  end
end

.from_hash(hash) ⇒ Object



59
60
61
# File 'lib/arduino/library/model.rb', line 59

def from_hash(hash)
  new(Types.schema[hash])
end

.from_json(json) ⇒ Object



63
64
65
# File 'lib/arduino/library/model.rb', line 63

def from_json(json)
  from_hash(JSON.load(json))
end

.from_json_file(file_or_url) ⇒ Object



67
68
69
70
# File 'lib/arduino/library/model.rb', line 67

def from_json_file(file_or_url)
  file = read_file_or_url(file_or_url)
  from_json(file.read)
end

.from_properties_file(file_or_url) ⇒ Object



72
73
74
75
# File 'lib/arduino/library/model.rb', line 72

def from_properties_file(file_or_url)
  raise "File #{file_or_url} does not exist?" unless File.exist?(file_or_url)
  Presenters::Properties.from(file_or_url)
end

Instance Method Details

#<=>(another) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/arduino/library/model.rb', line 43

def <=>(another)
  if self.name == another.name
    self.version_to_i <=> another.version_to_i
  else
    self.name <=> another.name
  end
end

#partial?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/arduino/library/model.rb', line 39

def partial?
  self.url.nil? && SEARCHABLE_FIELDS.any? { |field| self.send(field) }
end

#version_to_iObject

Convert a version such as ‘1.44.3’ into a number ‘1044003’ for easy sorting and comparison.



27
28
29
30
31
32
33
34
35
36
# File 'lib/arduino/library/model.rb', line 27

def version_to_i
  if version
    first, second, third = version.split(/\./).map(&:to_i)
    10 ** 6 * (first || 0) + 10 ** 3 * (second || 0) + (third || 0)
  else
    0
  end
rescue
  0
end