arduino-library 0.5.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32c6c38ed6001c70620f9a72bc1baad8b5d82a2c
4
- data.tar.gz: '0791f871e9b87f0ed40bbac061d88baaab67cd69'
3
+ metadata.gz: 238adfe1709888149a3316933804737c16d5d696
4
+ data.tar.gz: d44086c49e7b5d41306ba1b9e2fde18c83a8dc1f
5
5
  SHA512:
6
- metadata.gz: 4105c73cb121404bc8311070e581f32b95d7efa1ea3b313b0b3d10431d7846aa3e09c3de916ba5766c14b068f2dd12adc63b43c615f27aad3fc238df27d16890
7
- data.tar.gz: e0e1d7e66476d4e06ae7b974ae03f7b1adc34c0e345aa584206159ecedc718877d4cb028cb42f171f9938fdaa06b59ce3f995209d6d460eaf994466300b66de8
6
+ metadata.gz: 4d111c2a09af56569e48573375c00b46a2143d592de8fd164b66fa4bc6f9094611f2416e3edfefa7f365933eee39517468169c59d66d607a91a855d350c78bfe
7
+ data.tar.gz: e06b0c34cbf08cb2413fab35e6d0102044ea3259b3edc6444f926115dc65f2b433f2bf28aeb47351e3fdbfeec7e6c496a7f459729da60f403b6f457527471233
data/README.md CHANGED
@@ -87,7 +87,7 @@ There are two ways to include the DSL in your context:
87
87
  ```ruby
88
88
  require 'arduino/library'
89
89
  class Foo
90
- include Arduino::Library::InstanceMethods
90
+ include Arduino::Library
91
91
  end
92
92
  ```
93
93
 
@@ -26,7 +26,15 @@ require 'arduino/library/instance_methods'
26
26
 
27
27
  module Arduino
28
28
  module Library
29
- include InstanceMethods
29
+ class << self
30
+ def included(base)
31
+ base.include(::Arduino::Library::InstanceMethods)
32
+ base.include(::Arduino::Library::Finder::FinderMethods)
33
+ end
34
+
35
+ included(Arduino::Library)
36
+ end
37
+
30
38
  end
31
39
  end
32
40
 
@@ -11,11 +11,9 @@ module Arduino
11
11
  # query based this information. If multiple entries returned,
12
12
  # but for the same library, the latest version is returned.
13
13
  module Finder
14
- class << self
15
- include ::Arduino::Library::InstanceMethods
16
-
14
+ module FinderMethods
17
15
  # Finds a given model with only partial data by
18
- # searching in the Ardiuino Database.
16
+ # searching in the Arduino Database.
19
17
  #
20
18
  # model = Arduino::Library::Finder.find({ name: 'AudioZero'} )
21
19
  # # => <Arduino::Library::Model#0x3242gfa2...>
@@ -24,18 +22,21 @@ module Arduino
24
22
  #
25
23
  # @param [Model] model with a partial information only, such as the name.
26
24
  # @return [Model] a found model with #url provided, if found, nil otherwise.
27
- def find(model)
25
+ def find_library(model, version: :latest)
28
26
  raise ArgumentError, 'Model argument is required' unless model
29
-
30
27
  model = Model.from(model) unless model.is_a?(Model)
31
- return model unless model.partial?
28
+ return model unless model&.partial?
32
29
 
33
30
  query = construct_query(model)
34
31
  return nil if query.empty?
35
32
 
36
- latest_version_by(query)
33
+ get_library_version(query, version: version)
37
34
  end
38
35
 
36
+ alias find find_library
37
+
38
+ private
39
+
39
40
  # Given a model with partial information, constructs a query
40
41
  # by name and version.
41
42
  # @param [Model] model a library model with name, and optionally version
@@ -52,7 +53,7 @@ module Arduino
52
53
  #
53
54
  # @param [Hash] query model attributes to search for, eg, +name: 'AudioZero'
54
55
  # @return <Model> search result, or the most recent version if many match
55
- def latest_version_by(query)
56
+ def get_library_version(query, version: :latest)
56
57
  results = if query.key?(:name)
57
58
  Model.find(**query).sort
58
59
  else
@@ -60,9 +61,25 @@ module Arduino
60
61
  end
61
62
  return nil if results.size == 0
62
63
  return results.first if results.size == 1
