Class: RailsForge::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/railsforge/config.rb

Overview

Config class handles RailsForge configuration

Constant Summary collapse

CONFIG_FILE =

Default config file name

".railsforgerc".freeze
DEFAULT_CONFIG =

Default configuration

{
  "version" => "1.0",
  "profile" => "standard",
  "generators" => {
    "default_template_version" => "v1",
    "include_specs" => true
  },
  "analyzers" => {
    "controller_max_lines" => 150,
    "controller_max_methods" => 10,
    "model_max_lines" => 200,
    "model_max_method_lines" => 15
  },
  "refactor" => {
    "min_method_lines" => 15,
    "auto_extract" => false
  }
}.freeze

Class Method Summary collapse

Class Method Details

.find_rails_app_pathObject

Find Rails app path



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/railsforge/config.rb', line 167

def self.find_rails_app_path
  path = Dir.pwd
  max_depth = 10

  max_depth.times do
    return path if File.exist?(File.join(path, "config", "application.rb"))
    parent = File.dirname(path)
    break if parent == path
    path = parent
  end

  nil
end

.get(key, base_path = nil) ⇒ Object

Get a specific config value

Parameters:

  • key (String)

    Configuration key (dot-notation supported)

  • base_path (String) (defaults to: nil)

    Rails app root path

Returns:

  • (Object)

    Configuration value



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/railsforge/config.rb', line 64

def self.get(key, base_path = nil)
  config = load(base_path)

  keys = key.split(".")
  value = config

  keys.each do |k|
    value = value[k] if value.is_a?(Hash)
  end

  value
end

.init(base_path = nil) ⇒ Object

Initialize default config file

Parameters:

  • base_path (String) (defaults to: nil)

    Rails app root path



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/railsforge/config.rb', line 98

def self.init(base_path = nil)
  base_path ||= find_rails_app_path || Dir.pwd

  config_file = File.join(base_path, CONFIG_FILE)

  if File.exist?(config_file)
    puts "Config file already exists"
    return false
  end

  save(DEFAULT_CONFIG.dup, base_path)
  true
end

.load(base_path = nil) ⇒ Hash

Load configuration from file

Parameters:

  • base_path (String) (defaults to: nil)

    Rails app root path

Returns:

  • (Hash)

    Configuration



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/railsforge/config.rb', line 36

def self.load(base_path = nil)
  base_path ||= find_rails_app_path || Dir.pwd

  config_file = File.join(base_path, CONFIG_FILE)

  if File.exist?(config_file)
    YAML.safe_load(File.read(config_file), permitted_classes: [], permitted_symbols: [], aliases: true) || DEFAULT_CONFIG
  else
    DEFAULT_CONFIG.dup
  end
end

.reset(base_path = nil) ⇒ Object

Reset configuration to defaults

Parameters:

  • base_path (String) (defaults to: nil)

    Rails app root path



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/railsforge/config.rb', line 151

def self.reset(base_path = nil)
  base_path ||= find_rails_app_path || Dir.pwd

  config_file = File.join(base_path, CONFIG_FILE)

  if File.exist?(config_file)
    File.delete(config_file)
    puts "Configuration reset to defaults"
  else
    puts "No configuration file to reset"
  end
end

.save(config, base_path = nil) ⇒ Object

Save configuration to file

Parameters:

  • config (Hash)

    Configuration to save

  • base_path (String) (defaults to: nil)

    Rails app root path



51
52
53
54
55
56
57
58
# File 'lib/railsforge/config.rb', line 51

def self.save(config, base_path = nil)
  base_path ||= find_rails_app_path || Dir.pwd

  config_file = File.join(base_path, CONFIG_FILE)

  File.write(config_file, YAML.dump(config))
  puts "Configuration saved to #{config_file}"
end

.set(key, value, base_path = nil) ⇒ Object

Set a specific config value

Parameters:

  • key (String)

    Configuration key

  • value (Object)

    Value to set

  • base_path (String) (defaults to: nil)

    Rails app root path



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/railsforge/config.rb', line 81

def self.set(key, value, base_path = nil)
  config = load(base_path)

  keys = key.split(".")
  current = config

  keys[0...-1].each do |k|
    current[k] ||= {}
    current = current[k]
  end

  current[keys.last] = value
  save(config, base_path)
end

.show(base_path = nil) ⇒ Object

Show current configuration

Parameters:

  • base_path (String) (defaults to: nil)

    Rails app root path



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/railsforge/config.rb', line 114

def self.show(base_path = nil)
  config = load(base_path)

  puts "RailsForge Configuration:"
  puts ""

  # Version
  puts "  Version: #{config['version']}"
  puts "  Profile: #{config['profile']}"
  puts ""

  # Generators
  puts "  Generators:"
  gens = config["generators"] || {}
  gens.each do |key, value|
    puts "    #{key}: #{value}"
  end
  puts ""

  # Analyzers
  puts "  Analyzers:"
  analyzers = config["analyzers"] || {}
  analyzers.each do |key, value|
    puts "    #{key}: #{value}"
  end
  puts ""

  # Refactor
  puts "  Refactor:"
  refactor = config["refactor"] || {}
  refactor.each do |key, value|
    puts "    #{key}: #{value}"
  end
end