telepath 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e34c44c95862221a4d1cfb6bdf316d68ee622b2e
4
- data.tar.gz: 3754468fd1ecdfcc09f2fd4b3c3ddb817386b26e
3
+ metadata.gz: 7e69b7082bfdff850dd491c0b7afbd3f37eb611d
4
+ data.tar.gz: 9dcc58cf0842de030bba29f97a399df0a2c54387
5
5
  SHA512:
6
- metadata.gz: 0526648fba0e0336350967b4bf1d33c062c934efe718658698c6f9c6c3a1fccd83474fde450d5d90e78ea9626ee966291f060ab74fc06c9001f6274202eb88aa
7
- data.tar.gz: b613d99f4e5096bc84f1d6f703c50d9e54175dabc2548f01f0150f9d3030a492b1bb9c5080371bc649abe99323995433ff1a96e5330a55d6c0a9301009d21863
6
+ metadata.gz: c756e27d743378158c557de22d20d2e64f8795bfe7a0013e5cf1eba0a3725b96f9ce7fac9c7d137d7046104147a8298e021387ee49149e5b010f41a21867a34e
7
+ data.tar.gz: 4a0c542a0f0190460e30125579e24eda0f843b4a8813cbf9c78f2dbc4fab940bf66d5f085f566e6353a7e812b3ba6a6d732988729afe6430c73194b1efac4599
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ *.db
3
4
  .bundle
4
5
  .config
5
6
  .yardoc
data/bin/tel ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/telepath'
4
+
5
+ Telepath::Command.run
6
+
@@ -0,0 +1,43 @@
1
+ require 'clamp'
2
+
3
+ module Telepath
4
+ class Command < ::Clamp::Command
5
+
6
+ option ['-q', '--quiet'], :flag, "Only output when absolutely necessary."
7
+
8
+ subcommand ['+'], 'Add item to Telepath' do
9
+ parameter 'VALUE', 'value to add'
10
+
11
+ def execute
12
+ handler = Telepath::Handler.new self
13
+ success, name = handler.add value
14
+
15
+ if success then
16
+ Out.info "Added `#{value}' to `#{name}'!"
17
+ else
18
+ Out.error self, "Could not add `#{value}' to `#{name}'!"
19
+ end
20
+ end
21
+ end
22
+
23
+ subcommand ['='], 'Grab item from Telepath' do
24
+ parameter '[PATTERN]', 'pattern to find'
25
+
26
+ def execute
27
+ handler = Telepath::Handler.new self
28
+ value = handler.grab pattern
29
+
30
+ if value && !value.empty? then
31
+ Out.data value
32
+ else
33
+ Out.error self, "Pattern `#{pattern}' not matched."
34
+ end
35
+ end
36
+ end
37
+
38
+ def execute
39
+ exit 1
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,56 @@
1
+ module Telepath
2
+ class Handler
3
+ def initialize command
4
+ @command = command
5
+ end
6
+
7
+ def add value
8
+ name = 'stack'
9
+
10
+ with_store name do |container|
11
+ container << value
12
+ end
13
+
14
+ [true, name]
15
+ end
16
+
17
+ def grab pattern = nil
18
+ with_store 'stack' do |container|
19
+
20
+ if pattern && !pattern.empty? then
21
+ if container.include? pattern.to_s then
22
+ pattern.to_s
23
+ else
24
+ container.find do |element|
25
+ /#{pattern}/ =~ element.to_s
26
+ end
27
+ end
28
+ else
29
+ container.last
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ def storage
36
+ if @storage && @storage.ready? then
37
+ @storage
38
+ else
39
+ @storage = Telepath::Storage.new
40
+ end
41
+ end
42
+
43
+ protected
44
+
45
+ def with_store name = 'stack'
46
+ container = storage.store[name] || Array.new
47
+
48
+ result = yield container
49
+
50
+ storage.store[name] = container
51
+ result
52
+ ensure
53
+ storage.store.close
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,18 @@
1
+ module Telepath
2
+ module Out
3
+ module_function
4
+
5
+ def info *messages
6
+ puts messages.flatten.join ' '
7
+ end
8
+
9
+ def data *messages
10
+ puts messages.flatten
11
+ end
12
+
13
+ def error command, *messages
14
+ raise Clamp::UsageError.new(messages.flatten.join("\n"), command)
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,64 @@
1
+ require 'moneta'
2
+ require 'daybreak'
3
+
4
+ module Telepath
5
+ class Storage
6
+
7
+ DEFAULT_PATH = '~'
8
+ DEFAULT_FILE = '.telepath.db'
9
+ DEFAULT_TYPE = :Daybreak
10
+
11
+ def store
12
+ @store ||= create_store
13
+ end
14
+
15
+ def stack
16
+ store['stack'] || Array.new
17
+ end
18
+
19
+ def ready?
20
+ !store.adapter.backend.closed?
21
+ end
22
+
23
+ ### -----
24
+
25
+ def location
26
+ if path.exist? then
27
+ path.join file
28
+ else
29
+ raise <<-ERRMSG
30
+ Create or change the storage path for Telepath.
31
+ Current: `#{path}'
32
+ Environment Variable Name: `#{PATH_VAR}'
33
+ ERRMSG
34
+ end
35
+ end
36
+
37
+ def path
38
+ @path ||= Pathname.new(get :path).expand_path
39
+ end
40
+
41
+ def file
42
+ @file ||= get :file
43
+ end
44
+
45
+ def type
46
+ @type ||= get :type
47
+ end
48
+
49
+ protected
50
+
51
+ def create_store
52
+ Moneta.new type, file: location
53
+ end
54
+
55
+ def get name
56
+ env_name = "TELEPATH_#{name.to_s.upcase}"
57
+ const_name = "DEFAULT_#{name.to_s.upcase}"
58
+ value = ENV[env_name]
59
+
60
+ value && !value.empty? && value || self.class.const_get(const_name)
61
+ end
62
+
63
+ end
64
+ end
@@ -1,3 +1,3 @@
1
1
  module Telepath
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/telepath.rb CHANGED
@@ -3,7 +3,10 @@ require 'pathname'
3
3
  require 'bundler/setup'
