Class: CommandLion::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/command_lion/base.rb

Overview

To encapsulate some of the common patterns within Command Lion, there is a Base class which both the App and Command class inhereit from.

Direct Known Subclasses

App, Command, Flags

Class Method Summary collapse

Class Method Details

.build(&block) ⇒ Object

Build up an object with a given block.

Basic Usage

class Example < CommandLion::Base
  simple_attr :example
end

test = Example.build do
  example "lol"
end

test.example
# => "lol"

test.example?
# => true

Parameters:

  • (&block)


26
27
28
29
30
# File 'lib/command_lion/base.rb', line 26

def build(&block)
  obj = new
  obj.instance_eval(&block)
  obj
end

.key_value(attribute) ⇒ Object

Quickly spin up an in-memory key-value storage object for the framework.

Basic Usage

class Example < CommandLion::Base
  key_value :example
end

test = Example.new
test.example[:lol] = "lol"

test.example[:lol]
# => "lol"


45
46
47
48
49
50
# File 'lib/command_lion/base.rb', line 45

def key_value(attribute)
  define_method :"#{attribute}" do 
    instance_variable_set(:"@#{attribute}", Hash.new) unless instance_variable_get(:"@#{attribute}")
    instance_variable_get(:"@#{attribute}")
  end
end

.simple_attr(attribute) ⇒ Object

This method is important for creating the basic attributes for the framework.

Basic Usage

class Example < CommandLion::Base
  simple_attr :example
end

test = Example.new

test.example
# => nil

test.example?
# => false

test.example = "lol"
# => "lol"

test.example
# => "lol"

test.example?
# => true

Parameters:

  • attribute (Symbol)


77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/command_lion/base.rb', line 77

def simple_attr(attribute)
  define_method :"#{attribute}" do |value = nil|
    return instance_variable_get(:"@#{attribute}") if value.nil?
    instance_variable_set(:"@#{attribute}", value)
  end
  define_method :"#{attribute}=" do |value|
    instance_variable_set(:"@#{attribute}", value)
  end
  define_method :"#{attribute}?" do
    return true if instance_variable_get(:"@#{attribute}")
    false
  end
end

.simple_attrs(*attributes) ⇒ Object

This method is important for creating multiple basic attributes.

Basic Usage

class Example < CommandLion::Base
  simple_attrs :example, :example2
end

test = Example.new

test.example  = "lol"
test.example2 = "lol2"

Parameters:

  • attribute (Array<Symbol>)


104
105
106
107
108
# File 'lib/command_lion/base.rb', line 104

def simple_attrs(*attributes)
  attributes.each do |attribute|
    simple_attr(attribute)
  end
end