Module: Bini

Extended by:
Bini
Included in:
Bini
Defined in:
lib/bini/core.rb,
lib/bini.rb,
lib/bini/sash.rb,
lib/bini/config.rb,
lib/bini/prompts.rb,
lib/bini/version.rb,
lib/bini/filemagic.rb,
lib/bini/optparser.rb,
lib/bini/extensions/savable.rb

Overview

A collection of very small helpers that assist me in writing a CLI without getting in the way.

Provides some dynamic attributes, they all behave the same and just hook into the defaults to provide non-nil results when needed.

Defined Under Namespace

Modules: Extensions, FileMagic, Prompts Classes: OptionParser, Sash

Constant Summary collapse

Config =

A helper for storing configuration related data in.

Sash.new options:{file:"#{Bini.config_dir}/#{Bini.name}.yaml"}
VERSION =

The current version of Bini.

"0.7.0"
Options =

An automatically created entry point into the OptionParser class.

Bini::OptionParser.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_dirString

Returns The directory to store any cache related files.

Returns:

  • (String)

    The directory to store any cache related files.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bini/core.rb', line 15

module Bini
  extend self

  # A collection of sane defaults to be provided if the same attr is still nil.
  attr_accessor :defaults
  attr_accessor :long_name
  attr_accessor :cache_dir
  attr_accessor :config_dir
  attr_accessor :data_dir
  attr_accessor :version

  @defaults = {}
  @defaults[:long_name]   = Proc.new { $0.split("/").last }
  @defaults[:cache_dir]   = Proc.new { "#{Dir.home}/.cache/#{@long_name}" }
  @defaults[:config_dir]  = Proc.new { "#{Dir.home}/.config/#{@long_name}" }
  @defaults[:data_dir]    = Proc.new { "#{Dir.home}/.local/share/#{@long_name}" }
  @defaults[:version]     = Proc.new { "v0.0.0" }

  # Dynamic attribute's based off the defaults.
  def keys
    Bini.instance_methods.select do |m|
      m =~ /=$/ && m != :defaults=
    end.map do |m|
      m[0..-2].to_sym
    end
  end

  # Reset the defaults back to nothing.
  def clear
    @defaults.each do |key,value|
      instance_variable_set "@#{key}", nil
    end
  end
  keys.each do |key|
    define_method(key) do
      v = instance_variable_get "@#{key}"
      return v if v

      @defaults[key] ? default = @defaults[key].call : default = nil
      return default
    end
    define_method("#{key}=".to_sym) do |dir|
      instance_variable_set "@#{key}", dir
    end
  end

  # Adds a rails style configure method.
  def configure
    yield self
    parameters
  end

  # List available parameters and values in those params
  def parameters
    @values = {}
    keys.each { |k| @values.merge! k => Bini.send(k) }
    @values
  end
  alias_method :params, :parameters
end

#config_dirString

Returns The directory to store any config related files.

Returns:

  • (String)

    The directory to store any config related files.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bini/core.rb', line 15

module Bini
  extend self

  # A collection of sane defaults to be provided if the same attr is still nil.
  attr_accessor :defaults
  attr_accessor :long_name
  attr_accessor :cache_dir
  attr_accessor :config_dir
  attr_accessor :data_dir
  attr_accessor :version

  @defaults = {}
  @defaults[:long_name]   = Proc.new { $0.split("/").last }
  @defaults[:cache_dir]   = Proc.new { "#{Dir.home}/.cache/#{@long_name}" }
  @defaults[:config_dir]  = Proc.new { "#{Dir.home}/.config/#{@long_name}" }
  @defaults[:data_dir]    = Proc.new { "#{Dir.home}/.local/share/#{@long_name}" }
  @defaults[:version]     = Proc.new { "v0.0.0" }

  # Dynamic attribute's based off the defaults.
  def keys
    Bini.instance_methods.select do |m|
      m =~ /=$/ && m != :defaults=
    end.map do |m|
      m[0..-2].to_sym
    end
  end

  # Reset the defaults back to nothing.
  def clear
    @defaults.each do |key,value|
      instance_variable_set "@#{key}", nil
    end
  end
  keys.each do |key|
    define_method(key) do
      v = instance_variable_get "@#{key}"
      return v if v

      @defaults[key] ? default = @defaults[key].call : default = nil
      return default
    end
    define_method("#{key}=".to_sym) do |dir|
      instance_variable_set "@#{key}", dir
    end
  end

  # Adds a rails style configure method.
  def configure
    yield self
    parameters
  end

  # List available parameters and values in those params
  def parameters
    @values = {}
    keys.each { |k| @values.merge! k => Bini.send(k) }
    @values
  end
  alias_method :params, :parameters
end

#data_dirObject

Returns the value of attribute data_dir.



23
24
25
# File 'lib/bini/core.rb', line 23

def data_dir
  @data_dir
end

#defaultsObject

A collection of sane defaults to be provided if the same attr is still nil.



