lynx 0.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of lynx might be problematic. Click here for more details.

data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Pan Thomakos
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Lynx
2
+
3
+ TODO
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'lynx'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install lynx
18
+
19
+ ## Usage
20
+
21
+ TODO
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new{ |t| t.pattern = 'test/**/*_test.rb' }
5
+
6
+ task :default => :test
@@ -0,0 +1,54 @@
1
+ require 'lynx/config'
2
+
3
+ module Lynx
4
+ module Command
5
+ class Basic
6
+ attr_reader :config
7
+
8
+ class << self
9
+ # A declarative way to generate chaining commands.
10
+ def instruct(method, &block)
11
+ define_method(method) do |*args|
12
+ @command << block.call(self,*args)
13
+
14
+ self
15
+ end
16
+ end
17
+ end
18
+
19
+ def initialize(config)
20
+ @config = config
21
+ @command = []
22
+ end
23
+
24
+ def authorize
25
+ @command << "--user=#{config.username}" if config.username
26
+ @command << "--password=#{config.password}" if config.password
27
+ @command << "--host=#{config.host}" if config.host
28
+
29
+ self
30
+ end
31
+
32
+ instruct(:mysql){ |c| c.config.mysql }
33
+ instruct(:dump){ |c| c.config.dump }
34
+ instruct(:with_database){ |c| c.config.database }
35
+ instruct(:sql){ |c, sql| "--execute='#{sql}'" }
36
+ instruct(:batch){ '--batch' }
37
+ instruct(:no_names){ '--skip-column-names' }
38
+ instruct(:compress){ '--compress' }
39
+ instruct(:skip_add_drop_table){ '--skip-add-drop-table' }
40
+ instruct(:skip_add_locks){ '--skip-add-locks' }
41
+ instruct(:skip_comments){ '--skip-comments' }
42
+ instruct(:skip_quote_names){ '--skip-quote-names' }
43
+ instruct(:no_data){ '--no-data' }
44
+ instruct(:skip_triggers){ '--skip-triggers' }
45
+ instruct(:compact){ '--compact' }
46
+ instruct(:ignore){ |c,table| "--ignore-table=#{table}" }
47
+ instruct(:no_create_info){ '--no-create-info' }
48
+
49
+ def to_s
50
+ @command.join(' ')
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,30 @@
1
+ require 'lynx/command/basic'
2
+
3
+ module Lynx
4
+ module Command
5
+ class Dump < Basic
6
+ def initialize(*args)
7
+ super(*args)
8
+
9
+ dump
10
+ end
11
+
12
+ instruct(:where){ |c,condition| "--where='#{condition}'" }
13
+ instruct(:with_database){ |c| c.database(c.config.database) }
14
+
15
+ def database(*args)
16
+ @command << '--databases' unless args.empty?
17
+ @command += args
18
+
19
+ self
20
+ end
21
+
22
+ def table(*args)
23
+ @command << '--tables' unless args.empty?
24
+ @command += args
25
+
26
+ self
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,2 @@
1
+ require 'lynx/command/basic'
2
+ require 'lynx/command/dump'
@@ -0,0 +1,30 @@
1
+ module Lynx
2
+ class Config
3
+ MYSQL = ['mysql', 'mysql5', '/usr/local/bin/mysql', '/opt/local/bin/mysql5',
4
+ '/usr/local/mysql/bin/mysql']
5
+
6
+ DUMP = ['mysqldump', 'mysqldump5']
7
+
8
+ def initialize(config = {})
9
+ @config = config
10
+ end
11
+
12
+ [:username, :password, :host, :database].each do |method|
13
+ define_method(method){ @config[method] }
14
+ end
15
+
16
+ def mysql
17
+ @mysql ||= @config[:mysql] || detect(MYSQL)
18
+ end
19
+
20
+ def dump
21
+ @dump ||= @config[:dump] || detect(DUMP)
22
+ end
23
+
24
+ private
25
+
26
+ def detect(commands)
27
+ commands.detect{ |c| system("which #{c}") }
28
+ end
29
+ end
30
+ end
data/lib/lynx/d_s_l.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'lynx/config'
2
+ require 'lynx/command'
3
+
4
+ module Lynx
5
+ class DSL
6
+ def initialize(config)
7
+ @config = Lynx::Config.new(config)
8
+ end
9
+
10
+ def basic
11
+ Command::Basic.new(@config)
12
+ .mysql
13
+ .authorize
14
+ .with_database
15
+ end
16
+
17
+ def create_database
18
+ Command::Basic.new(@config)
19
+ .mysql
20
+ .authorize
21
+ .sql("create database #{@config.database}")
22
+ end
23
+
24
+ def drop_database
25
+ Command::Basic.new(@config)
26
+ .mysql
27
+ .authorize
28
+ .sql("drop database #{@config.database}")
29
+ end
30
+
31
+ def dump
32
+ Command::Dump.new(@config)
33
+ .authorize
34
+ .with_database
35
+ .skip_quote_names
36
+ .compress
37
+ .compact
38
+ end
39
+
40
+ def data
41
+ dump.no_create_info
42
+ end
43
+
44
+ def structure
45
+ dump.no_data
46
+ end
47
+
48
+ def select
49
+ basic
50
+ .batch
51
+ .no_names
52
+ .compress
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,21 @@
1
+ require 'lynx/pipe/basic'
2
+
3
+ module Lynx
4
+ module Pipe
5
+ class Append < Basic
6
+ def initialize(file)
7
+ @file = file
8
+ end
9
+
10
+ def perform(command)
11
+ system("#{command} >> #{@file}")
12
+ end
13
+
14
+ def clear
15
+ system("rm -rf #{@file}")
16
+
17
+ self
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module Lynx
2
+ module Pipe
3
+ class Basic
4
+ def perform(command)
5
+ puts command.to_s
6
+ end
7
+
8
+ def clear
9
+ self
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ require 'lynx/pipe/basic'
2
+
3
+ module Lynx
4
+ module Pipe
5
+ class Debug < Basic
6
+ def initialize(pipe = nil)
7
+ @pipe = pipe
8
+ end
9
+
10
+ def perform(command)
11
+ puts "[Lynx] #{command}"
12
+
13
+ @pipe.perform(command) if has_pipe?
14
+ end
15
+
16
+ def clear
17
+ @pipe.clear
18
+
19
+ self
20
+ end
21
+
22
+ private
23
+
24
+ def has_pipe?
25
+ !!@pipe
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ require 'lynx/pipe/basic'
2
+
3
+ module Lynx
4
+ module Pipe
5
+ class Get < Basic
6
+ def perform(command)
7
+ `#{command}`.strip
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ require 'lynx/pipe/basic'
2
+
3
+ module Lynx
4
+ module Pipe
5
+ class Import < Basic
6
+ def initialize(file)
7
+ @file = file
8
+ end
9
+
10
+ def perform(command)
11
+ system("cat #{@file} | #{command}")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require 'lynx/pipe/basic'
2
+
3
+ module Lynx
4
+ module Pipe
5
+ class Run < Basic
6
+ def perform(command)
7
+ system(command)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ require 'lynx/pipe/basic'
2
+
3
+ module Lynx
4
+ module Pipe
5
+ class Write < Basic
6
+ def initialize(file)
7
+ @file = file
8
+ end
9
+
10
+ def perform(command)
11
+ system("#{command} > #{@file}")
12
+ end
13
+
14
+ def clear
15
+ system("rm -rf #{@file}")
16
+
17
+ self
18
+ end
19
+ end
20
+ end
21
+ end
data/lib/lynx/pipe.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'lynx/pipe/basic'
2
+ require 'lynx/pipe/append'
3
+ require 'lynx/pipe/write'
4
+ require 'lynx/pipe/get'
5
+ require 'lynx/pipe/debug'
6
+ require 'lynx/pipe/import'
@@ -0,0 +1,3 @@
1
+ module Lynx
2
+ VERSION = "0.0.1"
3
+ end
data/lib/lynx.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'lynx/version'
2
+ require 'lynx/config'
3
+ require 'lynx/command'
4
+ require 'lynx/pipe'
5
+ require 'lynx/d_s_l'
6
+
7
+ module Lynx
8
+ class << self
9
+ def config(options)
10
+ Lynx::DSL.new(options)
11
+ end
12
+
13
+ def append(*args)
14
+ Pipe::Append.new(*args)
15
+ end
16
+
17
+ def run
18
+ Pipe::Run.new
19
+ end
20
+
21
+ def get
22
+ Pipe::Get.new
23
+ end
24
+
25
+ def write(*args)
26
+ Pipe::Write.new(*args)
27
+ end
28
+
29
+ def import(*args)
30
+ Pipe::Import.new(*args)
31
+ end
32
+
33
+ def debug(*args)
34
+ Pipe::Debug.new(*args)
35
+ end
36
+ end
37
+ end
data/lynx.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lynx/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "lynx"
8
+ gem.version = Lynx::VERSION
9
+ gem.authors = ["Pan Thomakos"]
10
+ gem.email = ["pan.thomakos@gmail.com"]
11
+ gem.description = %q{MySQL command line wrapper for Ruby.}
12
+ gem.summary = %q{MySQL command line wrapper for Ruby.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency('minitest')
21
+ end
@@ -0,0 +1,50 @@
1
+ require 'minitest/autorun'
2
+ require 'lynx/command/basic'
3
+
4
+ describe Lynx::Command::Basic do
5
+ def setup
6
+ @config = Lynx::Config.new(
7
+ dump: 'mysqldump',
8
+ database: 'foo',
9
+ mysql: 'mysql')
10
+ @command = Lynx::Command::Basic.new(@config)
11
+ end
12
+
13
+ def test_mysql
14
+ assert_equal 'mysql', @command.mysql.to_s
15
+ end
16
+
17
+ def test_dump
18
+ assert_equal 'mysqldump', @command.dump.to_s
19
+ end
20
+
21
+ def test_with_database
22
+ assert_equal 'mysql foo', @command.mysql.with_database.to_s
23
+ end
24
+
25
+ def test_batch
26
+ assert_equal 'mysql --batch', @command.mysql.batch.to_s
27
+ end
28
+
29
+ def test_authorize_with_username
30
+ @command.config.stub(:username, 'foo') do
31
+ assert_equal 'mysql --user=foo', @command.mysql.authorize.to_s
32
+ end
33
+ end
34
+
35
+ def test_authorize_with_password
36
+ @command.config.stub(:password, 'bar') do
37
+ assert_equal 'mysql --password=bar', @command.mysql.authorize.to_s
38
+ end
39
+ end
40
+
41
+ def test_authorize_with_host
42
+ @command.config.stub(:host, 'local') do
43
+ assert_equal 'mysql --host=local', @command.mysql.authorize.to_s
44
+ end
45
+ end
46
+
47
+ def test_compress
48
+ assert_equal 'mysql --compress', @command.mysql.compress.to_s
49
+ end
50
+ end
@@ -0,0 +1,20 @@
1
+ require 'minitest/autorun'
2
+ require 'lynx/command/dump'
3
+
4
+ describe Lynx::Command::Dump do
5
+ def setup
6
+ @config = Lynx::Config.new(dump: 'dump', database: 'foo')
7
+ @command = Lynx::Command::Dump.new(@config)
8
+ end
9
+
10
+ def test_command
11
+ @command.table('bar')
12
+ @command.where('1=1')
13
+
14
+ assert_equal "dump --tables bar --where='1=1'", @command.to_s
15
+ end
16
+
17
+ def test_with_database
18
+ assert_equal "dump --databases foo", @command.with_database.to_s
19
+ end
20
+ end
@@ -0,0 +1,40 @@
1
+ require 'minitest/autorun'
2
+ require 'lynx/config'
3
+
4
+ describe Lynx::Config do
5
+ def config
6
+ @config ||= Lynx::Config.new(
7
+ database: 'lynx',
8
+ username: 'name',
9
+ password: 'pass',
10
+ host: 'localhost')
11
+ end
12
+
13
+ it 'knows the mysql command' do
14
+ Kernel.stub(:system, true) do
15
+ assert_equal 'mysql', config.mysql
16
+ end
17
+ end
18
+
19
+ it 'knows the dump command' do
20
+ Kernel.stub(:system, true) do
21
+ assert_equal 'mysqldump', config.dump
22
+ end
23
+ end
24
+
25
+ it 'knows the database' do
26
+ assert_equal 'lynx', config.database
27
+ end
28
+
29
+ it 'knows the username' do
30
+ assert_equal 'name', config.username
31
+ end
32
+
33
+ it 'knows the password' do
34
+ assert_equal 'pass', config.password
35
+ end
36
+
37
+ it 'knows the host' do
38
+ assert_equal 'localhost', config.host
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ require 'minitest/autorun'
2
+ require 'lynx/d_s_l'
3
+
4
+ describe Lynx::DSL do
5
+ def setup
6
+ @dsl = Lynx::DSL.new({host: 'local'})
7
+ end
8
+
9
+ it 'passes the config to the commands' do
10
+ assert_equal 'local', @dsl.select.config.host
11
+ assert_equal 'local', @dsl.structure.config.host
12
+ assert_equal 'local', @dsl.dump.config.host
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ require 'minitest/autorun'
2
+ require 'lynx/command/basic'
3
+ require 'lynx/pipe/append'
4
+
5
+ describe Lynx::Pipe::Append do
6
+ before do
7
+ system("rm -rf append_test")
8
+ @config = Lynx::Config.new
9
+ @command = Lynx::Command::Basic.new(@config).mysql
10
+ @command.batch.no_names.sql("select 100")
11
+ @pipe = Lynx::Pipe::Append.new('append_test')
12
+ end
13
+
14
+ after do
15
+ system("rm -rf append_test")
16
+ end
17
+
18
+ def test_perform
19
+ @pipe.perform(@command)
20
+ @pipe.perform(@command)
21
+ assert_equal "100\n100", File.read('append_test').strip
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ require 'minitest/autorun'
2
+ require 'lynx/command/basic'
3
+ require 'lynx/pipe/get'
4
+
5
+ describe Lynx::Pipe::Get do
6
+ def setup
7
+ @config = Lynx::Config.new
8
+ @command = Lynx::Command::Basic.new(@config).mysql
9
+ @command.batch.no_names.sql("select 100")
10
+ @pipe = Lynx::Pipe::Get.new
11
+ end
12
+
13
+ def test_perform
14
+ assert_equal '100', @pipe.perform(@command)
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ require 'minitest/autorun'
2
+ require 'lynx/command/basic'
3
+ require 'lynx/pipe/write'
4
+
5
+ describe Lynx::Pipe::Write do
6
+ before do
7
+ system("rm -rf write_test")
8
+ @config = Lynx::Config.new
9
+ @command = Lynx::Command::Basic.new(@config).mysql
10
+ @command.batch.no_names.sql("select 100")
11
+ @pipe = Lynx::Pipe::Write.new('write_test')
12
+ end
13
+
14
+ after do
15
+ system("rm -rf write_test")
16
+ end
17
+
18
+ def test_perform
19
+ @pipe.perform(@command)
20
+ assert_equal '100', File.read('write_test').strip
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lynx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pan Thomakos
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: MySQL command line wrapper for Ruby.
31
+ email:
32
+ - pan.thomakos@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/lynx.rb
43
+ - lib/lynx/command.rb
44
+ - lib/lynx/command/basic.rb
45
+ - lib/lynx/command/dump.rb
46
+ - lib/lynx/config.rb
47
+ - lib/lynx/d_s_l.rb
48
+ - lib/lynx/pipe.rb
49
+ - lib/lynx/pipe/append.rb
50
+ - lib/lynx/pipe/basic.rb
51
+ - lib/lynx/pipe/debug.rb
52
+ - lib/lynx/pipe/get.rb
53
+ - lib/lynx/pipe/import.rb
54
+ - lib/lynx/pipe/run.rb
55
+ - lib/lynx/pipe/write.rb
56
+ - lib/lynx/version.rb
57
+ - lynx.gemspec
58
+ - test/lib/lynx/command/basic_test.rb
59
+ - test/lib/lynx/command/dump_test.rb
60
+ - test/lib/lynx/config_test.rb
61
+ - test/lib/lynx/d_s_l_test.rb
62
+ - test/lib/lynx/pipe/append_test.rb
63
+ - test/lib/lynx/pipe/get_test.rb
64
+ - test/lib/lynx/pipe/write_test.rb
65
+ homepage: ''
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.23
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: MySQL command line wrapper for Ruby.
89
+ test_files:
90
+ - test/lib/lynx/command/basic_test.rb
91
+ - test/lib/lynx/command/dump_test.rb
92
+ - test/lib/lynx/config_test.rb
93
+ - test/lib/lynx/d_s_l_test.rb
94
+ - test/lib/lynx/pipe/append_test.rb
95
+ - test/lib/lynx/pipe/get_test.rb
96
+ - test/lib/lynx/pipe/write_test.rb
97
+ has_rdoc: