arable 0.1.5 → 0.2.0

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: e759cee16589d2e4a6938fa4fdd8299e7265212cd9eab8313fd5bd8bf8b22836
4
- data.tar.gz: 20609ecbdca2c5c47b858d7d041a10e51f0c3c6a90fc447e25132e54c419175d
3
+ metadata.gz: 251d0bedc4f15c2f3cb447c5a5edd8c4c08febdfb4e81a22b41fd93e691d17d7
4
+ data.tar.gz: 9a6e43bfc59dd51a7d94425b67d448a3f0c6789728dadd4a2a37bfe1163d10e9
5
5
  SHA512:
6
- metadata.gz: d0d31fceda28bfc71b997b27ef8de06965e4663b0b25e913aecea6467347161540877090f4b54633f7ced28e7110be493c13ba70a30166d452c2d40e80ddaa2a
7
- data.tar.gz: 8a662ef630e48b470c6057575fc83c967aedfaa46544f470eacab9e667d5ab4ce8b778cdc3fccd5f3000fd0a50f4f184be3b260d21b265ec2700b79e66c3ee13
6
+ metadata.gz: 8e78101a34628e9b92c45cb5c93db9b90e9fbd7928fc7ab4d38fbff8cfdfaca04e1507e02640b2280f44c7b62a77ab0c240e9e6331b892122fb4be0c110352cb
7
+ data.tar.gz: bb4e6bfa658771a4a11f6b07cb509cf3c937a799e91f9a503687acfec21029480d7dd1ab5becbf0d163bacf17089db63aa8a249661d033d8b43a3c8ea1242ec4
data/CHANGELOG.md CHANGED
@@ -3,3 +3,8 @@
3
3
  ## [0.1.0] - 2022-05-17
4
4
 
5
5
  - Initial release