19
20
21
# File 'lib/bini/core.rb', line 19

def defaults
  @defaults
end

#long_nameString

Returns An application name, useful if your app is named differently then your binary.

Returns:

  • (String)

    An application name, useful if your app is named differently then your binary.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bini/core.rb', line 15

module Bini
  extend self

  # A collection of sane defaults to be provided if the same attr is still nil.
  attr_accessor :defaults
  attr_accessor :long_name
  attr_accessor :cache_dir
  attr_accessor :config_dir
  attr_accessor :data_dir
  attr_accessor :version

  @defaults = {}
  @defaults[:long_name]   = Proc.new { $0.split("/").last }
  @defaults[:cache_dir]   = Proc.new { "#{Dir.home}/.cache/#{@long_name}" }
  @defaults[:config_dir]  = Proc.new { "#{Dir.home}/.config/#{@long_name}" }
  @defaults[:data_dir]    = Proc.new { "#{Dir.home}/.local/share/#{@long_name}" }
  @defaults[:version]     = Proc.new { "v0.0.0" }

  # Dynamic attribute's based off the defaults.
  def keys
    Bini.instance_methods.select do |m|
      m =~ /=$/ && m != :defaults=
    end.map do |m|
      m[0..-2].to_sym
    end
  end

  # Reset the defaults back to nothing.
  def clear
    @defaults.each do |key,value|
      instance_variable_set "@#{key}", nil
    end
  end
  keys.each do |key|
    define_method(key) do
      v = instance_variable_get "@#{key}"
      return v if v

      @defaults[key] ? default = @defaults[key].call : default = nil
      return default
    end
    define_method("#{key}=".to_sym) do |dir|
      instance_variable_set "@#{key}", dir
    end
  end

  # Adds a rails style configure method.
  def configure
    yield self
    parameters
  end

  # List available parameters and values in those params
  def parameters
    @values = {}
    keys.each { |k| @values.merge! k => Bini.send(k) }
    @values
  end
  alias_method :params, :parameters
end

#versionString

Returns The version of the application, not of Bini.

Returns:

  • (String)

    The version of the application, not of Bini.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bini/core.rb', line 15

module Bini
  extend self

  # A collection of sane defaults to be provided if the same attr is still nil.
  attr_accessor :defaults
  attr_accessor :long_name
  attr_accessor :cache_dir
  attr_accessor :config_dir
  attr_accessor :data_dir
  attr_accessor :version

  @defaults = {}
  @defaults[:long_name]   = Proc.new { $0.split("/").last }
  @defaults[:cache_dir]   = Proc.new { "#{Dir.home}/.cache/#{@long_name}" }
  @defaults[:config_dir]  = Proc.new { "#{Dir.home}/.config/#{@long_name}" }
  @defaults[:data_dir]    = Proc.new { "#{Dir.home}/.local/share/#{@long_name}" }
  @defaults[:version]     = Proc.new { "v0.0.0" }

  # Dynamic attribute's based off the defaults.
  def keys
    Bini.instance_methods.select do |m|
      m =~ /=$/ && m != :defaults=
    end.map do |m|
      m[0..-2].to_sym
    end
  end

  # Reset the defaults back to nothing.
  def clear
    @defaults.each do |key,value|
      instance_variable_set "@#{key}", nil
    end
  end
  keys.each do |key|
    define_method(key) do
      v = instance_variable_get "@#{key}"
      return v if v

      @defaults[key] ? default = @defaults[key].call : default = nil
      return default
    end
    define_method("#{key}=".to_sym) do |dir|
      instance_variable_set "@#{key}", dir
    end
  end

  # Adds a rails style configure method.
  def configure
    yield self
    parameters
  end

  # List available parameters and values in those params
  def parameters
    @values = {}
    keys.each { |k| @values.merge! k => Bini.send(k) }
    @values
  end
  alias_method :params, :parameters
end

Instance Method Details

#clearObject

Reset the defaults back to nothing.



43
44
45
46
47
# File 'lib/bini/core.rb', line 43

def clear
  @defaults.each do |key,value|
    instance_variable_set "@#{key}", nil
  end
end

#configure {|_self| ... } ⇒ Object

Adds a rails style configure method.

Yields:

  • (_self)

Yield Parameters:

  • _self (Bini)

    the object that the method was called on



62
63
64
65
# File 'lib/bini/core.rb', line 62

def configure
  yield self
  parameters
end

#keysObject

Dynamic attribute’s based off the defaults.



34
35
36
37
38
39
40
# File 'lib/bini/core.rb', line 34

def keys
  Bini.instance_methods.select do |m|
    m =~ /=$/ && m != :defaults=
  end.map do |m|
    m[0..-2].to_sym
  end
end

#parametersObject Also known as: params

List available parameters and values in those params



68
69
70
71
72
# File 'lib/bini/core.rb', line 68

def parameters
  @values = {}
  keys.each { |k| @values.merge! k => Bini.send(k) }
  @values
end