Giter Site home page Giter Site logo

jchayan / abstract_command Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ktlacaelel/abstract_command

0.0 0.0 0.0 12 KB

Shell Command Abstraction - Let's you interact with shell commands as if they were objects.

License: MIT License

Ruby 100.00%

abstract_command's Introduction

Abstract Command

Shell Command Abstraction - Let's you interact with shell commands as if they were objects.

Ideas behind:

  • Enforces standardization.
  • Enforces separation of command definition and consumption.
  • Enforces configuration over code.
  • Enforces configuration over refactoring.
  • Enforces simple shell-command definition.
  • Enforces automatic sanitization of variables that get interpolated.
  • Provides a simple Object Oriented Interface.
  • Provides a scope for variables that belong to the command.
  • Provides getters and setter for every interpolation in command.
  • Provides a neat interface that plugs to data structures transparently.
  • Avoids methods with many arguments.
  • Avoids changes in the standared libarary: system, backtick, etc.

Hello world example:

require 'abstract_command'

# Hello World Example.
class HelloCommand < AbstractCommand
  def template
    'echo Hello %<name>s'
  end
end

command = HelloCommand.new
command.name = 'World.'

puts command.to_s
$ ruby example.rb
echo Hello World.

Dynamic variables in command:

require 'abstract_command'

class VarsCommand < AbstractCommand
  def template
    'echo %<var_1>s %<var_2>s %<var_3>s %<var_4>s %<var_5>s'
  end
end

command = VarsCommand.new
command.var_1 = 'one'
command.var_2 = 'two'
command.var_3 = 'three'
command.var_4 = 'four'
command.var_5 = 'five'
puts command.to_s
puts command.system
$ ruby variables.rb
echo one two three four five
one two three four five

Namespace suggestion:

require 'abstract_command'

module Command

  class Hello < AbstractCommand
    def template
      'echo Hello %<name>s'
    end
  end

  class Bye < AbstractCommand
    def template
      'echo Bye %<name>s'
    end
  end

end

command = Command::Hello.new
command.name = 'world'
puts command.to_s
puts command.system
puts command.backtick
$ ruby namespace_suggestion.rb
echo Hello world
Hello world
true
Hello world

Fast initialization:

require 'abstract_command'

module Command
  class Hello < AbstractCommand
    def template
      'echo Hello %<name>s'
    end
  end
end

command = Command::Hello.new(:name => 'Kazu')
puts command.to_s
puts command.system
$ ruby constructor.rb
echo Hello Kazu
Hello Kazu
true

Automatic Sanitization:

require 'abstract_command'

module Command
  class Hello < AbstractCommand
    def template
      'echo Hello %<name>s'
    end
  end
end

command = Command::Hello.new(:name => '; touch /tmp/x')
puts command.to_s
puts command.system
$ ruby sanitization.rb
echo Hello \;\ touch\ /tmp/x
Hello ; touch /tmp/x
true

Transparent data interpretation:

require 'abstract_command'

developers = [
  { :name => 'Kazu' },
  { :name => 'Adrian' },
  { :name => 'Cesar' },
  { :name => 'Sergio' }
]

module Command
  class Hello < AbstractCommand
    def template
      'echo Hello %<name>s'
    end
  end
end

developers.each do |developer|
  Command::Hello.new(developer).system
end
$ ruby hello_developers.rb
Hello Kazu
Hello Adrian
Hello Cesar
Hello Sergio

I hope it is useful to you :)

abstract_command's People

Contributors

jchayan avatar ktlacaelel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.