63
- results.last
64
+
65
+ if version == :latest
66
+ results.last
67
+ elsif version == :oldest
68
+ results.last
69
+ elsif version =~ /^[0-9.]*$/
70
+ results.find { |r| r.version == version }
71
+ else
72
+ raise ArgumentError, "Invalid version specified in arguments — #{version}." +
73
+ "Expecting either :latest, :oldest, or a specific version number."
74
+ end
64
75
  end
65
76
  end
77
+
78
+
79
+ class << self
80
+ include ::Arduino::Library::InstanceMethods
81
+ include ::Arduino::Library::Finder::FinderMethods
82
+ end
66
83
  end
67
84
  end
68
85
  end
@@ -1,3 +1,3 @@
1
1
  require 'arduino/library'
2
2
 
3
- include Arduino::Library::InstanceMethods
3
+ include Arduino::Library
@@ -27,7 +27,7 @@ module Arduino
27
27
  def version_to_i
28
28
  if version
29
29
  first, second, third = version.split(/\./).map(&:to_i)
30
- 10**6 * (first || 0) + 10**3 * (second || 0) + (third || 0)
30
+ 10 ** 6 * (first || 0) + 10 ** 3 * (second || 0) + (third || 0)
31
31
  else
32
32
  0
33
33
  end
@@ -37,7 +37,7 @@ module Arduino
37
37
 
38
38
  # @returns true if the library has enough data to be searched in the db
39
39
  def partial?
40
- self.url.nil? && SEARCHABLE_FIELDS.any?{ |field| self.send(field) }
40
+ self.url.nil? && SEARCHABLE_FIELDS.any? { |field| self.send(field) }
41
41
  end
42
42
 
43
43
  def <=>(another)
@@ -110,40 +110,48 @@ module Arduino
110
110
  # ends
111
111
  #
112
112
  # @return [Model | Array<Model> ] — array for search, otherwise a model
113
- def from(source = nil, ** opts)
114
- model = case source
115
- when Hash
116
- from_hash(source)
117
- when String
118
- if source =~ /^{/m
119
- from_json(source)
120
- elsif File.exist?(source)
121
- if source =~ /\.json(\.gz)?$/i
122
- from_json_file(source)
123
- elsif source =~ /\.properties(\.gz)?$/i
124
- from_properties_file(source)
125
- end
126
- end
127
- when NilClass
128
- if opts
129
- if SEARCHABLE_FIELDS.any?{ |field| opts[field] }
130
- results = search(**opts)
131
- if results
132
- results.sort.last
133
- else
134
- nil
135
- end
136
- else
137
- from_hash(opts)
138
- end
139
- end
140
- end
113
+ def from(source = nil, **opts)
114
+ model = model_by_class(source, **opts)
141
115
  if model&.partial?
142
- Finder.find(model)
116
+ Finder.find_library(model)
143
117
  else
144
118
  model
145
119
  end
146
120
  end
121
+
122
+ private
123
+
124
+ def model_by_class(source, **opts)
125
+ case source
126
+ when Hash
127
+ from_hash(source)
128
+ when String
129
+ if source =~ /^{/m
130
+ from_json(source)
131
+ elsif File.exist?(source)
132
+ if source =~ /\.json(\.gz)?$/i
133
+ from_json_file(source)
134
+ elsif source =~ /\.properties(\.gz)?$/i
135
+ from_properties_file(source)
136
+ end
137
+ end
138
+ when NilClass
139
+ if opts
140
+ if SEARCHABLE_FIELDS.any? { |field| opts[field] }
141
+ results = search(**opts)
142
+ if results
143
+ results.sort.last
144
+ else
145
+ nil
146
+ end
147
+ else
148
+ from_hash(opts)
149
+ end
150
+ end
151
+ else
152
+ nil
153
+ end
154
+ end
147
155
  end
148
156
  end
149
157
  end
@@ -1,6 +1,6 @@
1
1
  module Arduino
2
2
  module Library
3
- VERSION = '0.5.5'
3
+ VERSION = '0.6.0'
4
4
  DESCRIPTION = <<-eof
5
5
  This library provides a convenient ruby API for representation of an Arduino Library specification, including field and type validation, reading and writing the library.properties file, as well as downloading the official database of Arduino libraries, and offering a highly advanced searching functionality. This gem only offers Ruby API, but for command line usage please checkout the gem called "arli" — Arduino Library Dependency Manager that uses this library behind the scenes.
6
6
  eof
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arduino-library
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Gredeskoul
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-04 00:00:00.000000000 Z
11
+ date: 2018-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable