commento 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f2558be497fdf71eca0c553371dc0fb09fdb348a04bc930289ad3696a571a4fa
4
+ data.tar.gz: c4e41098d7c6ffcff077d5c18545e7c0d03fcb33b3b1273bb4cfac0406cabf38
5
+ SHA512:
6
+ metadata.gz: f93c37134940512adbe7fd03600d7aeb0f4af032b9196c55bc5d011a310138c5f2fbefc4b8962abd9aa7cbc88ab41156cff8121eb80142df91e057146c60f779
7
+ data.tar.gz: c66a4d0f5e6a626982a3842d743b712fe7023fd3303369426a9f911d4b4c3fbb7ebd00712b02de70f192857b4aa5adb0573bf7a782f4aa054ce3946c90e33da6
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Commento
2
+
3
+ Commento provides DSL for Ruby on Rails application for working with database comments.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+ ```ruby
9
+ gem 'commento'
10
+ ```
11
+
12
+ And then execute:
13
+ ```bash
14
+ $ bundle install
15
+ ```
16
+
17
+ ## Gem configuration
18
+
19
+ ### Initializer
20
+
21
+ Add configuration line to config/initializers/commento.rb:
22
+
23
+ #### ActiveRecord
24
+
25
+ ```ruby
26
+ require 'commento/adapters/active_record'
27
+
28
+ Commento.configure do |config|
29
+ config.adapter = Commento::Adapters::ActiveRecord.new
30
+ end
31
+ ```
32
+
33
+ ### Models
34
+
35
+ Update you application model
36
+ ```ruby
37
+ class ApplicationRecord < ActiveRecord::Base
38
+ include Commento::Helpers
39
+ end
40
+ ```
41
+
42
+ ## Usage
43
+
44
+ Commento provides helpers for models for setting and getting table's column comments.
45
+
46
+ ### Set table's column comment
47
+
48
+ ```ruby
49
+ User.set_column_comment(:email, 'Required field for user authentication')
50
+ ```
51
+
52
+ or reset comment by skiping value
53
+ ```ruby
54
+ User.set_column_comment(:email)
55
+ ```
56
+
57
+ ### Read table's column comment
58
+
59
+ ```ruby
60
+ User.get_column_comment(:email)
61
+ ```
62
+
63
+ ## License
64
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+
5
+ module Commento
6
+ module Adapters
7
+ class ActiveRecord
8
+ # Public: The name of the adapter.
9
+ attr_reader :name
10
+
11
+ # Public: Initialize a new ActiveRecord adapter instance.
12
+ #
13
+ # name - The Symbol name for this adapter. Optional (default :active_record)
14
+ def initialize(options={})
15
+ @name = options.fetch(:name, :active_record)
16
+ end
17
+
18
+ # Public: Sets comment for table's column.
19
+ def set_column_comment(table_name, column_name, value)
20
+ sql = "COMMENT ON COLUMN #{table_name}.#{column_name} IS "
21
+ sql += value ? "'#{value}'" : 'NULL'
22
+ execute(sql)
23
+ end
24
+
25
+ # Public: Returns comment for table's column.
26
+ def get_column_comment(table_name, column_name)
27
+ sql = <<-SQL.squish
28
+ SELECT
29
+ cols.column_name,
30
+ pg_catalog.col_description(c.oid, cols.ordinal_position::int)
31
+ FROM pg_catalog.pg_class c, information_schema.columns cols
32
+ WHERE
33
+ cols.table_catalog = '#{Rails.configuration.database_configuration[Rails.env]["database"]}' AND
34
+ cols.table_schema = 'public' AND
35
+ cols.table_name = '#{table_name}' AND
36
+ cols.table_name = c.relname
37
+ SQL
38
+ column_data = execute(sql).to_a.find { |column| column['column_name'].to_sym == column_name }
39
+ column_data.presence ? column_data['col_description'] : nil
40
+ end
41
+
42
+ private
43
+
44
+ def execute(sql)
45
+ ::ActiveRecord::Base.connection.execute(sql)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Commento
4
+ class Configuration
5
+ attr_accessor :adapter
6
+
7
+ def initialize
8
+ @adapter = nil
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Commento
4
+ class DSL
5
+ attr_reader :adapter
6
+
7
+ # Public: Returns a new instance of the DSL.
8
+ #
9
+ # adapter - The adapter that this DSL instance should use.
10
+ def initialize(adapter)
11
+ @adapter = adapter
12
+ end
13
+
14
+ # Public: Sets comment for table's column.
15
+ def set_column_comment(table_name, column_name, value)
16
+ adapter.set_column_comment(table_name, column_name, value)
17
+ end
18
+
19
+ # Public: Returns comment for table's column.
20
+ def get_column_comment(table_name, column_name)
21
+ adapter.get_column_comment(table_name, column_name)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Commento
4
+ module Helpers
5
+ def self.included(base)
6
+ base.class_eval do
7
+ extend ClassMethods
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def set_column_comment(column_name, value=nil)
13
+ instance.set_column_comment(table_name, column_name, value)
14
+ end
15
+
16
+ def get_column_comment(column_name)
17
+ instance.get_column_comment(table_name, column_name)
18
+ end
19
+
20
+ def instance
21
+ Commento.instance
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Commento
4
+ VERSION = '0.1.0'
5
+ end
data/lib/commento.rb ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ require 'commento/version'
6
+ require 'commento/configuration'
7
+ require 'commento/dsl'
8
+ require 'commento/helpers'
9
+
10
+ module Commento
11
+ extend self
12
+ extend Forwardable
13
+
14
+ # Public: Given an adapter returns a handy DSL to all the commentp goodness.
15
+ def new(adapter)
16
+ DSL.new(adapter)
17
+ end
18
+
19
+ # Public: Configure commentp.
20
+ #
21
+ # Commento.configure do |config|
22
+ # config.adapter = Commento::Adapters::ActiveRecord.new
23
+ # end
24
+ #
25
+ def configure
26
+ yield configuration
27
+ end
28
+
29
+ # Public: Returns Commento::Configuration instance.
30
+ def configuration
31
+ @configuration ||= Configuration.new
32
+ end
33
+
34
+ # Public: Default per thread commentp instance if configured.
35
+ # Returns Commento::DSL instance.
36
+ def instance
37
+ Thread.current[:commento_instance] ||= new(configuration.adapter)
38
+ end
39
+
40
+ # Public: All the methods delegated to instance. These should match the interface of Commento::DSL.
41
+ def_delegators :instance, :adapter
42
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commento
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bogdanov Anton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.39'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.39'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop-performance
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ description: Commento allows to work with database comments.
70
+ email:
71
+ - kortirso@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - README.md
77
+ - Rakefile
78
+ - lib/commento.rb
79
+ - lib/commento/adapters/active_record.rb
80
+ - lib/commento/configuration.rb
81
+ - lib/commento/dsl.rb
82
+ - lib/commento/helpers.rb
83
+ - lib/commento/version.rb
84
+ homepage: https://github.com/kortirso/commento
85
+ licenses:
86
+ - MIT
87
+ metadata:
88
+ homepage_uri: https://github.com/kortirso/commento
89
+ source_code_uri: https://github.com/kortirso/commento
90
+ changelog_uri: https://github.com/kortirso/commento/blob/master/CHANGELOG.md
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 2.7.0
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubygems_version: 3.4.1
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Work with database comments.
110
+ test_files: []