4
4
 
5
5
  require 'telepath/version'
6
- require 'telepath/store'
6
+ require 'telepath/out'
7
+ require 'telepath/storage'
8
+ require 'telepath/handler'
9
+ require 'telepath/command'
7
10
 
8
11
  module Telepath
9
12
  def self.root
data/spec/exe_spec.rb ADDED
@@ -0,0 +1,71 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe 'Telepath Executable' do
4
+ let(:exe){ run "./bin/tel #{command} #{args.join ' '}" }
5
+ let(:status){ exe.status }
6
+ let(:stdout){ exe.stdout.strip }
7
+
8
+ # override these in context
9
+ let(:command){ '' }
10
+ let(:args){ [] }
11
+
12
+ before do
13
+ pre_test_setup
14
+ end
15
+
16
+ after do
17
+ post_test_teardown
18
+ end
19
+
20
+ it 'runs' do
21
+ expect(status).to be_success
22
+ end
23
+
24
+ context 'invalid arguments' do
25
+ let(:args){ ['invalid_argument'] }
26
+ specify { expect(status).to_not be_success }
27
+ end
28
+
29
+ describe '+' do
30
+ let(:command){ :+ }
31
+
32
+ context 'without value' do
33
+ specify { expect(status).to_not be_success }
34
+ end
35
+
36
+ context 'with value' do
37
+ let(:args){ ['12'] }
38
+
39
+ specify { expect(status).to be_success }
40
+
41
+ it 'adds the value to the stack' do
42
+ expect(stdout).to eq("Added `12' to `stack'!")
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '=' do
48
+ let(:command){ '=' }
49
+ let(:handler){ Telepath::Handler.new double(Clamp::Command) }
50
+ let(:value){ '12' }
51
+
52
+ before do
53
+ handler.add value
54
+ end
55
+
56
+ context 'without parameters' do
57
+ context 'with values in telepath' do
58
+
59
+ specify { expect(status).to be_success }
60
+ specify { expect(stdout).to eq value }
61
+ end
62
+ end
63
+
64
+ context 'with parameters' do
65
+ let(:args){ ['1'] }
66
+
67
+ specify { expect(status).to be_success }
68
+ specify { expect(stdout).to eq value }
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,64 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Telepath::Handler do
4
+ before { pre_test_setup }
5
+ after { post_test_teardown }
6
+
7
+ subject(:handler){ described_class.new double(Clamp::Command) }
8
+
9
+ let(:storage){ Telepath::Storage.new }
10
+
11
+ it 'exists' do
12
+ expect(described_class).to be
13
+ end
14
+
15
+ after do
16
+ storage.store.close unless storage.store.adapter.backend.closed?
17
+ end
18
+
19
+ describe '#add' do
20
+ it 'adds 1 item' do
21
+ expect{ handler.add 'whatever' }.to change{
22
+ storage.store.adapter.backend.sunrise
23
+ storage.stack.length
24
+ }.from(0).to(1)
25
+ end
26
+
27
+ it 'adds the right item' do
28
+ handler.add 'whatever'
29
+ expect(storage.stack).to include('whatever')
30
+ end
31
+ end
32
+
33
+ describe '#grab' do
34
+ let(:value){ 'whatever' }
35
+ before do
36
+ handler.add value
37
+ end
38
+
39
+ it 'grabs last value' do
40
+ expect(handler.grab).to eq(value)
41
+ end
42
+
43
+ it 'does not delete the item' do
44
+ expect{ handler.grab value }.to change{
45
+ storage.store.adapter.backend.sunrise
46
+ storage.stack.length
47
+ }.by(0)
48
+ end
49
+
50
+ context 'pattern matching' do
51
+ it 'matches substrings of words' do
52
+ expect(handler.grab('what')).to eq(value)
53
+ end
54
+
55
+ context 'numbers' do
56
+ let(:value){ '12' }
57
+
58
+ it 'matches parts of numbers' do
59
+ expect(handler.grab('1')).to eq(value)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,35 @@
1
1
  require_relative '../lib/telepath'
