self_consciousness 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 98ce3ddf4ac2662e71b68dafb212d7fb90004b44
4
+ data.tar.gz: 658f350c760e00295dc092377e77b91638f576d2
5
+ SHA512:
6
+ metadata.gz: 4ca2bee64d1e0649595ce5174b5db8c6d8b559826e5b3d09eae6b5c26f0e7744fe552f6aca5fedf6f689f0b909cd1e76239ba9fce734c3b09a616bedde380981
7
+ data.tar.gz: 1a9e67c3819187bdca561c57a6ee06780e4a38b38959998a44f0545d07696716c3a1ee3d8746d293938e55065efc42657a2f58ed08bd8674a927ccc3ff4d20ff
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .self_identity
2
+ .self_consciousness
3
+ Gemfile.lock
4
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Justin Scott
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # self_consciousness
2
+
3
+ A module on top of self_identity to store what's "normal" and how the code has changed since then.
4
+
5
+ Use `require 'self_identity'` in the script you want to be conscious of. Then have the script, or the script running it, call `SelfConsciousness.normalize`. Whenever that script calls `SelfConsciousness.report`, you'll get a diff of the method chain since you last normalized. You can also call `SelfConsciousness.introspect` to create the diff without reporting.
6
+
7
+ Confused? Check out the `example` directory.
8
+
9
+ If you don't know what the method chain is, check out [self_identity](https://github.com/colstrom/self_identity) first.
10
+
11
+ ## Data format:
12
+
13
+ Inside .self_consciousness you'll find the following key/value Moneta stores:
14
+
15
+ - `dependencies`: the method chain dependencies that are "normal"
16
+ - `additions`: the dependencies that have been added since normalizing
17
+ - `removals`: the dependencies that have been removed since normalizing
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rake'
2
+
3
+ task :test do
4
+ require_relative 'kintama/self_consciousness'
5
+ end
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ require 'yaml'
3
+ require 'commander/import'
4
+ require 'colorize'
5
+ require_relative '../lib/self_consciousness'
6
+
7
+ program :name, 'Self Consciousness'
8
+ program :version, '0.0.1'
9
+ program :description, 'Introspection and reporting on a program\'s functionality'
10
+ program :help, 'Author', 'Justin Scott <jvscott@gmail.com>'
11
+
12
+ command :normalize do |c|
13
+ c.syntax = 'self_consciousness normalize'
14
+ c.description = 'Creates an idea of what identity is "normal".'
15
+ c.action do
16
+ SelfConsciousness.normalize
17
+ puts "[#{'Success'.green}] The last run program now defines normal."
18
+ end
19
+ end
20
+
21
+ command :report do |c|
22
+ c.syntax = 'self_consciousness report'
23
+ c.description = 'Finds how the program deviates from "normal" and displays a method chain diff.'
24
+ c.action do
25
+ puts SelfConsciousness.report
26
+ end
27
+ end
data/example.rb ADDED
@@ -0,0 +1,25 @@
1
+ require_relative 'lib/self_consciousness'
2
+ require 'self_identity'
3
+
4
+ def foo(array)
5
+ array.push 1
6
+ end
7
+
8
+ def bar(array)
9
+ array.push 2
10
+ end
11
+
12
+ def baz(array)
13
+ array.push 3
14
+ end
15
+
16
+ if ARGV[0] == 'prototype'
17
+ var = foo []
18
+ var = bar var
19
+ else
20
+ var = bar []
21
+ var = baz var
22
+ end
23
+
24
+ SelfConsciousness.report
25
+ SelfConsciousness.normalize
data/example/README.md ADDED
@@ -0,0 +1,11 @@
1
+ `prototype.rb` and `finalized.rb` represent two products in the early and late stage of development. Over time they add and remove functionality and we'd like to track how that changed, perhaps via a Continuous Integration server.
2
+
3
+ First, run `prototype.rb`. It requires [self_identity](https://github.com/colstrom/self_identity) which will save its state to file. This is what we want to base future comparisons off of so use `self_consciousness normalize` to set that run as "normal".
4
+
5
+ Then run `finalized.rb`. It will overwrite the previous identity with its own automatically, just like the prototype did. Now run `self_consciousness report` and the method chain differences will be printed.
6
+
7
+ If you normalize and report again without running any new code, it will let you know that nothing has changed.
8
+
9
+ ## Warning
10
+
11
+ Make sure that you do this all from the same directory. Ruby will look to the current working directory to find the identity storages held in file.
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ require 'self_identity'
3
+
4
+ def foo(array)
5
+ array.push 1
6
+ end
7
+
8
+ def bar(array)
9
+ array.push 2
10
+ end
11
+
12
+ def baz(array)
13
+ puts 'Done!'
14
+ end
15
+
16
+ var = bar []
17
+ var = baz var
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ require 'self_identity'
3
+
4
+ def foo(array)
5
+ array.push 1
6
+ end
7
+
8
+ def bar(array)
9
+ puts 'done'
10
+ end
11
+
12
+ var = foo []
13
+ var = bar var
@@ -0,0 +1,55 @@
1
+ require 'kintama'
2
+ require 'yaml'
3
+ require 'moneta'
4
+ require 'colorize'
5
+ require_relative '../lib/self_consciousness'
6
+ require_relative 'stdout_helper'
7
+
8
+ def execute(path)
9
+ thread = Thread.new { system "ruby #{path}" }
10
+ thread.join
11
+ end
12
+
13
+ def run_and(method = :normalize)
14
+ script = method == :normalize ? 'test.rb' : 'test.rb differently'
15
+ execute "#{__dir__}/#{script}"
16
+ SelfConsciousness.send method
17
+ end
18
+
19
+ describe SelfConsciousness do
20
+ setup do
21
+ @storage = Moneta.new :File, dir: '.self_consciousness'
22
+ @expected_introspection_results = {
23
+ removals: [{:output=>[1], :from=>:foo, :to=>:bar}],
24
+ additions: [{:output=>[2], :from=>:bar, :to=>:baz}]
25
+ }
26
+ SelfConsciousness.clear
27
+ run_and :normalize
28
+ end
29
+
30
+ should 'decide what\'s "normal"' do
31
+ expected_dependencies = [{:output=>[1], :from=>:foo, :to=>:bar}]
32
+ assert_equal true, @storage.key?('dependencies')
33
+ assert_equal expected_dependencies, @storage.fetch('dependencies', nil)
34
+ end
35
+
36
+ should 'compare itself with its idea of "normal"' do
37
+ run_and :introspect
38
+ @expected_introspection_results.each do |key, value|
39
+ assert_equal value, @storage.fetch(key.to_s, nil)
40
+ end
41
+ end
42
+
43
+ should 'introspect before reporting' do
44
+ run_and :report
45
+ @expected_introspection_results.each do |key, value|
46
+ assert_equal value, @storage.fetch(key.to_s, nil)
47
+ end
48
+ end
49
+
50
+ should 'report a diff of additions and removals' do
51
+ assert_output /-.*+/ do
52
+ run_and :report
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,27 @@
1
+ module ::Kernel
2
+ def capture_stdout
3
+ out = StringIO.new
4
+ $stdout = out
5
+ yield
6
+ out.rewind
7
+ return out
8
+ ensure
9
+ $stdout = STDOUT
10
+ end
11
+
12
+ def silence_stdout
13
+ $stdout = StringIO.new
14
+ return yield
15
+ ensure
16
+ $stdout = STDOUT
17
+ end
18
+ end
19
+
20
+ def assert_output(expected, &block)
21
+ output = capture_stdout(&block).read
22
+ if expected.is_a?(Regexp)
23
+ assert_match expected, output
24
+ else
25
+ assert_equal expected, output
26
+ end
27
+ end
data/kintama/test.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'self_identity'
2
+
3
+ def foo(array)
4
+ array.push 1
5
+ end
6
+
7
+ def bar(array)
8
+ array.push 2
9
+ end
10
+
11
+ def baz(array)
12
+ array.push 3
13
+ end
14
+
15
+ if ARGV[0].nil?
16
+ var = foo []
17
+ var = bar var
18
+ else
19
+ var = bar []
20
+ var = baz var
21
+ end
@@ -0,0 +1,49 @@
1
+ require 'moneta'
2
+ require 'fileutils'
3
+ require 'colorize'
4
+
5
+ module SelfConsciousness
6
+ def self.without_self_identity
7
+ begin
8
+ previously_enabled = SelfIdentity.enabled?
9
+ SelfIdentity.disable
10
+ yield if block_given?
11
+ SelfIdentity.enable if previously_enabled
12
+ rescue NameError => e
13
+ yield if block_given?
14
+ end
15
+ end
16
+
17
+ def self.normalize
18
+ without_self_identity do
19
+ FileUtils.mkdir_p '.self_consciousness'
20
+ FileUtils.cp '.self_identity/dependencies', '.self_consciousness/dependencies'
21
+ end
22
+ end
23
+
24
+ def self.clear
25
+ without_self_identity do
26
+ conscious = Moneta.new :File, dir: '.self_consciousness'
27
+ conscious['dependencies'] = []
28
+ end
29
+ end
30
+
31
+ def self.introspect
32
+ without_self_identity do
33
+ latest = Moneta.new :File, dir: '.self_identity'
34
+ conscious = Moneta.new :File, dir: '.self_consciousness'
35
+ conscious['removals'] = conscious['dependencies'] - latest['dependencies']
36
+ conscious['additions'] = latest['dependencies'] - conscious['dependencies']
37
+ end
38
+ end
39
+
40
+ def self.report
41
+ without_self_identity do
42
+ introspect
43
+ conscious = Moneta.new :File, dir: '.self_consciousness'
44
+ conscious['removals'].each { |item| puts "- #{item.to_s}".colorize(:red) }
45
+ conscious['additions'].each { |item| puts "+ #{item.to_s}".colorize(:green) }
46
+ puts 'Nothing has changed' if conscious['removals'].concat(conscious['additions']).empty?
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = 'self_consciousness'
3
+ gem.version = '0.1.0'
4
+ gem.licenses = 'MIT'
5
+ gem.authors = ['Justin Scott']
6
+ gem.email = 'jvscott@gmail.com'
7
+ gem.homepage = 'http://www.github.com/jscott/self_consciousness/'
8
+ gem.summary = 'Self Consciousness'
9
+ gem.description = 'Allowing code to introspect and report on its own functionality.'
10
+
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- kintama/**/*`.split("\n")
13
+ gem.executables = `git ls-files -- bin/self_consciousness`.split("\n").map { |f| File.basename(f) }
14
+
15
+ gem.add_runtime_dependency 'colorize'
16
+ gem.add_runtime_dependency 'moneta'
17
+
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'kintama'
20
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: self_consciousness
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Scott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: moneta
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: kintama
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Allowing code to introspect and report on its own functionality.
70
+ email: jvscott@gmail.com
71
+ executables:
72
+ - self_consciousness
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - bin/self_consciousness
82
+ - example.rb
83
+ - example/README.md
84
+ - example/finalized.rb
85
+ - example/prototype.rb
86
+ - kintama/self_consciousness.rb
87
+ - kintama/stdout_helper.rb
88
+ - kintama/test.rb
89
+ - lib/self_consciousness.rb
90
+ - self_consciousness.gemspec
91
+ homepage: http://www.github.com/jscott/self_consciousness/
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.5
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Self Consciousness
115
+ test_files: []