Class: Crudboy::App

Inherits:
Object show all
Defined in:
lib/crudboy/app.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ App

Returns a new instance of App.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/crudboy/app.rb', line 14

def initialize(options)
  require 'active_support/all'
  require 'active_record'
  require 'composite_primary_keys'
  require "crudboy/connection"
  require "crudboy/definition"
  @options = options
  App.env = @options.env
  App.connect_options = connect_options
  Connection.open(App.connect_options)
  @definition = Definition.new(effective_config)
  @context = TemplateContext.new(@definition)
  load_initializer!
  App.instance = self
end

Class Attribute Details

.connect_optionsObject

Returns the value of attribute connect_options.



7
8
9
# File 'lib/crudboy/app.rb', line 7

def connect_options
  @connect_options
end

.envObject

Returns the value of attribute env.



7
8
9
# File 'lib/crudboy/app.rb', line 7

def env
  @env
end

.instanceObject

Returns the value of attribute instance.



7
8
9
# File 'lib/crudboy/app.rb', line 7

def instance
  @instance
end

.promptObject

Returns the value of attribute prompt.



7
8
9
# File 'lib/crudboy/app.rb', line 7

def prompt
  @prompt
end

Class Method Details

.configObject



9
10
11
# File 'lib/crudboy/app.rb', line 9

def config
  @@effective_config
end

Instance Method Details

#configObject



65
66
67
68
69
# File 'lib/crudboy/app.rb', line 65

def config
  @config ||= YAML.load(IO.read(File.expand_path(@options.config_file)), aliases: true).with_indifferent_access
rescue ArgumentError
  @config ||= YAML.load(IO.read(File.expand_path(@options.config_file))).with_indifferent_access
end

#connect_optionsObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/crudboy/app.rb', line 30

def connect_options
  connect_conf = effective_config.slice(:adapter, :host, :username,
                         :password, :database, :encoding,
                         :pool, :port, :socket)
  if effective_config[:ssh].present?
    connect_conf.merge!(start_ssh_proxy!)
  end

  connect_conf
end

#effective_configObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/crudboy/app.rb', line 82

def effective_config
  @@effective_config ||= nil
  unless @@effective_config
    @@effective_config = selected_config.deep_merge(@options.to_h)
    if @@effective_config[:adapter].blank?
      @@effective_config[:adapter] = 'sqlite3'
    end
    @@effective_config[:database] = File.expand_path(@@effective_config[:database]) if @@effective_config[:adapter] == 'sqlite3'
  end
  @@effective_config
end

#load_initializer!Object



41
42
43
44
45
46
47
48
49
# File 'lib/crudboy/app.rb', line 41

def load_initializer!
  return unless effective_config[:initializer]
  initializer_file = File.expand_path(effective_config[:initializer])
  unless File.exists?(initializer_file)
    STDERR.puts "Specified initializer file not found, #{effective_config[:initializer]}"
    exit(1)
  end
  load(initializer_file)
end

#run!Object



94
95
96
# File 'lib/crudboy/app.rb', line 94

def run!
  Bundle.new(@options.template_bundle, @options.template_args, @options.destination, @context).render!
end

#selected_configObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/crudboy/app.rb', line 71

def selected_config
  if @options.env.present? && !config[@options.env].present?
    STDERR.puts "Specified ENV `#{@options.env}' not exists"
  end
  if env = @options.env
    config[env]
  else
    {}
  end
end

#start_ssh_proxy!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/crudboy/app.rb', line 51

def start_ssh_proxy!
  ssh_config = effective_config[:ssh]
  local_ssh_proxy_port = Crudboy::SSHProxy.connect(
    ssh_config.slice(:host, :user, :port, :password).merge(
      forward_host: effective_config[:host],
      forward_port: effective_config[:port],
      local_port: ssh_config[:local_port]))

  {
    host: '127.0.0.1',
    port: local_ssh_proxy_port
  }
end