Giter Club home page Giter Club logo

te_aro's Introduction

Te Aro ActiveRecordObserver

Designed to help with understanding how a particular block of code changes your ActiveRecord objects. See what gets changed, what type of objects are created, and what the values on those objects are. It can also display the sequence of callbacks that was triggered.

Installation

Add this line to your application's Gemfile:

gem 'te_aro'

And then execute:

$ bundle

Or install it yourself as:

$ gem install te_aro

Usage

# With defaults:
TeAro::Observer.new.observe { @some_ar_object.do_something_that_triggers_callbacks }

# With the tracer enabled (slow):
TeAro::Observer.new(tracer: true).observe { @some_ar_object.do_something_that_triggers_callbacks }

# Can also use the Kernel#aro method to call with defaults:
aro { @some_ar_object.do_something }

Sample Session

Te Aro in action on gitlab code:

# gitlab-development-kit/gitlab/spec/services/merge_requests/create_service_spec.rb
      # ...
      before do
        project.team << [user, :master]
        project.team << [assignee, :developer]
        allow(service).to receive(:execute_hooks)

        aro { @merge_request = service.execute }
      end
      # ...

Run:

$ bundle exec rspec spec/services/merge_requests/create_service_spec.rb -fd

Output in log/te_aro.log:

Object Count Changes:
        GitlabIssueTrackerService: +1
        MergeRequest: +1
        MergeRequestDiff: +1
New ActiveRecord objects:
        GitlabIssueTrackerService (id=1)
                id: 1
                type: GitlabIssueTrackerService
                project_id: 1
                created_at: 2016-03-21 07:17:46 UTC
                updated_at: 2016-03-21 07:17:46 UTC
                active: false
                properties: {}
                template: false
                push_events: true
                issues_events: true
                merge_requests_events: true
                tag_push_events: true
                note_events: true
                build_events: true
                category: issue_tracker
                default: true
        MergeRequest (id=1)
                id: 1
                target_branch: master
                source_branch: feature
                source_project_id: 1
                author_id: 3
                title: Awesome merge_request
                created_at: 2016-03-21 07:17:46 UTC
                updated_at: 2016-03-21 07:17:46 UTC
                state: opened
                merge_status: unchecked
                target_project_id: 1
                iid: 1
                description: please fix
                position: 0
                merge_params: {}
                merge_when_build_succeeds: false
        MergeRequestDiff (id=1)
                id: 1
                state: collected
                st_commits: [{:id=>"0b4bc9a49b562e85de7cc9e834518ea6828729b9", :message=>"Feature added\n\nSigned-off-by: Dmitriy Zaporozhets <[email protected]>\n", :parent_ids=>["ae73cb07c9eeaf35924a10f713b364d32b2dd34f"], :authored_date=>2014-02-27 21:26:01 +1300, :author_name=>"Dmitriy Zaporozhets", :author_email=>"[email protected]", :committed_date=>2014-02-27 21:26:01 +1300, :committer_name=>"Dmitriy Zaporozhets", :committer_email=>"[email protected]"}]
                st_diffs: [{:diff=>"--- /dev/null\n+++ b/files/ruby/feature.rb\n@@ -0,0 +1,5 @@\n+class Feature\n+  def foo\n+    puts 'bar'\n+  end\n+end\n", :new_path=>"files/ruby/feature.rb", :old_path=>"files/ruby/feature.rb", :a_mode=>"0", :b_mode=>"100644", :new_file=>true, :renamed_file=>false, :deleted_file=>false, :too_large=>false}]
                merge_request_id: 1
                created_at: 2016-03-21 07:17:46 UTC
                updated_at: 2016-03-21 07:17:46 UTC
                base_commit_sha: ae73cb07c9eeaf35924a10f713b364d32b2dd34f
                real_size: 1
Changed and persisted ActiveRecord objects:
        Project (id=1)
                last_activity_at: 2016-03-21 07:17:45 UTC -> 2016-03-21 07:17:46 UTC

Output

By default, output is logged to log/te_aro.log. This can be changed by passing a Logger instance when constructing the observer.

Eg to log to STDOUT:

TeAro::Observer.new(logger: Logger.new(STDOUT)).observe { some_ar_object.do_something }

Options

Options are passed as a hash to Observer.new.

The following options are available:

  • :tracer Show the ActiveRecord callbacks that were called. A truthy value will turn this on, it is off by default
  • :tracker Show ActiveRecord objects that have been created or changed. A falsey value will turn this off. On by default.
  • :logger Sets the logger used to record output.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/powershop/te_aro.

License

The gem is available as open source under the terms of the MIT License.

te_aro's People

Contributors

pmvenegas avatar mkrfowler avatar mkeating avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.