argem 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/README.md +31 -0
- data/Rakefile +9 -0
- data/lib/Rakefile +5 -0
- data/lib/argem/db_helper.rb +127 -0
- data/lib/argem/version.rb +5 -0
- data/lib/argem.rb +64 -0
- data/sig/argem.rbs +4 -0
- data/tasks/active_record.rake +61 -0
- data/templates/database.yml.sqlite +26 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e1dd6c8446d01ceb471239f39f5819f08c76759a862ec3c49f400d236a12bd14
|
4
|
+
data.tar.gz: 9d8c3996ec8d667e706d9a0570e07edcd70780a100b2abd4f83488a5a554bec7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ab2179f45f099640501ab1a189762f4fe38cb364a1f1668ffac723ab7179a1378215d1a72b7965a7dcfbc628c36ac49eacecdb58c3b669d710204943c32e2ab1
|
7
|
+
data.tar.gz: 116f79ec6a83e515102e5e22ca616ef27119f64b3c3f1a7ff4cd6ea07ce337193204801751cd681ac27624f59c53c8f1773c7d998854cd346d461cfa8c31685d
|
data/.rspec
ADDED
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Argem
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
4
|
+
|
5
|
+
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/argem`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Development
|
24
|
+
|
25
|
+
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.
|
26
|
+
|
27
|
+
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).
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/argem.
|
data/Rakefile
ADDED
data/lib/Rakefile
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
|
2
|
+
require 'yaml'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'active_record'
|
5
|
+
require 'erb'
|
6
|
+
|
7
|
+
module Argem
|
8
|
+
|
9
|
+
# Move ActiveRecord in Rakefile into here
|
10
|
+
# to allow creation of database file (sqlite)
|
11
|
+
# in anywhere
|
12
|
+
class DbHelper
|
13
|
+
include TR::CondUtils
|
14
|
+
|
15
|
+
def self.is_active_record?(obj)
|
16
|
+
case obj
|
17
|
+
when Class
|
18
|
+
obj.ancestors.include?(ActiveRecord::Base)
|
19
|
+
else
|
20
|
+
obj.is_a?(ActiveRecord::Base)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(config)
|
25
|
+
@_config = config
|
26
|
+
# which environment to use?
|
27
|
+
raise Error, "Given config object must repond to method :env" if not @_config.respond_to?(:env)
|
28
|
+
# where to write the migration file to?
|
29
|
+
raise Error, "Given config object must repond to method :migration_root" if not @_config.respond_to?(:migration_root)
|
30
|
+
# where to read/write the database.yml to?
|
31
|
+
raise Error, "Given config object must repond to method :config_root" if not @_config.respond_to?(:config_root)
|
32
|
+
# where to store the database file to ? (sqlite or similar only)
|
33
|
+
raise Error, "Given config object must repond to method :db_root" if not @_config.respond_to?(:db_root)
|
34
|
+
# where to store the generated model class to?
|
35
|
+
raise Error, "Given config object must repond to method :model_root" if not @_config.respond_to?(:model_root)
|
36
|
+
end
|
37
|
+
|
38
|
+
def db_connect
|
39
|
+
ActiveRecord::Base.establish_connection(db_config[@_config.env])
|
40
|
+
end
|
41
|
+
|
42
|
+
def db_create
|
43
|
+
db_connect
|
44
|
+
ActiveRecord::Base.connection.create_database(db_config[@_config.env]["database"]) if ActiveRecord::Base.connection.respond_to?(:create_database)
|
45
|
+
end
|
46
|
+
|
47
|
+
def db_migrate
|
48
|
+
conf = db_config[@_config.env]
|
49
|
+
if (conf["adapter"] =~ /sqlite3/) != nil
|
50
|
+
path = conf["database"]
|
51
|
+
FileUtils.mkdir_p(File.dirname(path)) if not File.exist?(File.dirname(path))
|
52
|
+
end
|
53
|
+
|
54
|
+
db_connect
|
55
|
+
ActiveRecord::MigrationContext.new(local_migration_root).migrate
|
56
|
+
dump_schema
|
57
|
+
end
|
58
|
+
|
59
|
+
def clear
|
60
|
+
ActiveRecord::Base.clear_all_connections!
|
61
|
+
end
|
62
|
+
|
63
|
+
def local_migration_root
|
64
|
+
if @_local_mig_root.nil?
|
65
|
+
#@_local_mig_root = File.join(File.dirname(__FILE__),"..","..","..","..","db","migrate")
|
66
|
+
@_local_mig_root = @_config.migration_root
|
67
|
+
parent = File.dirname(@_local_mig_root)
|
68
|
+
FileUtils.mkdir_p(parent) if not File.exist?(parent)
|
69
|
+
end
|
70
|
+
@_local_mig_root
|
71
|
+
end
|
72
|
+
|
73
|
+
def model_root
|
74
|
+
@_config.model_root
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
def logger
|
79
|
+
Argem.logger(:ar_helpers)
|
80
|
+
end
|
81
|
+
|
82
|
+
def prep_root(root)
|
83
|
+
FileUtils.mkdir_p(root) if not File.exist?(root)
|
84
|
+
end
|
85
|
+
|
86
|
+
def db_config_path
|
87
|
+
#File.join(@_config.config_root,"config","database.yml")
|
88
|
+
File.join(@_config.config_root,"database.yml")
|
89
|
+
end
|
90
|
+
|
91
|
+
def dump_schema
|
92
|
+
ActiveRecord::Base.establish_connection(db_config[@_config.env])
|
93
|
+
require 'active_record/schema_dumper'
|
94
|
+
#filename = File.join(@_config.db_root,"db/schema.rb")
|
95
|
+
filename = File.join(@_config.db_root,"schema.rb")
|
96
|
+
parent = File.dirname(filename)
|
97
|
+
FileUtils.mkdir_p(parent) if not File.exist?(parent)
|
98
|
+
File.open(filename, "w:utf-8") do |file|
|
99
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def db_config(reload = false)
|
104
|
+
|
105
|
+
if @_db_config.nil? or reload
|
106
|
+
|
107
|
+
if not File.exist?(db_config_path)
|
108
|
+
parentFolder = File.dirname(db_config_path)
|
109
|
+
FileUtils.mkdir_p(parentFolder) if not File.exist?(parentFolder)
|
110
|
+
srcTemplate = File.join(File.dirname(__FILE__),"..","..","templates","database.yml.sqlite")
|
111
|
+
srcCont = File.read(srcTemplate)
|
112
|
+
src = ERB.new(srcCont).result_with_hash(db_root: @_config.db_root)
|
113
|
+
File.open(db_config_path, "w") do |f|
|
114
|
+
f.write src
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
@_db_config = YAML.unsafe_load(File.read(db_config_path))
|
119
|
+
end
|
120
|
+
|
121
|
+
@_db_config
|
122
|
+
|
123
|
+
end # db_config
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
data/lib/argem.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'teLogger'
|
4
|
+
require 'toolrack'
|
5
|
+
|
6
|
+
require 'active_record'
|
7
|
+
require 'rake'
|
8
|
+
|
9
|
+
require_relative "argem/version"
|
10
|
+
require_relative 'argem/db_helper'
|
11
|
+
|
12
|
+
module Argem
|
13
|
+
include TR::CondUtils
|
14
|
+
|
15
|
+
class Error < StandardError; end
|
16
|
+
# Your code goes here...
|
17
|
+
|
18
|
+
#def self.env
|
19
|
+
# ENV['ARGEM_ENV'] || "production"
|
20
|
+
#end
|
21
|
+
|
22
|
+
def self.logger=(logger)
|
23
|
+
ActiveRecord::Base.logger = logger
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.logger(tag = nil, &block)
|
27
|
+
if @_logger.nil?
|
28
|
+
if ENV['ARGEM_LOG_TO_STDOUT'] == "true"
|
29
|
+
@_logger = TeLogger::Tlogger.new
|
30
|
+
else
|
31
|
+
@_logger = TeLogger::Tlogger.new("argem.log",5, 5*1024*1024)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
if block
|
36
|
+
if not_empty?(tag)
|
37
|
+
@_logger.with_tag(tag, &block)
|
38
|
+
else
|
39
|
+
@_logger.with_tag(@_logger.tag, &block)
|
40
|
+
end
|
41
|
+
else
|
42
|
+
if is_empty?(tag)
|
43
|
+
@_logger.tag = :argem
|
44
|
+
@_logger
|
45
|
+
else
|
46
|
+
# no block but tag is given? hmm
|
47
|
+
@_logger.tag = tag
|
48
|
+
@_logger
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
# load the rake tasks
|
57
|
+
rf = File.join(File.dirname(__FILE__),"Rakefile")
|
58
|
+
load rf
|
59
|
+
|
60
|
+
class Argem::ApplicationRecord < ActiveRecord::Base
|
61
|
+
primary_abstract_class
|
62
|
+
end
|
63
|
+
|
64
|
+
|
data/sig/argem.rbs
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
|
2
|
+
require 'argem'
|
3
|
+
require 'tty/prompt'
|
4
|
+
|
5
|
+
namespace :argem do
|
6
|
+
|
7
|
+
namespace :db do
|
8
|
+
|
9
|
+
desc "Migrate database"
|
10
|
+
task :migrate do
|
11
|
+
helper = Argem::DbHelper.new(ARConfig)
|
12
|
+
helper.db_migrate
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Generate migration"
|
16
|
+
task :migration do
|
17
|
+
helper = Argem::DbHelper.new(ARConfig)
|
18
|
+
name = ARGV[1] || raise("Specify name: rake g:migration your_migration")
|
19
|
+
timestamp = Time.now.strftime("%Y%m%d%H%M%S")
|
20
|
+
path = File.expand_path(File.join(helper.local_migration_root,"#{timestamp}_create_#{name.pluralize}.rb"), __FILE__)
|
21
|
+
parent = File.dirname(path)
|
22
|
+
FileUtils.mkdir_p(parent) if not File.exist?(parent)
|
23
|
+
migration_class = name.split("_").map(&:capitalize).join
|
24
|
+
|
25
|
+
File.open(path, 'w') do |file|
|
26
|
+
file.write <<-EOF
|
27
|
+
class Create#{migration_class.pluralize} < ActiveRecord::Migration[#{ActiveRecord.version.segments[0..1].join('.')}]
|
28
|
+
def change
|
29
|
+
create_table :#{name.pluralize} do |t|
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
EOF
|
34
|
+
end
|
35
|
+
|
36
|
+
#mdlPath = File.expand_path(File.join(File.dirname(__FILE__),"lib","banis","cli","database","model","ar","#{name}.rb"))
|
37
|
+
skip = false
|
38
|
+
mdlPath = File.join(helper.model_root, "#{name}.rb")
|
39
|
+
if File.exist?(mdlPath)
|
40
|
+
pmt = TTY::Prompt.new
|
41
|
+
skip = pmt.no?("The model file '#{name}.rb' already exist. Overwrite? ")
|
42
|
+
end
|
43
|
+
|
44
|
+
if not skip
|
45
|
+
parent = File.dirname(mdlPath)
|
46
|
+
FileUtils.mkdir_p(parent) if not File.exist?(parent)
|
47
|
+
File.open(mdlPath,"w") do |f|
|
48
|
+
f.write <<-EOF
|
49
|
+
class #{migration_class} < Argem::ApplicationRecord
|
50
|
+
self.table_name = "#{name.pluralize}"
|
51
|
+
end
|
52
|
+
EOF
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
puts "Migration #{path} created"
|
57
|
+
abort # needed stop other tasks
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# SQLite. Versions 3.8.0 and up are supported.
|
2
|
+
# gem install sqlite3
|
3
|
+
#
|
4
|
+
# Ensure the SQLite 3 gem is defined in your Gemfile
|
5
|
+
# gem "sqlite3"
|
6
|
+
#
|
7
|
+
default: &default
|
8
|
+
adapter: sqlite3
|
9
|
+
#pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
|
10
|
+
pool : 5
|
11
|
+
timeout: 5000
|
12
|
+
|
13
|
+
development:
|
14
|
+
<<: *default
|
15
|
+
database: <%= db_root %>/development.sqlite3
|
16
|
+
|
17
|
+
# Warning: The database defined as "test" will be erased and
|
18
|
+
# re-generated from your development database when you run "rake".
|
19
|
+
# Do not set this db to the same as development or production.
|
20
|
+
test:
|
21
|
+
<<: *default
|
22
|
+
database: <%= db_root %>/test.sqlite3
|
23
|
+
|
24
|
+
production:
|
25
|
+
<<: *default
|
26
|
+
database: <%= db_root %>/production.sqlite3
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: argem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-10-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: teLogger
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: toolrack
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: tty-prompt
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: release-gem
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.3.3
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.3.3
|
97
|
+
description: ''
|
98
|
+
email:
|
99
|
+
- chris@antrapol.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".rspec"
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/Rakefile
|
108
|
+
- lib/argem.rb
|
109
|
+
- lib/argem/db_helper.rb
|
110
|
+
- lib/argem/version.rb
|
111
|
+
- sig/argem.rbs
|
112
|
+
- tasks/active_record.rake
|
113
|
+
- templates/database.yml.sqlite
|
114
|
+
homepage: ''
|
115
|
+
licenses: []
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 2.6.0
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubygems_version: 3.4.6
|
133
|
+
signing_key:
|
134
|
+
specification_version: 4
|
135
|
+
summary: Package ActiveRecord suitable to include in other gem
|
136
|
+
test_files: []
|