6
+
7
+
8
+ ## [0.2.0] - 2022-08-17
9
+
10
+ - Add support for structure.sql
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ arable (0.2.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ parallel (1.22.1)
12
+ parser (3.1.2.0)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.1.1)
15
+ rake (13.0.6)
16
+ regexp_parser (2.4.0)
17
+ rexml (3.2.5)
18
+ rspec (3.11.0)
19
+ rspec-core (~> 3.11.0)
20
+ rspec-expectations (~> 3.11.0)
21
+ rspec-mocks (~> 3.11.0)
22
+ rspec-core (3.11.0)
23
+ rspec-support (~> 3.11.0)
24
+ rspec-expectations (3.11.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.11.0)
27
+ rspec-mocks (3.11.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-support (3.11.0)
31
+ rubocop (1.29.1)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.1.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml (>= 3.2.5, < 4.0)
37
+ rubocop-ast (>= 1.17.0, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.18.0)
41
+ parser (>= 3.1.1.0)
42
+ ruby-progressbar (1.11.0)
43
+ unicode-display_width (2.1.0)
44
+
45
+ PLATFORMS
46
+ x86_64-linux
47
+
48
+ DEPENDENCIES
49
+ arable!
50
+ rake (~> 13.0)
51
+ rspec (~> 3.0)
52
+ rubocop (~> 1.21)
53
+
54
+ BUNDLED WITH
55
+ 2.3.8
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Arable
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/arable`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ The days of writing `.arel_table` are gone! Arable enables you to write cleaner Arel (SQL) queries.
6
4
 
7
5
  ## Installation
8
6
 
@@ -16,17 +14,44 @@ And then execute:
16
14
 
17
15
  $ bundle install
18
16
 
19
- Or install it yourself as:
17
+ Extend `ApplicationRecord`, `ActiveRecord::Base` or your base class that is being used by the models:
18
+
19
+ ```ruby
20
+ class ApplicationRecord < ActiveRecord::Base
21
+ ...
22
+ extend Arable::ActiveRecordExtension
23
+ ...
24
+ end
20
25
 
21
- $ gem install arable
26
+ ```
22
27
 
23
28
  ## Usage
24
29
 
25
- TODO: Write usage instructions here
30
+ If you have a model `User birthday:date`, from now on you can use `User.birthday` directly. This acts as a shorthand for `User.arel_table[:birthday]`:
31
+
32
+ ```ruby
33
+ def birthdays
34
+ User.where(User.birthday.eq(Date.today))
35
+ end
36
+ ```
37
+
38
+ This goes very well together with [arel-extensions gem](https://github.com/Faveod/arel-extensions). If you have both, you can write:
39
+
40
+ ```ruby
41
+ def legal_aged_users
42
+ User.where(User.birthday <= 18.years.ago)
43
+ end
44
+ ```
45
+
46
+ ## Roadmap
47
+
48
+ [x] Support schema.rb
49
+ [x] Support structure.sql
50
+ [] Add tests
26
51
 
27
52
  ## Development
28
53
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests (which are coming soon). You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
55
 
31
56
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
57
 
@@ -0,0 +1,32 @@
1
+ require_relative "../columns"
2
+ require_relative "paths"
3
+
4
+ module Arable::Columns::FromSchema
5
+ module_function
6
+
7
+ def call(table_name)
8
+ return if schema.blank?
9
+
10
+ table_definition_match = schema.match(/create_table "#{table_name}".*?\n(.*?)\n *end/m)
11
+
12
+ columns =
13
+ table_definition_match[1]
14
+ .lines
15
+ .reject { |line| line.include?("t.index") }
16
+ .map { |line| line.match(/t\.[a-z]* "([a-z_]*)"/)[1] }
17
+
18
+ return columns if table_definition_match[0].include?("id: false")
19
+
20
+ ["id"] + columns
21
+ end
22
+
23
+ private
24
+
25
+ def self.schema
26
+ @schema ||= begin
27
+ return unless File.exist?(Arable::Columns::Paths::SCHEMA)
28
+
29
+ File.read(Arable::Columns::Paths::SCHEMA)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ require_relative "../columns"
2
+ require_relative "paths"
3
+
4
+ module Arable::Columns::FromStructure
5
+ module_function
6
+
7
+ def call(table_name)
8
+ return if structure.blank?
9
+
10
+ table_definition_match = structure.match(/CREATE TABLE\s\w+\.#{table_name}.*?\n(.*?)\n *\);/m)
11
+
12
+ table_definition_match[1]
13
+ .lines
14
+ .map(&:strip)
15
+ .map { |line| line.match(/(\w*)\s/)[1] }
16
+ end
17
+
18
+ private
19
+
20
+ def self.structure
21
+ @structure ||= begin
22
+ return unless File.exist?(Arable::Columns::Paths::STRUCTURE)
23
+
24
+ File.read(Arable::Columns::Paths::STRUCTURE)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ require_relative "../columns"
2
+ require_relative "from_schema"
3
+ require_relative "from_structure"
4
+ require_relative "paths"
5
+
6
+ module Arable::Columns::Parser
7
+ WARN_MESSAGE = "No schema definition found, looked in #{Arable::Columns::Paths::ALL.join(', ')}".freeze
8
+
9
+ module_function
10
+
11
+ def call(table_name)
12
+ Arable::Columns::FromSchema.call(table_name) ||
13
+ Arable::Columns::FromStructure.call(table_name) ||
14
+ Rails.logger.warn(WARN_MESSAGE)
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ require_relative "../columns"
2
+
3
+ module Arable::Columns::Paths
4
+ SCHEMA = 'db/schema.rb'.freeze
5
+ STRUCTURE = 'db/structure.sql'.freeze
6
+
7
+ ALL = [
8
+ SCHEMA,
9
+ STRUCTURE,
10
+ ].freeze
11
+ end
@@ -0,0 +1,2 @@
1
+ module Arable::Columns
2
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Arable
4
- VERSION = "0.1.5"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/arable.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "arable/version"
3
+ require_relative 'arable/version'
4
+ require_relative 'arable/columns/parser'
4
5
 
5
6
  module Arable
6
7
  class Error < StandardError; end
@@ -13,29 +14,10 @@ module Arable
13
14
  .reject { |model| model.class_variable_defined?(SKIP_ARABLE_COLUMNS_CLASS_VAR_NAME) }
14
15
  end
15
16
 
16
- def self.column_names_from_schema(table_name)
17
- table_definition_match =
18
- File
19
- .read('db/schema.rb')
20
- .match(/create_table "#{table_name}".*?\n(.*?)\n *end/m)
21
-
22
- columns =
23
- table_definition_match[1]
24
- .lines
25
- .reject { |line| line.include?('t.index') }
26
- .map { |line| line.match(/t\.[a-z]* "([a-z_]*)"/)[1] }
27
-
28
- if table_definition_match[0].include?('id: false')
29
- columns
30
- else
31
- ['id'] + columns
32
- end
33
- end
34
-
35
17
  def self.included(klass)
36
18
  return if klass.class_variable_defined?(SKIP_ARABLE_COLUMNS_CLASS_VAR_NAME)
37
19
 
38
- column_names = column_names_from_schema(klass.table_name).map(&:to_sym)
20
+ column_names = Arable::Columns::Parser.call(klass.table_name).map(&:to_sym)
39
21
  illegal_names = column_names & klass.methods
40
22
 
41
23
  if illegal_names.any?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danielius Visockas
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2022-05-17 00:00:00.000000000 Z
12
+ date: 2022-08-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email:
@@ -23,11 +23,17 @@ files:
23
23
  - ".rubocop.yml"
24
24
  - CHANGELOG.md
25
25
  - Gemfile
26
+ - Gemfile.lock
26
27
  - LICENSE.txt
27
28
  - README.md
28
29
  - Rakefile
29
30
  - arable-0.1.0.gem
30
31
  - lib/arable.rb
32
+ - lib/arable/columns.rb
33
+ - lib/arable/columns/from_schema.rb
34
+ - lib/arable/columns/from_structure.rb
35
+ - lib/arable/columns/parser.rb
36
+ - lib/arable/columns/paths.rb
31
37
  - lib/arable/version.rb
32
38
  - sig/arable.rbs
33
39
  homepage: https://github.com/dvisockas/arable
@@ -37,6 +43,7 @@ metadata:
37
43
  allowed_push_host: https://rubygems.org
38
44
  homepage_uri: https://github.com/dvisockas/arable
39
45
  source_code_uri: https://github.com/dvisockas/arable
46
+ changelog_uri: https://github.com/dvisockas/arable
40
47
  post_install_message:
41
48
  rdoc_options: []
42
49
  require_paths:
@@ -52,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
59
  - !ruby/object:Gem::Version
53
60
  version: '0'
54
61
  requirements: []
55
- rubygems_version: 3.3.7
62
+ rubygems_version: 3.3.20
56
63
  signing_key:
57
64
  specification_version: 4
58
65
  summary: Shorthand syntax to acces your arel tables directly