cli-dispatcher 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/structured.rb +70 -1
  3. metadata +7 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 850d024672db8785ec7d329961a30baf9e8d4f1724189f6cf6250472cc17e5e8
4
- data.tar.gz: 11e2ab38ddd9cc99d7ec2b66bf4b97d229a470ded4295ef2e7ecddca99bca74e
3
+ metadata.gz: e946454a75a033c55b82ca4814654e27e75f8635ea02b42ad5fbd95a6cc7b4c8
4
+ data.tar.gz: f964e67077a8e2077a44db8b033d5a82be7fcbe10e9f2cad11504b6116e8397e
5
5
  SHA512:
6
- metadata.gz: 3f5a3f85f1f64785b526df560efe2ef2ee94b031d0ef9bd47d43fd6500eaf4faba0b70a6230d1ccbc49db4d9a8d0681a1ba17edd847c105ab0b8d106568ccb2f
7
- data.tar.gz: e6c3a31bd0825b3ae234dd68b6432e5a5b7f0fe4daf76a9494545018d264615630f89368b1c352d950be23a66be76e36fa5a3059bb38ad411d37f91912480f5a
6
+ metadata.gz: d93f6c815a777f40501094b84bc386178f8a45b20b6dccb4809667d3c7f855b2899a1aa48df04e9cc71eee43353b4105dd911ad1c422f187e276193aa75e8446
7
+ data.tar.gz: 6d81ee52b9cac75a9446b221b7b8c8edf767b5998146a87a14bfe0e9353e80ebfbb4b57ce639c3d2d7c1b7f5fb29c1bc809197b3d07aa71a7cfd7c355295eb99
data/lib/structured.rb CHANGED
@@ -36,6 +36,8 @@ require 'yaml'
36
36
  # As a result, at the end of the initialization of a Structured object, it will
37
37
  # have instance variables set corresponding to all the defined elements.
38
38
  #
39
+ # == Customization of Structured Classes
40
+ #
39
41
  # The above explanation is default behavior, and several customizations are
40
42
  # available.
41
43
  #
@@ -58,6 +60,57 @@ require 'yaml'
58
60
  # Please read the documentation for Structured::ClassMethods for more on
59
61
  # defining expected elements, type checking, and so on.
60
62
  #
63
+ # == Subfiles as Input
64
+ #
65
+ # The Structured class provides automatic support for separating inputs into
66
+ # YAML subfiles. This is useful for including complex objects in a file. Two
67
+ # types of subfile inputs are supported: those for object hashes, and those for
68
+ # arrays.
69
+ #
70
+ # To include a subfile as part of an object hash, include the key `read_file` in
71
+ # the hash, with the value being the file to be read. (Other keys besides
72
+ # `read_file` may be included.) The subfile should itself contain YAML for a
73
+ # hash with further keys for the object.
74
+ #
75
+ # To include multiple subfiles in an array, set the first element of the array
76
+ # to the string `read_file`, and then the other elements of the array should be
77
+ # filenames. These subfiles should contain YAML for arrays.
78
+ #
79
+ # Consider a Structured object for a book, containing elements for the title,
80
+ # subtitle, and an array of authors. The input file could look like this:
81
+ #
82
+ # ---
83
+ # title: A Book
84
+ # subtitle: Containing Many Pages
85
+ # author:
86
+ # - John Q. Public
87
+ # - Jane Doe
88
+ #
89
+ # Using the subfile feature, the input file could instead look like:
90
+ #
91
+ # ---
92
+ # title: A Book
93
+ # read_file: subtitle_file.yaml
94
+ # author:
95
+ # - read_file
96
+ # - author_file.yaml
97
+ #
98
+ # This would instruct Structured to read hash keys out of `subtitle_file.yaml`,
99
+ # and to read array elements out of `author_file.yaml`. These two files, in
100
+ # turn, should look like:
101
+ #
102
+ # # subtitle_file.yaml
103
+ # ---
104
+ # subtitle: Containing Many Pages
105
+ #
106
+ # # author_file.yaml
107
+ # ---
108
+ # - John Q. Public
109
+ # - Jane Doe
110
+ #
111
+ # When incorporated, Structured will combine these subfiles as if they were a
112
+ # single object specification.
113
+ #
61
114
  module Structured
62
115
 
63
116
  #
@@ -289,7 +342,7 @@ module Structured
289
342
  def default_element(*args, **params)
290
343
  if (key_params = params.delete(:key))
291
344
  @default_key = element_data(
292
- key_params.delete(:type) || Object, key_params
345
+ key_params.delete(:type) || Object, **key_params
293
346
  )
294
347
  else
295
348
  @default_key = element_data(Symbol, preproc: proc { |s| s.to_sym })
@@ -451,6 +504,21 @@ module Structured
451
504
  end
452
505
  end
453
506
 
507
+ def try_read_array(filenames)
508
+ new_item = []
509
+ begin
510
+ filenames.each do |file|
511
+ begin
512
+ res = YAML.load_file(file)
513
+ raise InputError unless res.is_a?(Array)
514
+ new_item.concat(res)
515
+ rescue
516
+ input_err("Failed to read array from #{file}: #$!")
517
+ end
518
+ end
519
+ end
520
+ return new_item
521
+ end
454
522
 
455
523
  #
456
524
  # Given an element value and an #element_data hash of processing tools
@@ -534,6 +602,7 @@ module Structured
534
602
  when Array
535
603
  input_err("#{item} is not Array") unless item.is_a?(Array)
536
604
  Structured.trace(Array) do
605
+ item = try_read_array(item[1..-1]) if item.first.to_s == 'read_file'
537
606
  return item.map.with_index { |i, idx|
538
607
  Structured.trace(idx) do
539
608
  convert_item(i, type.first, parent)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cli-dispatcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Duan
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-15 00:00:00.000000000 Z
11
+ date: 2024-12-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Library for creating command-line programs that accept commands. Also
@@ -28,7 +28,8 @@ licenses:
28
28
  - MIT
29
29
  metadata:
30
30
  source_code_uri: https://github.com/charlesduan/cli-dispatcher
31
- post_install_message:
31
+ documentation_uri: https://rubydoc.info/gems/cli-dispatcher/
32
+ post_install_message:
32
33
  rdoc_options: []
33
34
  require_paths:
34
35
  - lib
@@ -43,8 +44,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
44
  - !ruby/object:Gem::Version
44
45
  version: '0'
45
46
  requirements: []
46
- rubygems_version: 3.0.3.1
47
- signing_key:
47
+ rubygems_version: 3.5.11
48
+ signing_key:
48
49
  specification_version: 4
49
50
  summary: Command-line command dispatcher
50
51
  test_files: []