2
2
 
3
3
  require 'rspec'
4
+ require 'open4'
4
5
  require 'pry' unless ENV['CI'] == 'true'
5
6
 
6
7
  Dir.chdir Telepath.root
7
8
 
9
+ module SpecHelpers
10
+ def run *args
11
+ result_class = Struct.new :pid, :stdout, :stderr, :commandline, :status
12
+ command = args.join ' '
13
+
14
+ result = nil
15
+ status = Open4.open4(command) do |pid, stdin, stdout, stderr|
16
+ result = result_class.new pid, stdout.read, stderr.read, command
17
+ end
18
+
19
+ result.status = status
20
+ result
21
+ end
22
+
23
+ def path_env_var; 'TELEPATH_PATH'; end
24
+ def test_path; Telepath.root.join 'tmp'; end
25
+ def test_file; test_path.join Telepath::Storage.new.file; end
26
+ def cleanup_db; test_file.delete if test_file.exist?; end
27
+ def assign_test_path; ENV[path_env_var] = test_path.to_s; end
28
+ def unassign_test_path; ENV[path_env_var] = nil; end
29
+ def pre_test_setup; assign_test_path; cleanup_db; end
30
+ def post_test_teardown; unassign_test_path; cleanup_db; end
31
+ end
32
+
33
+ RSpec.configure do |c|
34
+ c.include SpecHelpers
35
+ end
@@ -0,0 +1,73 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Telepath::Storage do
4
+ subject(:storage){ described_class.new }
5
+ let(:default_file){ '.telepath.db' }
6
+ let(:default_path){ File.expand_path '~' }
7
+ let(:default_location){ "#{default_path}/#{default_file}" }
8
+
9
+ it 'exists' do
10
+ expect(described_class).to be
11
+ end
12
+
13
+ context 'with default location' do
14
+ describe '#file' do
15
+ subject(:file){ storage.file }
16
+
17
+ it 'matches defaults' do
18
+ expect(file.to_s).to eq default_file
19
+ end
20
+ end
21
+
22
+ describe '#path' do
23
+ subject(:path){ storage.path }
24
+
25
+ it 'defaults to home directory' do
26
+ expect(path.to_s).to eq default_path
27
+ end
28
+ end
29
+
30
+ describe '#location' do
31
+ subject(:path){ storage.location }
32
+
33
+ it 'defaults to a combination of the path and file' do
34
+ expect(path.to_s).to eq default_location
35
+ end
36
+
37
+ context 'invalid path' do
38
+ before do
39
+ ENV[path_env_var] = '/this/path/doesnt/exist/hopefully'
40
+ end
41
+
42
+ after do
43
+ ENV[path_env_var] = nil
44
+ end
45
+
46
+ specify { expect{path}.to raise_exception }
47
+ end
48
+ end
49
+ end
50
+
51
+ context 'with test location' do
52
+
53
+ before :all do
54
+ ENV[path_env_var] = test_path.to_s
55
+ test_file.delete if test_file.exist?
56
+ end
57
+
58
+ after :all do
59
+ ENV[path_env_var] = nil
60
+ test_file.delete if test_file.exist?
61
+ end
62
+
63
+ describe '.new' do
64
+ before do
65
+ Moneta.stub(:new) { 'test object' }
66
+ end
67
+
68
+ it 'creates a store object using Moneta' do
69
+ expect(storage.store).to eq 'test object'
70
+ end
71
+ end
72
+ end
73
+ end
data/telepath.gemspec CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency 'daybreak'
24
24
 
25
25
  # commandline
26
- #spec.add_dependency 'clamp'
26
+ spec.add_dependency 'clamp'
27
27
  #spec.add_dependency 'highline'
