Class: Hairballs::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/hairballs/configuration.rb

Overview

The base-level Hairballs class maintains an object of this type, which contains the current configuration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



40
41
42
43
44
45
46
47
48
# File 'lib/hairballs/configuration.rb', line 40

def initialize
  @themes = []
  @current_theme = nil
  @plugins = []
  @loaded_plugins = []
  @completion_procs = []
  @project_name = nil
  @project_root = nil
end

Instance Attribute Details

#completion_procsArray<Proc> (readonly)

Used for maintaining all possible completion Procs, thus allowing many different plugins to define a Proc for completion without overriding Procs defined for other plugins.

Returns:

  • (Array<Proc>)


35
36
37
# File 'lib/hairballs/configuration.rb', line 35

def completion_procs
  @completion_procs
end

#current_themeHairballs::Theme (readonly)

Returns:



18
19
20
# File 'lib/hairballs/configuration.rb', line 18

def current_theme
  @current_theme
end

#loaded_pluginsArray<Hairballs::Plugin> (readonly)

All Hairballs::Plugins that have been loaded.

Returns:



28
29
30
# File 'lib/hairballs/configuration.rb', line 28

def loaded_plugins
  @loaded_plugins
end

#pluginsArray<Hairballs::Plugin> (readonly)

All Hairballs::Plugins available to be loaded.

Returns:



23
24
25
# File 'lib/hairballs/configuration.rb', line 23

def plugins
  @plugins
end

#project_nameString (readonly)

Name of the project, if it can be determined. If not, defaults to “irb”.

Returns:

  • (String)


38
39
40
# File 'lib/hairballs/configuration.rb', line 38

def project_name
  @project_name
end

#themesArray<Hairballs::Theme> (readonly)

Returns:



15
16
17
# File 'lib/hairballs/configuration.rb', line 15

def themes
  @themes
end

Instance Method Details

#add_plugin(name, **options) ⇒ Object

Builds a new Hairballs::Plugin and adds it to thelist of ‘.plugins` that can be used.

Parameters:

  • name (Symbol)
  • options (Hash)

    Plugin-dependent options to define. These get passed along the the Hairballs::Plugin object and are used as attributes of the plugin.



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/hairballs/configuration.rb', line 117

def add_plugin(name, **options)
  vputs "[config] Adding plugin: #{name}"
  plugin = Plugin.new(name, options)
  yield plugin
  plugins << plugin
  vputs "[config] Added plugin: #{name}"

  plugin
rescue => ex
  # rubocop:disable Metrics/LineLength
  vputs "[config] Exception adding plugin: #{ex.class}: #{ex.message}\n#{ex.backtrace[0]}"
  # rubocop:enable Metrics/LineLength
end

#add_theme(name) {|theme| ... } ⇒ Theme

Builds a new Hairballs::Theme and adds it to the list of ‘.themes` that can be used.

Parameters:

  • name (Symbol)

    Name to give the new Theme.

Yields:

  • (theme)

Returns:



85
86
87
88
89
90
91
92
93
94
# File 'lib/hairballs/configuration.rb', line 85

def add_theme(name)
  theme = Theme.new(name)

  yield theme

  themes << theme
  vputs "[config] Added theme: #{name}"

  theme
end

#load_plugin(plugin_name, **options) ⇒ Object

Searches for the Hairballs::Plugin by the plugin_name, then loads it. Raises

Parameters:

  • plugin_name (Symbol)


134
135
136
137
138
139
140
141
142
143
# File 'lib/hairballs/configuration.rb', line 134

def load_plugin(plugin_name, **options)
  plugin_to_use = plugins.find { |plugin| plugin.name == plugin_name }
  fail PluginNotFound, plugin_name unless plugin_to_use
  vputs "[config] Using plugin: #{plugin_name}"

  plugin_to_use.load!(options)
  loaded_plugins << plugin_to_use

  true
end

#project_rootPathname

Returns:

  • (Pathname)


70
71
72
# File 'lib/hairballs/configuration.rb', line 70

def project_root
  @project_root ||= find_project_root
end

#project_root?(path) ⇒ Boolean

Parameters:

  • path (String)

Returns:

  • (Boolean)


76
77
78
# File 'lib/hairballs/configuration.rb', line 76

def project_root?(path)
  File.expand_path(path) == project_root
end

#rails?Boolean

Is IRB getting loaded for a Rails console?

Returns:

  • (Boolean)


58
59
60
# File 'lib/hairballs/configuration.rb', line 58

def rails?
  ENV.key?('RAILS_ENV') || Kernel.const_defined?(:Rails)
end

#use_theme(theme_name) ⇒ Object

Tells IRB to use the Hairballs::Theme of theme_name.

Parameters:

  • theme_name (Symbol)

    The name of the Theme to use/switch to.



99
100
101
102
103
104
105
106
107
108
# File 'lib/hairballs/configuration.rb', line 99

def use_theme(theme_name)
  switch_to = themes.find { |theme| theme.name == theme_name }
  fail ThemeUseFailure, theme_name unless switch_to
  vputs "[config] Switched to theme: #{theme_name}"

  switch_to.use!
  @current_theme = switch_to

  true
end

#versionString

Returns:

  • (String)


51
52
53
# File 'lib/hairballs/configuration.rb', line 51

def version
  Hairballs::VERSION
end