diff --git a/.travis.yml b/.travis.yml index 26d333f..08d27fb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,3 +11,4 @@ gemfile: - gemfiles/rails_4.1.gemfile - gemfiles/rails_4.2.gemfile - gemfiles/mongoid_3.1.gemfile + - gemfiles/nobrainer.gemfile diff --git a/Appraisals b/Appraisals index 1fa9966..7024697 100644 --- a/Appraisals +++ b/Appraisals @@ -34,3 +34,7 @@ appraise 'ripple' do gem 'tzinfo' gem 'ripple' end + +appraise 'nobrainer' do + gem 'nobrainer' +end diff --git a/gemfiles/nobrainer.gemfile b/gemfiles/nobrainer.gemfile new file mode 100644 index 0000000..23b0ee5 --- /dev/null +++ b/gemfiles/nobrainer.gemfile @@ -0,0 +1,7 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "nobrainer" + +gemspec :path => "../" diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index e30a2d4..ab4a669 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -33,7 +33,7 @@ unless defined?(AwesomePrint::Inspector) require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper) require File.dirname(__FILE__) + "/awesome_print/ext/mongoid" if defined?(Mongoid) require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri) - require File.dirname(__FILE__) + "/awesome_print/ext/no_brainer" if defined?(NoBrainer) + require File.dirname(__FILE__) + "/awesome_print/ext/nobrainer" if defined?(NoBrainer) require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple) require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel) require File.dirname(__FILE__) + "/awesome_print/ext/ostruct" if defined?(OpenStruct) diff --git a/lib/awesome_print/ext/no_brainer.rb b/lib/awesome_print/ext/no_brainer.rb deleted file mode 100644 index 431509d..0000000 --- a/lib/awesome_print/ext/no_brainer.rb +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright (c) 2010-2013 Michael Dvorkin -# -# Awesome Print is freely distributable under the terms of MIT license. -# See LICENSE file or http://www.opensource.org/licenses/mit-license.php -#------------------------------------------------------------------------------ -module AwesomePrint - module NoBrainer - - def self.included(base) - base.send :alias_method, :cast_without_no_brainer, :cast - base.send :alias_method, :cast, :cast_with_no_brainer - end - - # Add NoBrainer class names to the dispatcher pipeline. - #------------------------------------------------------------------------------ - def cast_with_no_brainer(object, type) - cast = cast_without_no_brainer(object, type) - if defined?(::NoBrainer::Document) - if object.is_a?(Class) && object.ancestors.include?(::NoBrainer::Document) - cast = :no_brainer_class - elsif object.class.ancestors.include?(::NoBrainer::Document) - cast = :no_brainer_document - end - end - cast - end - - # Format NoBrainer class object. - #------------------------------------------------------------------------------ - def awesome_no_brainer_class(object) - return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields) - - # We want id first - data = object.fields.sort_by { |key| key[0] == :id ? '_id' : key[0].to_s }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| - hash[c[0]] = :object - hash - end - "class #{object} < #{object.superclass} " << awesome_hash(data) - end - - # Format NoBrainer Document object. - #------------------------------------------------------------------------------ - def awesome_no_brainer_document(object) - return object.inspect if !defined?(::ActiveSupport::OrderedHash) - - data = object.attributes.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| - hash[c[0].to_sym] = c[1] - hash - end - if !object.errors.empty? - data = {:errors => object.errors, :attributes => data} - end - "#{object} #{awesome_hash(data)}" - end - end -end - -AwesomePrint::Formatter.send(:include, AwesomePrint::NoBrainer) diff --git a/lib/awesome_print/ext/nobrainer.rb b/lib/awesome_print/ext/nobrainer.rb new file mode 100644 index 0000000..d13b906 --- /dev/null +++ b/lib/awesome_print/ext/nobrainer.rb @@ -0,0 +1,49 @@ +# Copyright (c) 2010-2013 Michael Dvorkin +# +# Awesome Print is freely distributable under the terms of MIT license. +# See LICENSE file or http://www.opensource.org/licenses/mit-license.php +#------------------------------------------------------------------------------ +module AwesomePrint + module NoBrainer + + def self.included(base) + base.send :alias_method, :cast_without_nobrainer, :cast + base.send :alias_method, :cast, :cast_with_nobrainer + end + + # Add NoBrainer class names to the dispatcher pipeline. + #------------------------------------------------------------------------------ + def cast_with_nobrainer(object, type) + cast = cast_without_nobrainer(object, type) + if defined?(::NoBrainer::Document) + if object.is_a?(Class) && object < ::NoBrainer::Document + cast = :nobrainer_class + elsif object.is_a?(::NoBrainer::Document) + cast = :nobrainer_document + end + end + cast + end + + # Format NoBrainer class object. + #------------------------------------------------------------------------------ + def awesome_nobrainer_class(object) + data = Hash[object.fields.map do |field, options| + [field, (options[:type] || Object).to_s.underscore.to_sym] + end] + "class #{object} < #{object.superclass} " << awesome_hash(data) + end + + # Format NoBrainer Document object. + #------------------------------------------------------------------------------ + def awesome_nobrainer_document(object) + data = object.inspectable_attributes.symbolize_keys + if object.errors.present? + data = {:errors => object.errors, :attributes => data} + end + "#{object} #{awesome_hash(data)}" + end + end +end + +AwesomePrint::Formatter.send(:include, AwesomePrint::NoBrainer) diff --git a/spec/ext/nobrainer_spec.rb b/spec/ext/nobrainer_spec.rb new file mode 100644 index 0000000..bd1e470 --- /dev/null +++ b/spec/ext/nobrainer_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +RSpec.describe "AwesomePrint/NoBrainer", skip: ->{ !ExtVerifier.has_nobrainer? }.call do + + if ExtVerifier.has_nobrainer? + before :all do + NoBrainer.configure + end + + before :all do + class SomeModel + include NoBrainer::Document + + field :first_name, :type => String + field :last_name, :type => String + field :some_field + end + end + + after :all do + Object.instance_eval{ remove_const :SomeModel } + end + end + + before do + stub_dotfile! + @ap = AwesomePrint::Inspector.new :plain => true + end + + it "should print class instance" do + user = SomeModel.new :first_name => "Al", :last_name => "Capone" + out = @ap.send :awesome, user + + object_id = user.id.inspect + str = <<-EOS.strip +# { + :id => #{object_id}, + :first_name => "Al", + :last_name => "Capone" +} + EOS + expect(out).to eq(str) + end + + it "should print the class" do + class_spec = <<-EOS.strip +class SomeModel < Object { + :id => :string, + :first_name => :string, + :last_name => :string, + :some_field => :object +} + EOS + + expect(@ap.send(:awesome, SomeModel)).to eq class_spec + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9107e9a..f34dbc8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -22,7 +22,7 @@ Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each do |file| end ExtVerifier.require_dependencies!(%w{rails active_record action_view - active_support/all mongoid mongo_mapper ripple}) + active_support/all mongoid mongo_mapper ripple nobrainer}) require 'nokogiri' require 'awesome_print' diff --git a/spec/support/ext_verifier.rb b/spec/support/ext_verifier.rb index a41b1ac..36597ca 100644 --- a/spec/support/ext_verifier.rb +++ b/spec/support/ext_verifier.rb @@ -29,6 +29,11 @@ module ExtVerifier defined?(Ripple) end module_function :has_ripple? + + def has_nobrainer? + defined?(NoBrainer) + end + module_function :has_nobrainer? end RSpec.configure do |config|