diff --git a/lib/active_remote/attribute_methods.rb b/lib/active_remote/attribute_methods.rb index 8646288..cff6286 100644 --- a/lib/active_remote/attribute_methods.rb +++ b/lib/active_remote/attribute_methods.rb @@ -6,6 +6,12 @@ module ClassMethods def attribute_names @attribute_names ||= attribute_types.keys end + + attr_reader :filtered_attribute_names + + def filtered_attributes(attributes) + @filtered_attribute_names = attributes + end end def [](name) @@ -45,7 +51,7 @@ def attribute_for_inspect(attr_name) end def attribute_names - @attributes.keys + @attributes.keys.delete(self.class.filtered_attribute_names) || [] end end end diff --git a/lib/active_remote/base.rb b/lib/active_remote/base.rb index 6c5e688..bc1cddf 100644 --- a/lib/active_remote/base.rb +++ b/lib/active_remote/base.rb @@ -118,7 +118,7 @@ def inspect "not initialized" end - "#<#{self.class} #{inspection}>" + "#<#{[self.class, inspection.presence].compact.join(' ')}>" end # Returns a hash of the given methods with their names as keys and returned values as values. diff --git a/spec/lib/active_remote/base_spec.rb b/spec/lib/active_remote/base_spec.rb index 4611a70..7a65da9 100644 --- a/spec/lib/active_remote/base_spec.rb +++ b/spec/lib/active_remote/base_spec.rb @@ -7,4 +7,34 @@ described_class.new end end + + describe "filtered_attributes" do + describe "single attribute" do + let(:subject) { ::User.new(:password => "foobar") } + + it "#inspect doesn't show the password" do + expect(subject.inspect).not_to include("foobar") + end + + it "#inspect just equals the class name" do + expect(subject.inspect).to eq("#") + end + end + + describe "multiple attributes" do + let(:subject) { ::Author.new(:name => "foo", :age => 15) } + + it "#inspect doesn't show the name" do + expect(subject.inspect).not_to include("foo") + end + + it "#inspect doesn't show the birthday" do + expect(subject.inspect).not_to include("15") + end + + it "#inspect just equals the class name" do + expect(subject.inspect).to eq("#") + end + end + end end diff --git a/spec/support/models.rb b/spec/support/models.rb index acb99cb..919018f 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -5,3 +5,4 @@ require "support/models/no_attributes" require "support/models/post" require "support/models/tag" +require "support/models/user" diff --git a/spec/support/models/author.rb b/spec/support/models/author.rb index b14fe41..857f858 100644 --- a/spec/support/models/author.rb +++ b/spec/support/models/author.rb @@ -17,6 +17,8 @@ class Author < ::ActiveRemote::Base attribute :writes_fiction, :boolean attribute :net_sales, :float + filtered_attributes [:birthday, :age] + has_many :posts has_many :user_posts, :class_name => "::Post", :scope => :user_guid has_many :flagged_posts, :class_name => "::Post" diff --git a/spec/support/models/user.rb b/spec/support/models/user.rb new file mode 100644 index 0000000..a8ef304 --- /dev/null +++ b/spec/support/models/user.rb @@ -0,0 +1,10 @@ +require "support/protobuf/post.pb" + +## +# Define a generic class that inherits from active remote base +# +class User < ::ActiveRemote::Base + attribute :password, :string + + filtered_attributes :password +end diff --git a/spec/support/protobuf/user.pb.rb b/spec/support/protobuf/user.pb.rb new file mode 100644 index 0000000..53db9f0 --- /dev/null +++ b/spec/support/protobuf/user.pb.rb @@ -0,0 +1,32 @@ +# encoding: utf-8 + +## +# This file is auto-generated. DO NOT EDIT! +# +require 'protobuf' +require 'protobuf/rpc/service' + + +## +# Imports +# +require 'error.pb' + +module Generic + module Remote + ::Protobuf::Optionable.inject(self) { ::Google::Protobuf::FileOptions } + + ## + # Message Classes + # + class User < ::Protobuf::Message; end + + ## + # Message Fields + # + class User + optional :string, :password, 1 + optional :string, :password_digest, 2 + end + end +end