Class: Brice::Config

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

Overview

Exclude unwanted packages:

config.exclude 'foo', 'bar'
Brice.config -= %w[quix quux]

Include non-default packages:

config.include 'foo', 'bar'
Brice.config += %w[quix quux]

Configure individual packages (depends on package):

# set property
config.foo.bar = 'baz'

# set multiple properties
config.foo = %w[bar baz]
# which is equivalent to:
config.foo.bar = true
config.foo.baz = true

# reset package configuration
config.foo!

# see whether package is enabled/included
config.foo?

Defined Under Namespace

Classes: PackageConfig

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(packages = []) ⇒ Config

Returns a new instance of Config.


62
63
64
65
# File 'lib/brice/config.rb', line 62

def initialize(packages = [])
  @packages = Hash.new { |h, k| h[k] = PackageConfig.new }
  self.include(*packages)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

call-seq:

config.package                # package configuration
config.package = 'foo'        # equivalent to: config.package.foo = true
config.package = %w[foo bar]  # see above, multiple
config.package!               # reset package configuration
config.package?               # see whether package is enabled/included

Convenience accessors to individual package configurations.


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/brice/config.rb', line 139

def method_missing(method, *args)
  package, punctuation = method.to_s.sub(/([=!?])?\z/, ''), $1

  assignment = punctuation == '='

  unless assignment || args.empty?
    raise ArgumentError, "wrong number of arguments (#{args.size} for 0)"
  end

  case punctuation
    when '=', '!'
      config = packages[package] = PackageConfig.new

      assignment ? Array(args.first).each { |arg|
        config.send("#{arg}=", true)
      } : config
    when '?'
      include?(package)
    else
      self[package]
  end
end

Instance Attribute Details

#packagesObject (readonly)

Returns the value of attribute packages


60
61
62
# File 'lib/brice/config.rb', line 60

def packages
  @packages
end

Instance Method Details

#[](package) ⇒ Object

call-seq:

config[package]

Accessor for package package.


71
72
73
# File 'lib/brice/config.rb', line 71

def [](package)
  packages[package.to_s]
end

#clearObject

call-seq:

config.clear

Clear all packages.


116
117
118
# File 'lib/brice/config.rb', line 116

def clear
  packages.clear
end

#exclude(*packages) ⇒ Object Also known as: -

call-seq:

config.exclude(*packages)
config -= packages

Disable/exclude packages packages.


92
93
94
95
# File 'lib/brice/config.rb', line 92

def exclude(*packages)
  packages.each { |package| self.packages.delete(package.to_s) }
  self
end

#include(*packages) ⇒ Object Also known as: +

call-seq:

config.include(*packages)
config += packages

Enable/include packages packages.


80
81
82
83
# File 'lib/brice/config.rb', line 80

def include(*packages)
  packages.each { |package| self[package] }
  self
end

#include?(package) ⇒ Boolean Also known as: have?

call-seq:

config.include?(package) => true or false
config.have?(package) => true or false

See whether package package is enabled/included.

Returns:

  • (Boolean)

125
126
127
# File 'lib/brice/config.rb', line 125

def include?(package)
  packages.include?(package.to_s)
end

#only(*packages) ⇒ Object Also known as: %

call-seq:

config.only(*packages)
config %= packages

Enable/include only packages packages, disable/exclude everything else.


105
106
107
108
# File 'lib/brice/config.rb', line 105

def only(*packages)
  clear
  self.include(*packages)
end