Class: MultiAR::Interface
- Inherits:
- 
      Object
      
        - Object
- MultiAR::Interface
 
- Defined in:
- lib/multi_ar/interface.rb
Overview
An utility to ease creation of executable using multi database ActiveRecord through command line interface.
Constant Summary collapse
- DEFAULT_DB_CONFIG =
- "config/database.yaml"
Instance Attribute Summary collapse
- 
  
    
      #dependencies  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Hash of gems the application depends to. 
- 
  
    
      #description  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Description of the application show in usage texts on CLI. 
- 
  
    
      #migration_framework  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    If set to true, migration framework and other Rake related functionality will be enabled. 
- 
  
    
      #options  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Options that will be enabled. 
- 
  
    
      #run_by_default  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Boolean of whether no arguments are needed. 
- 
  
    
      #version  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Version shown with –version flag on CLI. 
Instance Method Summary collapse
- #cli {|p| ... } ⇒ Object
- 
  
    
      #initialize  ⇒ Interface 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of Interface. 
Constructor Details
#initialize ⇒ Interface
Returns a new instance of Interface.
| 52 53 54 55 56 | # File 'lib/multi_ar/interface.rb', line 52 def initialize @options = {} @dependencies = {} @run_by_default = false end | 
Instance Attribute Details
#dependencies ⇒ Object
what for this actually is? Write an example or just tell what for this should be used.
Hash of gems the application depends to.
Format should be:
{ "gem_name" => "~> 2.0", "another_gem" => nil }
This is used in –init.
| 50 51 52 | # File 'lib/multi_ar/interface.rb', line 50 def dependencies @dependencies end | 
#description ⇒ Object
Description of the application show in usage texts on CLI
| 34 35 36 | # File 'lib/multi_ar/interface.rb', line 34 def description @description end | 
#migration_framework ⇒ Object
If set to true, migration framework and other Rake related functionality will be enabled.
| 37 38 39 | # File 'lib/multi_ar/interface.rb', line 37 def migration_framework @migration_framework end | 
#options ⇒ Object
Options that will be enabled.
Options supported by this system are:
- 
config # ‘true` or `String` 
- 
db_config # ‘true` or `String` 
- 
dry # boolean 
- 
environment # ‘true` or `String` 
- 
verbose # boolean 
- 
databases # false, ‘Array` or `Hash`. 
If value is ‘true`, an option will be added to CLI interface. If the value is something else, the option will be populated by this value instead.
‘environment` option is enabled by default, rest are disabled.
| 28 29 30 | # File 'lib/multi_ar/interface.rb', line 28 def @options end | 
#run_by_default ⇒ Object
Boolean of whether no arguments are needed
| 40 41 42 | # File 'lib/multi_ar/interface.rb', line 40 def run_by_default @run_by_default end | 
#version ⇒ Object
Version shown with –version flag on CLI
| 31 32 33 | # File 'lib/multi_ar/interface.rb', line 31 def version @version end | 
Instance Method Details
#cli {|p| ... } ⇒ Object
hardcode shorthands
Consumes ARGV, create copy of it if you need it for something.
| 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | # File 'lib/multi_ar/interface.rb', line 62 def cli p = Trollop::Parser.new p.version @version if @version p. @description if @description p.opt "init", "Create stub environment with configuration and database.yaml. " + "For current dir, use “.”.", type: :string p.opt "databases", "List of databases to perform operations", type: :strings if(@options["databases"] != false && !@options["databases"].respond_to?(:each)) p.opt "db_config", "Path to database config file", type: :string, default: DEFAULT_DB_CONFIG if @options["db_config"] == true p.opt "config", "Path to MultiAR framework config file", type: :string, default: "config/settings.yaml" if @options["config"] == true p.opt "dry", "Run the program without doing anything. Useful for debugging with -v", type: :flag if @options["dry"] == true p.opt "environment", "The environment to use. Corresponds to database config name " + "(environment for foo_development is “development”).", type: :string, default: "development" p.opt "verbose", "Be verbose", type: :flag if @options["verbose"] == true if @migration_framework == true p.opt "all_rake_tasks", "List all Rake tasks, not only commented ones", short: "A", type: :flag # TODO: not implemented currently, do we really need this? #p.opt "list_databases", "Lists databases that contains migrations in the gem", type: :flag # TODO: should we do migration_dirs here too instead? #p.opt "migration_dir", "The directory where migrations for databases are read from", type: :string, default: "db/migrate" p.opt "task", "Rake task to execute", short: "t", type: :string p.opt "tasks", "List available Rake tasks", short: "T", type: :flag end yield p if block_given? opts = Trollop::with_standard_exception_handling p do args = ARGV.clone result = p.parse ARGV if not @run_by_default raise Trollop::HelpNeeded if args.empty? end result end @options.each do |key, value| next if value == true # Not bothering to do checks as we just take the intended values, and not looping the array otherwise opts[key] = value end bootstrap opts if opts["init"] # Bootstrap will exit after execution; in that case nothing after this will be run. raise "--config must be path to valid file" if opts[:config_given] and not File.exist? opts["config"] raise "Database config #{opts["db_config"]} seems to be missing" if @options["db_config"] and not File.exist? opts["db_config"] @opts = opts init_multi_ar # Then run Rake tasks as requested return opts if not @migration_framework # TODO: I think there should be much more fine grained control for this if opts["tasks"] || opts["all_rake_tasks"] @multi_ar.list_tasks all_rake_tasks: opts["all_rake_tasks"] exit 1 end if opts["task"].nil? puts "Task must be specified. Check if you passed --task option." exit 1 end puts "Running task #{opts["task"]}" if opts["verbose"] @multi_ar.rake_task opts["task"] end |