28
28
 
29
29
  # development
@@ -34,6 +34,6 @@ Gem::Specification.new do |spec|
34
34
  spec.add_development_dependency 'pry-theme'
35
35
  # testing
36
36
  spec.add_development_dependency 'rspec', '~> 2'
37
- #spec.add_development_dependency 'bogus'
37
+ spec.add_development_dependency 'open4'
38
38
  #spec.add_development_dependency 'ffaker'
39
39
  end
data/tmp/.gitkeep ADDED
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telepath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Cook
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-31 00:00:00.000000000 Z
11
+ date: 2013-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: moneta
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: clamp
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -108,10 +122,25 @@ dependencies:
108
122
  - - ~>
109
123
  - !ruby/object:Gem::Version
110
124
  version: '2'
125
+ - !ruby/object:Gem::Dependency
126
+ name: open4
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
111
139
  description: Sorta like IPC for people to GTD.
112
140
  email:
113
141
  - anthonymichaelcook@gmail.com
114
- executables: []
142
+ executables:
143
+ - tel
115
144
  extensions: []
116
145
  extra_rdoc_files: []
117
146
  files:
@@ -123,14 +152,20 @@ files:
123
152
  - LICENSE.txt
124
153
  - README.markdown
125
154
  - Rakefile
155
+ - bin/tel
126
156
  - lib/telepath.rb
127
- - lib/telepath/store.rb
157
+ - lib/telepath/command.rb
158
+ - lib/telepath/handler.rb
159
+ - lib/telepath/out.rb
160
+ - lib/telepath/storage.rb
128
161
  - lib/telepath/version.rb
162
+ - spec/exe_spec.rb
163
+ - spec/handler_spec.rb
129
164
  - spec/spec_helper.rb
130
- - spec/store_spec.rb
165
+ - spec/storage_spec.rb
131
166
  - spec/telepath_spec.rb
132
167
  - telepath.gemspec
133
- - test.db
168
+ - tmp/.gitkeep
134
169
  homepage: http://github.com/acook/telepath#readme
135
170
  licenses:
136
171
  - MIT
@@ -156,6 +191,8 @@ signing_key:
156
191
  specification_version: 4
157
192
  summary: Spooky action at a distance.
158
193
  test_files:
194
+ - spec/exe_spec.rb
195
+ - spec/handler_spec.rb
159
196
  - spec/spec_helper.rb
160
- - spec/store_spec.rb
197
+ - spec/storage_spec.rb
161
198
  - spec/telepath_spec.rb
@@ -1,35 +0,0 @@
1
- require 'moneta'
2
- require 'daybreak'
3
-
4
- module Telepath
5
- class Store
6
-
7
- class << self
8
- def path
9
- Pathname.new('~/').expand_path
10
- end
11
-
12
- def file
13
- '.telepath.db'
14
- end
15
-
16
- def location
17
- path.join file
18
- end
19
-
20
-
21
- def create
22
- new Moneta.new :Daybreak, file: location
23
- end
24
- end
25
-
26
- def initialize store
27
- @store = store
28
- end
29
-
30
- protected
31
-
32
- attr_accessor :store
33
-
34
- end
35
- end
data/spec/store_spec.rb DELETED
@@ -1,46 +0,0 @@
1
- require_relative 'spec_helper'
2
-
3
- describe Telepath::Store do
4
- it 'exists' do
5
- expect(Telepath::Store).to be
6
- end
7
-
8
- let(:default_file){ '.telepath.db' }
9
- let(:default_path){ File.expand_path '~' }
10
- let(:default_location){ "#{default_path}/#{default_file}" }
11
-
12
- describe '.file' do
13
- subject(:file){ described_class.file }
14
-
15
- it 'matches defaults' do
16
- expect(file.to_s).to eq default_file
17
- end
18
- end
19
-
20
- describe '.path' do
21
- subject(:path){ described_class.path }
22
-
23
- it 'defaults to home directory' do
24
- expect(path.to_s).to eq default_path
25
- end
26
- end
27
-
28
- describe '.location' do
29
- subject(:path){ described_class.location }
30
-
31
- it 'defaults to a combination of the path and file' do
32
- expect(path.to_s).to eq default_location
33
- end
34
- end
35
-
36
- describe '.create' do
37
- it 'relays the new data store to new' do
38
- expect(described_class).to receive(:new).with anything
39
- described_class.create
40
- end
41
-
42
- it 'assigns the store object' do
43
- expect(described_class.create.send(:store)).to be
44
- end
45
- end
46
- end
data/test.db DELETED
Binary file