data_files 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile +3 -3
- data/Gemfile.lock +1 -1
- data/README.md +11 -0
- data/lib/data_files/active_data.rb +1 -1
- data/lib/data_files/autocompletion.rb +57 -0
- data/lib/data_files/repl.rb +4 -2
- data/lib/data_files/version.rb +3 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6bc77b2428d997de38f6a43403419c6309bfccfa7447cb05ff4a2400d554d4be
|
4
|
+
data.tar.gz: e8ad9473ff79bd780755e0bf363ffbd7ed9d4d36055d7e1a8c7ffa726b103d7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 077d728c7dc6ec187a1f2022e2e5bb5c95f99552e485e427e466d9cf42a35c5648d0f4a33556f6f5d44cdc71b825e33adfb4eb9be24ebee27f9c3c92d4ae739a
|
7
|
+
data.tar.gz: 4acd129fe10323bf1615edf6a9acd0f607daf1b91d05b58142b91a6adc45acec5e516e7d1d896c99efd8347335fd5a58135b8866698d144afc318505e6f3d7e3
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -54,6 +54,17 @@ The internal `_id` attribute is ephemeral and can change between different sessi
|
|
54
54
|
#<Game title: "Super Mario Maker 2", url: "https://www.nintendo.com/games/detail/super-mario-maker-2-switch/", year: 2019, _id: 12>
|
55
55
|
```
|
56
56
|
|
57
|
+
## Autocompletion
|
58
|
+
|
59
|
+
The methods `where` and `find_by` support autocompletion for attribute values which can be invoked by pressing the TAB key. If several suggestions are available, the TAB key needs to be pressed twice to show the suggestions.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
> Game.where(title: "⇥
|
63
|
+
A Light In Chorus Another World Donut County wipE'out
|
64
|
+
Advance Wars BOXBOY! Firewatch
|
65
|
+
Animal Crossing: New Leaf Commander Keen in Goodbye, Galaxy Xenon 2: Megablast
|
66
|
+
```
|
67
|
+
|
57
68
|
## Creating new data
|
58
69
|
|
59
70
|
We can add new items to our data file:
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DataFiles
|
4
|
+
# Provides attribute value autocompletion for the ActiveData methods where and find_by.
|
5
|
+
class Autocompletion
|
6
|
+
ALLOWED_METHODS = %w[where find_by'].freeze
|
7
|
+
REGEX_OPENING_PARENTHESIS_OR_WHITESPACE = /\(|\s/.freeze
|
8
|
+
|
9
|
+
ParsedInput = Struct.new(:klass_name, :method_name, :attribute_name, :attribute_value) do
|
10
|
+
def valid?
|
11
|
+
klass_name && method_name && attribute_name
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
Readline.completion_append_character = ''
|
17
|
+
Readline.completion_proc = proc do
|
18
|
+
parsed = parse_input(Readline.line_buffer)
|
19
|
+
|
20
|
+
if parsed.valid?
|
21
|
+
suggestions(
|
22
|
+
parsed.klass_name,
|
23
|
+
parsed.method_name,
|
24
|
+
parsed.attribute_name,
|
25
|
+
parsed.attribute_value
|
26
|
+
)
|
27
|
+
else
|
28
|
+
[]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def parse_input(input)
|
34
|
+
klass_name, remainder = input.split('.')
|
35
|
+
method_name, attribute_name, attribute_value = remainder&.split(REGEX_OPENING_PARENTHESIS_OR_WHITESPACE)
|
36
|
+
attribute_name = attribute_name&.sub(':', '')&.strip
|
37
|
+
attribute_value = attribute_value&.sub('"', '')&.sub('\'', '')
|
38
|
+
|
39
|
+
ParsedInput.new(klass_name, method_name, attribute_name, attribute_value)
|
40
|
+
end
|
41
|
+
|
42
|
+
def suggestions(klass_name, method_name, attribute_name, attribute_value)
|
43
|
+
return [] unless Object.const_defined?(klass_name)
|
44
|
+
return [] unless ALLOWED_METHODS.include?(method_name)
|
45
|
+
|
46
|
+
values = Object.const_get(klass_name).data.collect do |item|
|
47
|
+
item[attribute_name]
|
48
|
+
end
|
49
|
+
|
50
|
+
if attribute_value
|
51
|
+
values.select { |value| value.to_s.start_with?(attribute_value) }
|
52
|
+
else
|
53
|
+
values
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/data_files/repl.rb
CHANGED
@@ -4,10 +4,11 @@ require 'date'
|
|
4
4
|
require 'readline'
|
5
5
|
require 'yaml'
|
6
6
|
require_relative 'active_data.rb'
|
7
|
+
require_relative 'autocompletion.rb'
|
7
8
|
|
8
|
-
# Loads all yaml files in given directory, creates ActiveData subclass
|
9
|
-
# for each file and provides an interactive shell.
|
10
9
|
module DataFiles
|
10
|
+
# Loads all yaml files in given directory, creates ActiveData subclass
|
11
|
+
# for each file and provides an interactive shell.
|
11
12
|
class REPL
|
12
13
|
def initialize(directory)
|
13
14
|
@klass_names = []
|
@@ -43,6 +44,7 @@ module DataFiles
|
|
43
44
|
|
44
45
|
def prompt
|
45
46
|
initial_prompt
|
47
|
+
DataFiles::Autocompletion.new
|
46
48
|
read_input
|
47
49
|
end
|
48
50
|
|
data/lib/data_files/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_files
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Zecher
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: This interactive shell allows users to manipulate Middleman Data Files
|
14
14
|
with an API similar to ActiveRecord.
|
@@ -32,6 +32,7 @@ files:
|
|
32
32
|
- data_files.gemspec
|
33
33
|
- exe/data_files
|
34
34
|
- lib/data_files/active_data.rb
|
35
|
+
- lib/data_files/autocompletion.rb
|
35
36
|
- lib/data_files/repl.rb
|
36
37
|
- lib/data_files/version.rb
|
37
38
|
homepage: https://github.com/pixelate/data-files
|