lowdata 0.4.0 → 0.4.2

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: 5b4ee6fd29a6d15edeb3d85e8517c7b7a3a8d2256e8839479bf5596087879da9
4
- data.tar.gz: a383a5cd9e4153e59ebe872af635bd7b96880a0fb38e347f8e6a6094d0163e73
3
+ metadata.gz: 27f2f5c6b645472ee8de9fe796bdef23493d50dc234998cb78c4a49d417f6d1b
4
+ data.tar.gz: 2f1fbb112d7dbcc9be8053b660f88a7781196e6a8cfc7e1d23b479651fb0779c
5
5
  SHA512:
6
- metadata.gz: '009095ae108f1118421d6ca8945b66e93664ed1b1b45f8fb120c9204d3a359f4b16d789a60f0001a07cd058b788c25484929f020fc6dba19e36e432cbee7eb4d'
7
- data.tar.gz: 6431685585fe4c8cbd8703ebe7c17c50dd22292f4055912c65ba0673e7af424b8be54f4a41c444190f432aa90b248b939ea83f53827961367b354b81932037eb
6
+ metadata.gz: 37de426f6f987b98618492a48c37b060ff732e89b568cbe1ff6032e9805c28d28988deaa25cb236e0a7c2e951fb3d0ccda8dbd4a6b3af740732ec4f714af6b26
7
+ data.tar.gz: a1b741111a08db463198e8d7edd9c84dc6e88ccac97505d00b8608745f176b8d8f527465e30c510e44534792c6e4066e1d694d95edd33bb03e3adbed384ac97f
data/lib/lowdata.rb CHANGED
@@ -23,6 +23,7 @@ require_relative 'writer'
23
23
  require_relative 'data_expression/writes'
24
24
  require_relative 'table_reference'
25
25
  require_relative 'table_reference/writes'
26
+ require_relative 'queries'
26
27
 
27
28
  class LowData
28
29
  class << self
@@ -49,6 +50,19 @@ class LowData
49
50
  # is, so it's a real method rather than a method_missing lookup. Existing class methods, like `table`, are kept.
50
51
  def method_added(name)
51
52
  super
53
+ define_factory_method(name)
54
+ end
55
+
56
+ # The Method Factory pattern for a module's methods too, like a module of shared queries, as method_added only sees
57
+ # methods defined in the class.
58
+ def include(*modules)
59
+ super.tap do
60
+ modules.each { |mod| mod.public_instance_methods.each { |name| define_factory_method(name) } }
61
+ end
62
+ end
63
+
64
+ # Defines a class method that calls the instance method of the same name on a new instance.
65
+ def define_factory_method(name)
52
66
  return if self == LowData || !public_method_defined?(name)
53
67
  return unless name.match?(/\A[a-z_]\w*[?!]?\z/) # Setters like `title=` stay instance methods.
54
68
  return if factory_method?(name) # A parent's class method already calls `new` on this class.
@@ -60,6 +74,7 @@ class LowData
60
74
  def #{name}(...) = new.public_send(:#{name}, ...) # def update(...) = new.public_send(:update, ...)
61
75
  RUBY
62
76
  end
77
+ private :define_factory_method
63
78
 
64
79
  # Resolve undefined constants in a repository to tables, so `Users` and `BlogPosts` become :users and :blog_posts.
65
80
  # `Table` is the repository's own table, joined to whichever related tables own the columns asked for.
data/lib/nesting.rb CHANGED
@@ -51,7 +51,7 @@ class LowData
51
51
  raise ConfigError, ConfigError::MISSING_DATABASE if database.nil?
52
52
 
53
53
  @database = database
54
- @schema = Schema.new(database:)
54
+ @schema = Schema.for(database)
55
55
  @filters = FilterCompiler.new(database:, schema: @schema)
56
56
  @orders = OrderCompiler.new(schema: @schema)
57
57
  @queries = QueryCompiler.new(database:)
