aro 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8413e2488b187dba53691f3d1429443644c045b821942a1597a7326aa04cf793
4
+ data.tar.gz: 922193d29d9bb1312e549177bbf8b94cee5a3248b42c51d48a5b23352371ac1c
5
+ SHA512:
6
+ metadata.gz: 6488ba77f14f0d5ff7624da6d288a3c67efec639193ec9059788dfac9d232bca1ad2e9909971719af32bc194a980133b8cfa3a57fb261d7c83701832638a01ee
7
+ data.tar.gz: acbe98b3982fb6018a3e70381d9588ca89ace357c9ca4cd08d685ba99864fdc050b7f3101dec8770a05f007c38ce689dbe5a67b4a9fdf170e2370b8985c86463
data/bin/aro ADDED
@@ -0,0 +1,74 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ =begin
4
+
5
+ aro
6
+
7
+ entry point for the aro cli
8
+
9
+ by i2097i
10
+
11
+ =end
12
+
13
+ require :aro.to_s
14
+ [
15
+ :cli,
16
+ ].each do |dir|
17
+ Dir[
18
+ File.join(
19
+ __dir__,
20
+ dir.to_s,
21
+ :"**/*.rb".to_s
22
+ )
23
+ ].each { |file| require file}
24
+ end
25
+
26
+ # set environment variable
27
+ ENV[:ARO_ENV.to_s] = :production.to_s
28
+ # ENV[:ARO_ENV.to_s] = :development.to_s
29
+
30
+ # todo: add tab completion using abbrev gem
31
+ # https://stackoverflow.com/a/11627931
32
+
33
+ # todo: response formats
34
+ # text (default)
35
+ # json
36
+ #
37
+
38
+ module CLI
39
+
40
+ if CLI::LOAD_DECK_ACTIONS.keys.map{|k| k.downcase.to_sym}.include?(ARGV[0]&.to_sym)
41
+ # enable deck shortcut (skip typing deck while in-game)
42
+ ARGV0 = :deck
43
+ ARGV1 = ARGV[0]&.to_sym
44
+ ARGV2 = ARGV[1]&.to_sym
45
+ else
46
+ # default
47
+ ARGV0 = ARGV[0]&.to_sym
48
+ ARGV1 = ARGV[1]&.to_sym
49
+ ARGV2 = ARGV[2]&.to_sym
50
+ end
51
+
52
+ if CLI::FLAGS[:VERSION].include?(CLI::ARGV0)
53
+ Aro::P.p.say(Aro::VERSION)
54
+ exit(CLI::EXIT_CODES[:SUCCESS])
55
+ end
56
+
57
+ # cli parser
58
+ case CLI::ARGV0
59
+ when :create
60
+ CLI::create
61
+ when :deck
62
+ CLI::deck
63
+ else
64
+ unless Aro::Mancy.game.nil? || CLI::FLAGS[:HELP].include?(CLI::ARGV0)
65
+ Aro::P.p.say(I18n.t("cli.messages.welcome", name: "#{Aro::Mancy.name}.game.show"))
66
+ Aro::Mancy.game.show
67
+ exit(CLI::EXIT_CODES[:SUCCESS])
68
+ end
69
+
70
+ CLI::usage
71
+ exit(CLI::EXIT_CODES[:SUCCESS])
72
+ end
73
+
74
+ end
@@ -0,0 +1,37 @@
1
+ =begin
2
+
3
+ constants.rb
4
+
5
+ define constants for the aro cli.
6
+
7
+ by i2097i
8
+
9
+ =end
10
+
11
+ module CLI
12
+
13
+ FLAGS = {
14
+ HELP: [:"-h", :"--help"],
15
+ VERSION: [:"-v", :"--version"],
16
+ }
17
+
18
+ EXIT_CODES = {
19
+ SUCCESS: 0,
20
+ GENERAL_ERROR: 1,
21
+ INVALID_ARG: 3
22
+ }
23
+
24
+ CREATE_DECK_ACTIONS = {
25
+ CREATE: :CREATE
26
+ }
27
+
28
+ LOAD_DECK_ACTIONS = {
29
+ DRAW: :DRAW,
30
+ EXPLORE: :EXPLORE,
31
+ REPLACE: :REPLACE,
32
+ RESET: :RESET,
33
+ SHOW: :SHOW,
34
+ SHUFFLE: :SHUFFLE,
35
+ }
36
+
37
+ end
data/bin/cli/create.rb ADDED
@@ -0,0 +1,17 @@
1
+ =begin
2
+
3
+ constants.rb
4
+
5
+ process aro creation commands
6
+
7
+ by i2097i
8
+
9
+ =end
10
+
11
+ module CLI
12
+ def self.create
13
+ name = CLI::ARGV1 || I18n.t("cli.messages.invalid_name")
14
+ Aro::P.p.say(I18n.t("cli.messages.creation_attempt", name: name))
15
+ Aro::Create.new(CLI::ARGV1)
16
+ end
17
+ end
data/bin/cli/deck.rb ADDED
@@ -0,0 +1,79 @@
1
+ =begin
2
+
3
+ deck.rb
4
+
5
+ process deck commands.
6
+
7
+ by i2097i
8
+
9
+ =end
10
+
11
+ module CLI
12
+ def self.deck
13
+ action = CLI::ARGV1&.upcase&.to_sym
14
+
15
+ if CLI::FLAGS[:HELP].include?(CLI::ARGV1)
16
+ CLI::usage
17
+ exit(CLI::EXIT_CODES[:SUCCESS])
18
+ elsif CLI::ARGV1.nil?
19
+ # no args, open deck menu
20
+ Aro::Create.new(Aro::Database.get_name_from_namefile)
21
+ Aro::Deck.display_selection_menu
22
+ elsif action == CLI::CREATE_DECK_ACTIONS[:CREATE]
23
+ Aro::Mancy.exit_error_missing_args! if CLI::ARGV2.nil?
24
+ deck = Aro::Deck.make(CLI::ARGV1)
25
+ Aro::P.p.say(I18n.t("cli.messages.deck_created_sucessfully", name: deck.name))
26
+ Aro::Deck.display_selection_menu
27
+ elsif CLI::LOAD_DECK_ACTIONS.include?(action)
28
+ deck = CLI::get_deck
29
+
30
+ if action == CLI::LOAD_DECK_ACTIONS[:EXPLORE]
31
+ deck.explore
32
+ else
33
+ # assume shuffle or show, in which case show is always called
34
+ if action == CLI::LOAD_DECK_ACTIONS[:SHUFFLE]
35
+ Aro::P.p.say(I18n.t("cli.messages.shuffling", name: deck.name))
36
+ deck.shuffle
37
+ elsif action == CLI::LOAD_DECK_ACTIONS[:DRAW]
38
+ Aro::P.p.say(I18n.t("cli.messages.drawing", name: deck.name))
39
+ deck.draw
40
+ elsif action == CLI::LOAD_DECK_ACTIONS[:REPLACE]
41
+ Aro::P.p.say(I18n.t("cli.messages.replacing_drawn", name: deck.name))
42
+ deck.replace
43
+ elsif action == CLI::LOAD_DECK_ACTIONS[:RESET]
44
+ if Aro::AROYES != Aro::P.p.ask(I18n.t("cli.messages.confirmation_prompt", name: deck.name))
45
+ Aro::P.p.say(I18n.t("cli.messages.understood", name: deck.name))
46
+ exit(CLI::EXIT_CODES[:SUCCESS])
47
+ end
48
+
49
+ Aro::P.p.say(I18n.t("cli.messages.resetting", name: deck.name))
50
+ deck.reset
51
+ end
52
+
53
+ Aro::P.p.say(I18n.t("cli.messages.showing", name: deck.name))
54
+ deck.show
55
+ end
56
+ else
57
+ CLI::usage
58
+ end
59
+ end
60
+
61
+ def self.get_deck
62
+ Aro::Create.new(Aro::Database.get_name_from_namefile)
63
+ deck = nil
64
+ if CLI::ARGV2.nil?
65
+ # assume current deck
66
+ deck = Aro::Deck.current_deck
67
+ else
68
+ deck = Aro::Deck.find_by(name: CLI::ARGV2)
69
+ deck = Aro::Deck.find_by(id: CLI::ARGV2) if deck.nil?
70
+ end
71
+
72
+ if deck.nil?
73
+ Aro::P.p.say(I18n.t("cli.errors.missing_deck", cmd: "#{ARGV0} #{ARGV1} #{ARGV2}"))
74
+ exit(CLI::EXIT_CODES[:GENERAL_ERROR])
75
+ end
76
+
77
+ deck
78
+ end
79
+ end
data/bin/cli/usage.rb ADDED
@@ -0,0 +1,15 @@
1
+ =begin
2
+
3
+ usage.rb
4
+
5
+ display aro usage.
6
+
7
+ by i2097i
8
+
9
+ =end
10
+
11
+ module CLI
12
+ def self.usage
13
+ Aro::P.less(I18n.t("cli.usage"))
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ class CreateDecks < ActiveRecord::Migration[8.1]
2
+
3
+ def self.up
4
+ create_table :decks do |t|
5
+ t.string :name
6
+ t.string :cards
7
+ t.string :drawn
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :decks
15
+ end
16
+
17
+ end
@@ -0,0 +1,19 @@
1
+ class CreateLogs < ActiveRecord::Migration[8.1]
2
+
3
+ def self.up
4
+ create_table :logs do |t|
5
+ t.string :card_data
6
+ t.string :drawn_data
7
+
8
+ t.timestamps
9
+ end
10
+
11
+ add_column :logs, :deck_id, :integer
12
+ add_index :logs, :deck_id
13
+ end
14
+
15
+ def self.down
16
+ drop_table :logs
17
+ end
18
+
19
+ end
data/lib/aro/c.rb ADDED
@@ -0,0 +1,32 @@
1
+ module Aro
2
+ DIRS = {
3
+ ARO: Proc.new{Aro::IS_TEST.call ? ".aro_test" : ".aro"},
4
+ }
5
+
6
+ AROYES = "aroyes"
7
+
8
+ NUMERALS = {
9
+ O: 0,
10
+ I: 1,
11
+ II: 2,
12
+ III: 3,
13
+ IV: 4,
14
+ V: 5,
15
+ VI: 6,
16
+ VII: 7,
17
+ VIII: 8,
18
+ IX: 9,
19
+ X: 10,
20
+ XI: 11,
21
+ XII: 12,
22
+ XIII: 13,
23
+ XIV: 14,
24
+ XV: 15,
25
+ XVI: 16,
26
+ XVII: 17,
27
+ XVIII:18,
28
+ XIX: 19,
29
+ XX: 20,
30
+ XXI: 21,
31
+ }
32
+ end
data/lib/aro/create.rb ADDED
@@ -0,0 +1,30 @@
1
+ module Aro
2
+ class Create
3
+ def initialize(name = nil)
4
+ name = name&.strip
5
+
6
+ error_msg = nil
7
+
8
+ # is a non-empty string
9
+ error_msg = I18n.t("cli.errors.missing_args", cmd: "aro create") if name.nil? || !name.kind_of?(String) || name.empty?
10
+
11
+ # display error and abort
12
+ unless error_msg.nil?
13
+ Aro::P.p.say(error_msg)
14
+ Aro::Mancy.exit_error_missing_args!
15
+ end
16
+
17
+ # create the new aro directory and database
18
+ if Aro::Database.get_name_from_namefile.nil? && !Dir.exist?(name)
19
+ Aro::P.p.say(I18n.t("cli.messages.no_decks"))
20
+ create_cmd = "mkdir #{name}"
21
+ Aro::P.p.say(create_cmd)
22
+ system(create_cmd)
23
+ end
24
+
25
+ # create database
26
+ Aro::Database.new(name)
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,113 @@
1
+ require :active_record.to_s
2
+ require :base64.to_s
3
+ require :yaml.to_s
4
+
5
+ module Aro
6
+ class Database
7
+ CONFIG_FILE = "database.yml"
8
+ SQL_FILE = "database.sql"
9
+ SCHEMA_FILE = "schema.rb"
10
+ MIGRATIONS_DIR = "db/migrate"
11
+ NAME_FILE = ".name"
12
+
13
+ def initialize(name = nil)
14
+ # show queries in stout
15
+ ActiveRecord::Base.logger = Logger.new(STDOUT) if ENV[:ARO_ENV.to_s] == :development.to_s
16
+
17
+ # generate .name file
18
+ if name.nil? && Aro::Database.is_aro_dir?
19
+ # pwd is in aro directory, use name file
20
+ name = get_name_from_namefile
21
+ elsif !name.nil? && !Aro::Database.is_aro_dir?
22
+ # first use, pwd is not in aro directory yet
23
+ echo_cmd = "echo #{name} >> #{name}/#{NAME_FILE}"
24
+ Aro::P.p.say(echo_cmd)
25
+ system(echo_cmd)
26
+ end
27
+
28
+ if name.nil?
29
+ # if name is still nil, need to use create to generate aro directory
30
+ raise "invalid aro directory. use `aro create` to generate one"
31
+ end
32
+
33
+ setup_local_aro(name)
34
+ end
35
+
36
+ def connect(name)
37
+ ActiveRecord::Base.establish_connection(config(name))
38
+ end
39
+
40
+ def config(name = nil)
41
+ @config ||= YAML.load_file(db_config_filepath(name))
42
+ end
43
+
44
+ def base_aro_dir(name)
45
+ "#{Aro::Database.is_aro_dir? ? "." : name}/#{Aro::DIRS[:ARO].call}"
46
+ end
47
+
48
+ def db_config_filepath(name)
49
+ "#{base_aro_dir(name)}/#{CONFIG_FILE}"
50
+ end
51
+
52
+ def db_filepath(name)
53
+ "#{base_aro_dir(name)}/#{SQL_FILE}"
54
+ end
55
+
56
+ def self.is_aro_dir?
57
+ File.exist?(NAME_FILE)
58
+ end
59
+
60
+ def self.get_name_from_namefile
61
+ Aro::Database.is_aro_dir? ? File.read(NAME_FILE).strip : nil
62
+ end
63
+
64
+ def setup_local_aro(name = nil, force = false)
65
+ # create local .aro/ directory
66
+ unless File.exist?(base_aro_dir(name)) || force
67
+ if File.exist?(base_aro_dir(name)) && force
68
+ rm_cmd = "rm -rf #{base_aro_dir(name)}"
69
+ Aro::P.p.say(rm_cmd)
70
+ system(rm_cmd)
71
+ end
72
+
73
+ mk_cmd = "mkdir #{base_aro_dir(name)}"
74
+ Aro::P.p.say(mk_cmd)
75
+ system(mk_cmd)
76
+ end
77
+
78
+ # create database config yaml file
79
+ c = {
80
+ adapter: :sqlite3.to_s,
81
+ database: "#{Aro::Database.is_aro_dir? ? "." : name}/#{Aro::DIRS[:ARO].call}/#{SQL_FILE}",
82
+ username: name,
83
+ password: name
84
+ }.to_yaml
85
+ File.open(db_config_filepath(name), "w") do |file|
86
+ file.write(c)
87
+ end
88
+
89
+ connect(name)
90
+ setup(name)
91
+ end
92
+
93
+ def setup(name)
94
+ local_migrate_dir = "#{base_aro_dir(name)}/#{MIGRATIONS_DIR}"
95
+ unless Dir.exist?(local_migrate_dir)
96
+ gem_dir = Dir[Gem.loaded_specs[:aro.to_s]&.full_gem_path || '.'].first
97
+ cp_cmd = "cp -R #{gem_dir}/db #{base_aro_dir(name)}"
98
+ Aro::P.p.say(cp_cmd)
99
+ system(cp_cmd)
100
+ end
101
+
102
+ migration_version = Dir["#{local_migrate_dir}/*.rb"].map{|n|
103
+ Pathname.new(n).basename.to_s.split("_")[0].to_i
104
+ }.max
105
+ ActiveRecord::MigrationContext.new(local_migrate_dir).migrate(migration_version)
106
+ require 'active_record/schema_dumper'
107
+ filename = "#{base_aro_dir(name)}/#{SCHEMA_FILE}"
108
+ File.open(filename, "w+") do |f|
109
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection_pool, f)
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,3 @@
1
+ module Aro
2
+ IS_TEST = Proc.new{ENV[:ARO_ENV.to_s] == :test.to_s}
3
+ end
data/lib/aro/i18n.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require :i18n.to_s
4
+
5
+ module Aro
6
+ # ...
7
+ end
8
+
9
+ locale_path = Gem.loaded_specs[:aro.to_s]&.full_gem_path
10
+ if Aro::IS_TEST.call
11
+ locale_path = Dir.pwd
12
+ end
13
+ I18n.load_path += Dir["#{locale_path}/locale/*.yml"]
14
+ I18n.default_locale = :en
data/lib/aro/prompt.rb ADDED
@@ -0,0 +1,21 @@
1
+ require :"tty-prompt".to_s
2
+
3
+ module Aro
4
+ class P
5
+ include Singleton
6
+
7
+ attr_accessor :prompt
8
+
9
+ def initialize
10
+ self.prompt = TTY::Prompt.new
11
+ end
12
+
13
+ def self.p
14
+ P.instance.prompt
15
+ end
16
+
17
+ def self.less(display_text = "")
18
+ IO.popen("less -X", "w") { |f| f.puts display_text }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ module Aro
2
+ VERSION = :"0.1.0"
3
+ RELEASE_NOTES = :"initial gem release."
4
+ end
data/lib/aro.rb ADDED
@@ -0,0 +1,39 @@
1
+ # require aro directories
2
+ [
3
+ :aro,
4
+ :models,
5
+ ].each{|dir|
6
+ Dir[File.join(
7
+ __dir__,
8
+ dir.to_s,
9
+ :"**/*.rb".to_s
10
+ )].each { |file| require file}
11
+ }
12
+
13
+ module Aro
14
+ class Mancy
15
+ include Singleton
16
+
17
+ attr_accessor :game
18
+
19
+ def initialize
20
+ Aro::Create.new(Aro::Database.get_name_from_namefile)
21
+ self.game = Aro::Deck.current_deck
22
+ end
23
+
24
+ def self.exit_error_missing_args!
25
+ Aro::P.p.say(I18n.t("cli.errors.header"))
26
+ Aro::P.p.say(I18n.t("cli.errors.missing_args", cmd: "#{CLI::ARGV0} #{CLI::ARGV1} #{CLI::ARGV2}"))
27
+ exit(CLI::EXIT_CODES[:INVALID_ARG])
28
+ end
29
+
30
+ def self.game
31
+ Mancy.instance.game
32
+ end
33
+ end
34
+ end
35
+
36
+ # TODO: this doesn't work
37
+ # require :"active_support/time_with_zone".to_s
38
+ # TODO: this doesn't work
39
+ # Time.zone = ActiveSupport::TimeZone[Time.now.gmtoff].tzinfo.name