mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Update NoBrainer awesome prints
This commit is contained in:
parent
ee871db0c3
commit
06a4649bb5
9 changed files with 125 additions and 60 deletions
|
@ -11,3 +11,4 @@ gemfile:
|
|||
- gemfiles/rails_4.1.gemfile
|
||||
- gemfiles/rails_4.2.gemfile
|
||||
- gemfiles/mongoid_3.1.gemfile
|
||||
- gemfiles/nobrainer.gemfile
|
||||
|
|
|
@ -34,3 +34,7 @@ appraise 'ripple' do
|
|||
gem 'tzinfo'
|
||||
gem 'ripple'
|
||||
end
|
||||
|
||||
appraise 'nobrainer' do
|
||||
gem 'nobrainer'
|
||||
end
|
||||
|
|
7
gemfiles/nobrainer.gemfile
Normal file
7
gemfiles/nobrainer.gemfile
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file was generated by Appraisal
|
||||
|
||||
source "https://rubygems.org"
|
||||
|
||||
gem "nobrainer"
|
||||
|
||||
gemspec :path => "../"
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
49
lib/awesome_print/ext/nobrainer.rb
Normal file
49
lib/awesome_print/ext/nobrainer.rb
Normal file
|
@ -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)
|
57
spec/ext/nobrainer_spec.rb
Normal file
57
spec/ext/nobrainer_spec.rb
Normal file
|
@ -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
|
||||
#<SomeModel id: #{object_id}> {
|
||||
: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
|
|
@ -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'
|
||||
|
||||
|
|
|
@ -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|
|
||||
|
|
Loading…
Reference in a new issue