data/lib/queries.rb ADDED
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ class LowData
4
+ # Include in a module of queries that repositories share, so its tables and Col are found as they are in a
5
+ # repository. Ruby looks a constant up where its method is written, so without this `Users` in the module isn't a table:
6
+ #
7
+ # module LatestQueries
8
+ # include LowData::Queries
9
+ #
10
+ # def latest(count: 10) = Table(published: true)[:title, :username] ^ -Col[:created_at] * count
11
+ # end
12
+ #
13
+ # class PostsData < LowData
14
+ # include LatestQueries # PostsData.latest, like the repository's own methods
15
+ # end
16
+ module Queries
17
+ def self.included(queries)
18
+ queries.extend(Tables)
19
+ end
20
+
21
+ # Resolves the module's undefined constants to tables, as LowData.const_missing does in a repository. `Table` is the
22
+ # repository's own table, so it's written `Table(...)`, a method call that's resolved in the repository.
23
+ module Tables
24
+ def const_missing(name)
25
+ return LowData::Col if name == :Col
26
+ raise NameError, "Write Table(...) in #{self}, as it's the table of the repository that includes it" if name == :Table
27
+
28
+ LowData::TableReference.new(table: LowData.table_name(name))
29
+ end
30
+ end
31
+ end
32
+ end
@@ -14,7 +14,7 @@ class LowData
14
14
  raise ConfigError, ConfigError::MISSING_DATABASE if database.nil?
15
15
 
16
16
  @database = database
17
- @schema = Schema.new(database:)
17
+ @schema = Schema.for(database)
18
18
  @filters = FilterCompiler.new(database:, schema: @schema)
19
19
  @orders = OrderCompiler.new(schema: @schema)
20
20
  end
data/lib/schema.rb CHANGED
@@ -1,8 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class LowData
4
- # Reads tables, columns and foreign keys from the database, caching each lookup.
4
+ # Reads tables, columns and foreign keys from the database, caching each lookup. Every query shares one per database,
5
+ # so a lookup is made once while the app runs. `Schema.reset` forgets them, as after a migration.
5
6
  class Schema
7
+ def self.for(database)
8
+ @instances ||= {}
9
+ @instances[database] ||= new(database:)
10
+ end
11
+
12
+ def self.reset
13
+ @instances = {}
14
+ end
15
+
6
16
  def initialize(database:)
7
17
  @database = database
8
18
  @schemas = {}
@@ -21,10 +31,14 @@ class LowData
21
31
  @foreign_keys[table] ||= @database.foreign_key_list(table)
22
32
  end
23
33
 
34
+ def tables
35
+ @tables ||= @database.tables
36
+ end
37
+
24
38
  # Tables with a foreign key to or from the table.
25
39
  def related_tables(table:)
26
40
  outgoing = foreign_keys(table:).map { |foreign_key| foreign_key[:table] }
27
- incoming = @database.tables.select do |other|
41
+ incoming = tables.select do |other|
28
42
  foreign_keys(table: other).any? { |foreign_key| foreign_key[:table] == table }
29
43
  end
30
44
 
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class LowData
4
- VERSION = '0.4.0'
4
+ VERSION = '0.4.2'
5
5
  end
data/lib/writer.rb CHANGED
@@ -16,7 +16,7 @@ class LowData
16
16
  raise ConfigError, ConfigError::MISSING_DATABASE if database.nil?
17
17
 
18
18
  @database = database
19
- @schema = Schema.new(database:)
19
+ @schema = Schema.for(database)
20
20
  @filters = FilterCompiler.new(database:, schema: @schema)
21
21
  end
22
22
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lowdata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
@@ -72,6 +72,7 @@ files:
72
72
  - lib/nesting_path.rb
73
73
  - lib/nesting_tree.rb
74
74
  - lib/order_compiler.rb
75
+ - lib/queries.rb
75
76
  - lib/query_compiler.rb
76
77
  - lib/rows.rb
77
78
  - lib/schema.rb
@@ -99,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
100
  - !ruby/object:Gem::Version
100
101
  version: '0'
101
102
  requirements: []
102
- rubygems_version: 4.0.6
103
+ rubygems_version: 4.0.20
103
104
  specification_version: 4
104
105
  summary: The repository pattern with a twist; Data Expressions
105
106
  test_files: []