template_class 1.1.0 → 1.2.1

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
  SHA256:
3
- metadata.gz: d10dcbdc76cd1f1ffddb70a98ee133e5686acb67ce73583715aeeaa9b10d4e70
4
- data.tar.gz: 5f293dcd3eaf2e54882052e83f2eef8abd2c1fa71792a38f27cd688cbb641072
3
+ metadata.gz: 7009ef2378c645609f8d669f09a1a5f348c598740ebe3593c09b0789cc87ed38
4
+ data.tar.gz: 872c47796a2c4c5d4c972ca73327a14ee8bc4a373b594dfbce2d75e616a7b56e
5
5
  SHA512:
6
- metadata.gz: d536399259e30cc20e4c6352410092aed9b4044a7ab42d4fcfab77b82589d25e64a66a9cccd073da9fbf01a022276e05c4b0acf5afa46dc9e94dc5c04ab94f3d
7
- data.tar.gz: 3ba7a56886ab200f11f827b2078ffb8994b01456215cc6ff8c0c3d45475da06a2d2c19018d1f1f9af6005d190c1c326e1d3a29846b168b2e847a4b7888ad8a71
6
+ metadata.gz: 575c196c51373ac4b20980f177a85427c25032f297681739963df69d22c76d40848aa20f76c115533cd5c4ebbe41eda4873bd48f88ae2ab7570ae896bae77aa4
7
+ data.tar.gz: f789d966d43c6d82f1d5e367f6f114815ce4d347cf8246b813229835200f94cdaa843f4e3ac3fad04c4bdea7bd2791ed71231ba90c5ad25ec6692d08670801d0
data/CHANGELOG.md CHANGED
@@ -8,6 +8,18 @@
8
8
  ### Bug fixes
9
9
  )-->
10
10
 
11
+ ## 1.2.1 2025-01-20
12
+
13
+ ### Bug fixes
14
+
15
+ - Relaxed dependency.
16
+
17
+ ## 1.2.0 2024-08-19
18
+
19
+ ### New features
20
+
21
+ - Bulk instantiation
22
+
11
23
  ## 1.1.0 2023-08-31
12
24
 
13
25
  ### New features
data/README.md CHANGED
@@ -82,6 +82,32 @@ List[:any] = Array
82
82
 
83
83
  Notice that the parameter isn't constrained to classes: you can use any object.
84
84
 
85
+ ### Bulk Instantiation
86
+
87
+ A module (or class) containing multiple templates can include `TemplateClass::BulkInstanceable` to be able to instantiate them in bulk using the same parameter.
88
+
89
+ ```ruby
90
+ module Templates
91
+ include TemplateClass::BulkInstantiable
92
+
93
+ class Template1
94
+ ...
95
+ end
96
+
97
+ class Template2
98
+ ...
99
+ end
100
+ end
101
+
102
+ module Instances
103
+ Templates.bulk_instance Integer, into: self
104
+ end
105
+ ```
106
+
107
+ The new constants `Instances::Template1` and `Instances::Template2` are now defined, and they point respectively to `Templates::Template1[Integer]` and `Templates::Template2[Integer]`.
108
+
109
+ The optional keyword argument `with_overrides`, which defaults to `true` if `Zeitwerk` is defined and to `false` otherwise, allows you to automatically access and reopen the instances in files that follow the Zeitwerk naming conventions (eg. you can reopen `Instances::Template1` in the file `instances/template1.rb`). If `with_overrides` is `false`, you can still reopen the instances, but you need to manually handle the appropriate `requires` in the appropriate order.
110
+
85
111
  ## Plans for future development
86
112
 
87
113
  - Multiple specialization parameters
@@ -0,0 +1,21 @@
1
+ module TemplateClass
2
+ module BulkInstantiable
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ def bulk_instantiate(*args, into:, with_overrides: defined?(Zeitwerk))
7
+ constants(false)
8
+ .map { |constant_name| const_get(constant_name) }
9
+ .map do |constant|
10
+ into.const_set constant.name.demodulize.to_sym, constant[*args]
11
+ end
12
+ .tap { return unless with_overrides } # rubocop:disable Lint/NonLocalExitFromIterator
13
+ .each do |instance|
14
+ require instance.name.underscore
15
+ rescue LoadError
16
+ nil
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TemplateClass
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.1'
5
5
  end
@@ -1,2 +1,3 @@
1
1
  require_relative 'template_class/version'
2
2
  require_relative 'template_class/template'
3
+ require_relative 'template_class/bulk_instantiable'
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/template_class/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'template_class'
7
+ spec.version = TemplateClass::VERSION
8
+ spec.authors = ['Moku S.r.l.', 'Riccardo Agatea']
9
+ spec.email = ['info@moku.io']
10
+ spec.license = 'MIT'
11
+
12
+ spec.summary = 'A way to define templated classes, in a similar fashion to C++ templates.'
13
+ spec.description = 'In most cases Ruby doesn\'t need templated classes, nor any other system of generics, because it isn\'t statically type checked. However, sometimes we need to automatically generate multiple similar classes, either because of poor design or because of external necessities. For example, to define a GraphQL schema with GraphQL Ruby (https://graphql-ruby.org/) we need to define a distinct class for each type. Since GraphQL is statically type checked but doesn\'t provide generics, if we need a set of similar but distinct types we\'re left to define them one by one.'
14
+ spec.homepage = 'https://github.com/moku-io/template_class'
15
+ spec.required_ruby_version = '>= 3.0.0' # Maybe we should check (?)
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/moku-io/template_class'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/moku-io/template_class'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir __dir__ do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_dependency 'activesupport'
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: template_class
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moku S.r.l.
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-08-31 00:00:00.000000000 Z
12
+ date: 2025-01-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: 7.0.0
20
+ version: '0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - "~>"
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: 7.0.0
27
+ version: '0'
28
28
  description: In most cases Ruby doesn't need templated classes, nor any other system
29
29
  of generics, because it isn't statically type checked. However, sometimes we need
30
30
  to automatically generate multiple similar classes, either because of poor design
@@ -47,9 +47,11 @@ files:
47
47
  - README.md
48
48
  - Rakefile
49
49
  - lib/template_class.rb
50
+ - lib/template_class/bulk_instantiable.rb
50
51
  - lib/template_class/template.rb
51
52
  - lib/template_class/version.rb
52
53
  - sig/lib/template_class/template.rbs
54
+ - template_class.gemspec
53
55
  homepage: https://github.com/moku-io/template_class
54
56
  licenses:
55
57
  - MIT
@@ -72,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
74
  - !ruby/object:Gem::Version
73
75
  version: '0'
74
76
  requirements: []
75
- rubygems_version: 3.4.6
77
+ rubygems_version: 3.5.22
76
78
  signing_key:
77
79
  specification_version: 4
78
80
  summary: A way to define templated classes, in a similar fashion to C++ templates.