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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f09c5ae55f674a4ad8b4bc76254597886a23049ae89a7ed6d92721f441f12f28
4
- data.tar.gz: b03aadea4384d63f6762a20661e5224a2e8d41649b1ad2ea68fae54062a47def
3
+ metadata.gz: 6bc77b2428d997de38f6a43403419c6309bfccfa7447cb05ff4a2400d554d4be
4
+ data.tar.gz: e8ad9473ff79bd780755e0bf363ffbd7ed9d4d36055d7e1a8c7ffa726b103d7b
5
5
  SHA512:
6
- metadata.gz: 6a08f15a341afb32219e9ef96d2730895201c9a0b211c26bd457dd9784573dddc3f379fa480c473781d90d43e395e98c85a78c3a92f846c9637c63455ad1de9a
7
- data.tar.gz: eebfa90ca1b931055cfd16b230541d53d313c1650f4e681a4d95ba6a49485866824505500ec9287e5e7273dd8032075188e2d47c12a5baf695a07cd3686f6d44
6
+ metadata.gz: 077d728c7dc6ec187a1f2022e2e5bb5c95f99552e485e427e466d9cf42a35c5648d0f4a33556f6f5d44cdc71b825e33adfb4eb9be24ebee27f9c3c92d4ae739a
7
+ data.tar.gz: 4acd129fe10323bf1615edf6a9acd0f607daf1b91d05b58142b91a6adc45acec5e516e7d1d896c99efd8347335fd5a58135b8866698d144afc318505e6f3d7e3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## [1.1.0] - 2020-01-10
2
+ ### Added
3
+ - Attribute value autocompletion for the ActiveData methods `where` and `find_by`.
4
+
1
5
  ## [1.0.0] - 2020-01-09
2
6
  ### Added
3
7
  - Initial release.
data/Gemfile CHANGED
@@ -1,7 +1,7 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in data-files.gemspec
4
4
  gemspec
5
5
 
6
- gem "rake", "~> 12.0"
7
- gem "minitest", "~> 5.0"
6
+ gem 'minitest', '~> 5.0'
7
+ gem 'rake', '~> 12.0'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- data_files (1.0.0.rc6)
4
+ data_files (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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:
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Base class for data querying and manipulation.
4
3
  module DataFiles
4
+ # Base class for data querying and manipulation.
5
5
  class ActiveData
6
6
  attr_reader :errors
7
7
 
@@ -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
@@ -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
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DataFiles
2
- VERSION = "1.0.0"
4
+ VERSION = '1.1.0'
3
5
  end
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.0.0
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-09 00:00:00.000000000 Z
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