hisoka 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hisoka.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mark Burns
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.
@@ -0,0 +1,41 @@
1
+ # Hisoka
2
+
3
+ "Hisoka ni" means in secret. It is a simple spy library that can be used
4
+ in a Rails* app.
5
+
6
+ ```
7
+ ひそかに【密かに】
8
+ 【人に知られないように隠れて】secretly, in secret;
9
+ ```
10
+
11
+ Easily seeing what methods are called on an object
12
+ during normal usage means you can swap it out for an equivalent.
13
+
14
+ This project was created because of the need to replace some
15
+ calls to a database with an external service and not port across
16
+ unnecessary methods.
17
+
18
+ #Usage
19
+
20
+ ##Iterable spies
21
+ ```ruby
22
+ #old code
23
+ @users = User.all
24
+
25
+ #replaced with
26
+ @users = Hisoka::Iterable.new("all-users")
27
+ ```
28
+
29
+ #log output example
30
+
31
+ ```log
32
+ ```
33
+
34
+ #Note
35
+ It doesn't need to be Rails specific, but has been abstracted out of
36
+ being used in 2 Rails apps. Pull requests welcome to make it work
37
+ in a more generic way
38
+
39
+ ## Installation, contributions etc
40
+ All the usual bundler gem/github PR guidelines.
41
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hisoka/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hisoka"
8
+ spec.version = Hisoka::VERSION
9
+ spec.authors = ["Mark Burns"]
10
+ spec.email = ["markthedeveloper@gmail.com"]
11
+ spec.summary = %q{Simple spies for development}
12
+ spec.description = %q{Find out what objects in your app are doing}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+ spec.add_development_dependency "debugger"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
@@ -0,0 +1,11 @@
1
+ require "active_support/all"
2
+
3
+ require "hisoka/version"
4
+ require "hisoka/spy_stuff"
5
+ require "hisoka/basic"
6
+ require "hisoka/tryable"
7
+ require "hisoka/iterable"
8
+ require "hisoka/routable"
9
+
10
+ module Hisoka
11
+ end
@@ -0,0 +1,5 @@
1
+ module Hisoka
2
+ class Basic < BasicObject
3
+ include SpyStuff
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ module Hisoka
2
+ class Iterable < Tryable
3
+ Enumerable.instance_methods.each do |m|
4
+ define_method(m) do |*args|
5
+ called_method(m, *args)
6
+ end
7
+ end
8
+
9
+ def as_json(*args)
10
+ {spy_json: to_s}.to_json
11
+ end
12
+
13
+ def to_json(*args)
14
+ as_json
15
+ end
16
+
17
+ def to_ary(*args, &block)
18
+ if block_given?
19
+ each(&block).to_a
20
+ else
21
+ each{}.to_a
22
+ end
23
+ end
24
+
25
+ def each(&block)
26
+ called_method(:each)
27
+
28
+ result = called_method("block-inside-each")
29
+ block.call(result)
30
+ end
31
+
32
+ def to_s
33
+ "#{self.class.name} `#{@name}': #{@parent_method}"
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ module Hisoka
2
+ class Routable < Iterable
3
+ def id
4
+ "some-id"
5
+ end
6
+
7
+ def to_param
8
+ id
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,79 @@
1
+ module Hisoka
2
+ class ColorString < String
3
+ def blue; Hisoka.C("\033[34m#{self}\033[0m") end
4
+ def magenta; Hisoka.C("\033[35m#{self}\033[0m") end
5
+ def cyan; Hisoka.C("\033[36m#{self}\033[0m") end
6
+ def gray; Hisoka.C("\033[37m#{self}\033[0m") end
7
+ def bold; Hisoka.C("\033[1m#{self}\033[22m") end
8
+ def reverse_color; Hisoka.C("\033[7m#{self}\033[27m") end
9
+ end
10
+
11
+
12
+ def self.C(string, *methods)
13
+ color = ColorString.new(string)
14
+
15
+ methods.each do |m|
16
+ color = color.send(m)
17
+ end
18
+
19
+ color
20
+ end
21
+
22
+ class StdoutLogger
23
+ def info(*args)
24
+ $stdout.puts *args
25
+ end
26
+ def warn(*args)
27
+ $stdout.puts *args
28
+ end
29
+ def error(*args)
30
+ $stdout.puts *args
31
+ end
32
+ end
33
+ module SpyStuff
34
+ extend ActiveSupport::Concern
35
+
36
+ def initialize(name, logger=nil)
37
+ @name = name
38
+ @logger ||= (Rails.logger rescue StdoutLogger.new)
39
+ end
40
+
41
+ def parent_method=(m)
42
+ @parent_method = m
43
+ end
44
+
45
+ def new_child_node(name, parent_method=nil)
46
+ self.class.new(name).tap{|r|r.parent_method = parent_method}
47
+ end
48
+
49
+ def method_missing(method_name, *args)
50
+ called_method(method_name, *args)
51
+ end
52
+
53
+ def called_method(method_name, *args)
54
+ args = if args.empty?
55
+ ""
56
+ else
57
+ "with args #{args.to_s}"
58
+ end
59
+
60
+ call_info = caller[1..1][0].to_s.gsub(/:in `.*haml[\d|_]*'$/, "")
61
+ call_info.gsub!(/#{Rails.root}/, "") rescue nil
62
+
63
+ title = Hisoka.C("\nHisoka", :bold, :blue)
64
+
65
+ title = "#{title}: #{@name} ".ljust(50, " ")
66
+ meth = "#{@parent_method}.#{Hisoka.C(method_name.to_s,:cyan)} "
67
+ stack = "(called from #{call_info} )"
68
+
69
+ @logger.info "#{title} #{stack} #{meth} #{args}"
70
+
71
+ new_child_node(@name, [@parent_method, method_name].compact.join("."))
72
+ end
73
+
74
+ def to_s
75
+ "#{self.class.name} `#{@name}'"
76
+ end
77
+ end
78
+ end
79
+
@@ -0,0 +1,5 @@
1
+ module Hisoka
2
+ class Tryable
3
+ include SpyStuff
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Hisoka
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,74 @@
1
+ require "./lib/hisoka"
2
+
3
+ describe Hisoka::Routable do
4
+ let(:hisoka) { Hisoka::Routable.new "my spy"}
5
+
6
+ def expect_log(value)
7
+ l = lambda {|s|
8
+ if value.is_a?(Regexp)
9
+ s =~ value
10
+ else
11
+ s[value].present?
12
+ end
13
+ }
14
+
15
+ expect($stdout).to receive(:puts).
16
+ with(l), "expected #{value}"
17
+ end
18
+
19
+ it "logs the spy name" do
20
+ expect_log "my spy"
21
+ hisoka.doesnt_matter
22
+ end
23
+
24
+ it "is routable" do
25
+ expect(hisoka.to_param).to eq "some-id"
26
+ end
27
+
28
+ it "is convertible to json" do
29
+ expect_log "each"
30
+ expect_log "block-inside-each"
31
+
32
+ json = {"spy_json" => "Hisoka::Routable `my spy': block-inside-each"}
33
+
34
+ hisoka.each do |h|
35
+ expect(JSON.parse(h.to_json)).to eq json
36
+ end
37
+ end
38
+
39
+
40
+
41
+ it "logs messages called" do
42
+ expect_log /this_gets_called/
43
+
44
+ hisoka.this_gets_called
45
+ end
46
+
47
+ it "logs attributes" do
48
+ expect_log "an argument"
49
+
50
+ hisoka.called_with "an argument"
51
+ end
52
+
53
+ it "logs multiple calls" do
54
+ expect_log "first"
55
+ expect_log "second"
56
+
57
+ hisoka.first
58
+ hisoka.second
59
+ end
60
+
61
+ it "is iterable" do
62
+ expect_log "each"
63
+ expect_log "block-inside-each"
64
+ expect_log "inside_loop"
65
+ expect_log "another_inside_loop"
66
+ expect_log "with parameters"
67
+
68
+ hisoka.each do |h|
69
+ h.inside_loop
70
+ h.another_inside_loop
71
+ h.one_more "with parameters"
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,90 @@
1
+ require "debugger"
2
+ # This file was generated by the `rspec --init` command. Conventionally, all
3
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
5
+ # file to always be loaded, without a need to explicitly require it in any files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need it.
13
+ #
14
+ # The `.rspec` file also contains a few flags that are not defaults but that
15
+ # users commonly want.
16
+ #
17
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18
+ RSpec.configure do |config|
19
+ # rspec-expectations config goes here. You can use an alternate
20
+ # assertion/expectation library such as wrong or the stdlib/minitest
21
+ # assertions if you prefer.
22
+ config.expect_with :rspec do |expectations|
23
+ # This option will default to `true` in RSpec 4. It makes the `description`
24
+ # and `failure_message` of custom matchers include text for helper methods
25
+ # defined using `chain`, e.g.:
26
+ # be_bigger_than(2).and_smaller_than(4).description
27
+ # # => "be bigger than 2 and smaller than 4"
28
+ # ...rather than:
29
+ # # => "be bigger than 2"
30
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
31
+ end
32
+
33
+ # rspec-mocks config goes here. You can use an alternate test double
34
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
35
+ config.mock_with :rspec do |mocks|
36
+ # Prevents you from mocking or stubbing a method that does not exist on
37
+ # a real object. This is generally recommended, and will default to
38
+ # `true` in RSpec 4.
39
+ mocks.verify_partial_doubles = true
40
+ end
41
+
42
+ # The settings below are suggested to provide a good initial experience
43
+ # with RSpec, but feel free to customize to your heart's content.
44
+ =begin
45
+ # These two settings work together to allow you to limit a spec run
46
+ # to individual examples or groups you care about by tagging them with
47
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
48
+ # get run.
49
+ config.filter_run :focus
50
+ config.run_all_when_everything_filtered = true
51
+
52
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
53
+ # For more details, see:
54
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
55
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
56
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
57
+ config.disable_monkey_patching!
58
+
59
+ # This setting enables warnings. It's recommended, but in some cases may
60
+ # be too noisy due to issues in dependencies.
61
+ config.warnings = true
62
+
63
+ # Many RSpec users commonly either run the entire suite or an individual
64
+ # file, and it's useful to allow more verbose output when running an
65
+ # individual spec file.
66
+ if config.files_to_run.one?
67
+ # Use the documentation formatter for detailed output,
68
+ # unless a formatter has already been configured
69
+ # (e.g. via a command-line flag).
70
+ config.default_formatter = 'doc'
71
+ end
72
+
73
+ # Print the 10 slowest examples and example groups at the
74
+ # end of the spec run, to help surface which specs are running
75
+ # particularly slow.
76
+ config.profile_examples = 10
77
+
78
+ # Run specs in random order to surface order dependencies. If you find an
79
+ # order dependency and want to debug it, you can fix the order by providing
80
+ # the seed, which is printed after each run.
81
+ # --seed 1234
82
+ config.order = :random
83
+
84
+ # Seed global randomization in this process using the `--seed` CLI option.
85
+ # Setting this allows you to use `--seed` to deterministically reproduce
86
+ # test failures related to randomization by passing the same `--seed` value
87
+ # as the one that triggered the failure.
88
+ Kernel.srand config.seed
89
+ =end
90
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hisoka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mark Burns
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-12-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
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
+ - !ruby/object:Gem::Dependency
31
+ name: debugger
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.7'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.7'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '10.0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '10.0'
94
+ description: Find out what objects in your app are doing
95
+ email:
96
+ - markthedeveloper@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - hisoka.gemspec
108
+ - lib/hisoka.rb
109
+ - lib/hisoka/basic.rb
110
+ - lib/hisoka/iterable.rb
111
+ - lib/hisoka/routable.rb
112
+ - lib/hisoka/spy_stuff.rb
113
+ - lib/hisoka/tryable.rb
114
+ - lib/hisoka/version.rb
115
+ - spec/integration_spec.rb
116
+ - spec/spec_helper.rb
117
+ homepage: ''
118
+ licenses:
119
+ - MIT
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 1.8.23.2
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: Simple spies for development
142
+ test_files:
143
+ - spec/integration_spec.rb
144
+ - spec/spec_helper.rb