lowdata 0.3.0 → 0.4.1

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: 00edcbe16f363ff5571130711b163c2b7a30f269b063237d9513f0d0d65b1ac9
4
- data.tar.gz: b76da81116417a88cb6d440acfcd676aee2f9f53726c8b079551e4ab6df5279f
3
+ metadata.gz: 6470fa017fbce0db1ecccb41d8e1c63498c915f1a4081cea06ec13c0875acfcf
4
+ data.tar.gz: 63fdea0e684086cc58e4c4663843f4480e8bf24dba5447c4dbb8f2251886510a
5
5
  SHA512:
6
- metadata.gz: 76724feffd8a520b11e60bd387ebf1a3aa7543fc7bb962af8b4b5db4411f9099fa42cef7207c98592cf6687a393076b51f3ffad45b198e32b885f8636accac93
7
- data.tar.gz: f555c5e21c68df568a8e02e511f9fd6ee8ced5ca9a97edc799eb95415e96fb56c38b2711eb161546fe7c00b775905c1418127b146caddb474a96c9ede039062b
6
+ metadata.gz: 1b8c2f13b64730a0a43f7321beea95558dd16fbc2e56c99e7999a39f03f099201124fa2aa5f055d0761e43cc3f2ca611ccd91513c384d923dcb303289435888f
7
+ data.tar.gz: 2a7942d5ab17e81d0ed6bc7fffc3ad60511cdffef63ab2248dd21ee86b642c430d034d5eb9c3447be52c3f5594279b99a11b9a71844a9572ff8d2d22ac2ab75a
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
@@ -44,6 +45,37 @@ class LowData
44
45
  @table ||= name && table_name(name.split('::').last.delete_suffix('Data'))
45
46
  end
46
47
 
48
+ # The Method Factory pattern, as in LowNode: each public instance method gets a class method of the same name, so
49
+ # `UsersData.update(1, attributes)` calls `UsersData.new.update(1, attributes)`. Defined when the instance method
50
+ # is, so it's a real method rather than a method_missing lookup. Existing class methods, like `table`, are kept.
51
+ def method_added(name)
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)
66
+ return if self == LowData || !public_method_defined?(name)
67
+ return unless name.match?(/\A[a-z_]\w*[?!]?\z/) # Setters like `title=` stay instance methods.
68
+ return if factory_method?(name) # A parent's class method already calls `new` on this class.
69
+ return warn(class_method_exists_message(name:)) if respond_to?(name, true)
70
+
71
+ (@factory_methods ||= []) << name
72
+ # public_send because `private def secret` makes the method private only after it's added.
73
+ singleton_class.class_eval <<~RUBY, __FILE__, __LINE__ + 1
74
+ def #{name}(...) = new.public_send(:#{name}, ...) # def update(...) = new.public_send(:update, ...)
75
+ RUBY
76
+ end
77
+ private :define_factory_method
78
+
47
79
  # Resolve undefined constants in a repository to tables, so `Users` and `BlogPosts` become :users and :blog_posts.
48
80
  # `Table` is the repository's own table, joined to whichever related tables own the columns asked for.
49
81
  def const_missing(name)
@@ -64,6 +96,14 @@ class LowData
64
96
 
65
97
  private
66
98
 
99
+ def factory_method?(name)
100
+ self < LowData && (@factory_methods&.include?(name) || superclass.send(:factory_method?, name))
101
+ end
102
+
103
+ def class_method_exists_message(name:)
104
+ "#{self}##{name} has no class method, because #{self}.#{name} exists. Call #{self}.new.#{name}"
105
+ end
106
+
67
107
  def inverted_table
68
108
  raise ConfigError, "Set the table for #{self}, e.g. self.table = :posts" if table.nil?
69
109
 
data/lib/nesting_path.rb CHANGED
@@ -10,6 +10,8 @@ class LowData
10
10
  class NestingPath
11
11
  ARRAY_END_MESSAGE = 'An array ends a + chain, because it nests several tables. Put the rest of the chain in the ' \
12
12
  'array, e.g. Users + [Posts + Comments, Orders]'
13
+ AFTER_ARRAY_MESSAGE = '%<child>s comes after an array, so it has no single table to nest in. Put it in the array: ' \
14
+ '%<beside>s nests %<child>s in %<parent>s, and %<in_last>s nests it in %<last>s'
13
15
 
14
16
  # A node waiting for its children to be linked, and the entries from it down to the end of its chain.
15
17
  Entry = Struct.new(:node, :children, :tail, :closed) do
@@ -43,16 +45,19 @@ class LowData
43
45
 
44
46
  # Nests a segment and returns the path to the end of the chain.
45
47
  def attach(path:, segment:)
46
- raise JoinError, ARRAY_END_MESSAGE if path.last.closed
47
48
  return siblings(path:, nodes: segment.nodes) if segment.is_a?(DataExpression::Siblings)
48
49
 
49
50
  child = @tree.entry(node: segment)
51
+ raise JoinError, after_array_message(array: path.last, child:) if path.last.closed
52
+
50
53
  parent = parent(path:, child:)
51
54
  parent.children << child
52
55
  path[0..path.index { |entry| entry.equal?(parent) }] + child.path
53
56
  end
54
57
 
55
58
  def siblings(path:, nodes:)
59
+ raise JoinError, ARRAY_END_MESSAGE if path.last.closed
60
+
56
61
  nodes.each { |node| path.last.children << @tree.entry(node:) }
57
62
  path.last.closed = true
58
63
  path
@@ -94,6 +99,26 @@ class LowData
94
99
  entry.tables.product(other.tables).any? { |table, other_table| @schema.linked?(table, other_table) }
95
100
  end
96
101
 
102
+ # Suggests the two places in the array a table after it can go: beside its elements, or in its last one.
103
+ def after_array_message(array:, child:)
104
+ *elements, last = array.children
105
+ beside = [*elements, last, child].map { |entry| chained(entry:) }
106
+ in_last = [*elements.map { |entry| chained(entry:) }, "#{chained(entry: last)} + #{chained(entry: child)}"]
107
+ format(AFTER_ARRAY_MESSAGE, child: child.tables.first, parent: array.tables.last, last: last.path.last.tables.last,
108
+ beside: in_array(array:, elements: beside), in_last: in_array(array:, elements: in_last))
109
+ end
110
+
111
+ def in_array(array:, elements:) = "#{written(entry: array)} + [#{elements.join(', ')}]"
112
+
113
+ # An array element as written, with its whole chain: `Posts + Comments`.
114
+ def chained(entry:) = entry.path.map { |link| written(entry: link) }.join(' + ')
115
+
116
+ # A table as written in an expression, without its brackets: `Posts`, or `(Posts & Likes)` for a join.
117
+ def written(entry:)
118
+ names = entry.tables.map { |table| LowData.constant_name(table) }
119
+ names.one? ? names.first : "(#{names.join(' & ')})"
120
+ end
121
+
97
122
  # Suggests an array, or naming the foreign key that reaches back to the earlier table.
98
123
  def unlinked_message(child:, parent:, earlier:)
99
124
  message = "No foreign key links #{child} to #{parent}"
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
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class LowData
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.1'
5
5
  end
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.3.0
4
+ version: 0.4.1
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: []