protobuf-activerecord 1.2.6 → 2.0.0.beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +0 -4
 - data/lib/protobuf/activerecord/protoable.rb +4 -4
 - data/lib/protobuf/activerecord/protoable/columns.rb +56 -0
 - data/lib/protobuf/activerecord/protoable/errors.rb +0 -10
 - data/lib/protobuf/activerecord/protoable/persistence.rb +0 -91
 - data/lib/protobuf/activerecord/protoable/scope.rb +0 -2
 - data/lib/protobuf/activerecord/protoable/serialization.rb +87 -64
 - data/lib/protobuf/activerecord/protoable/transformation.rb +140 -0
 - data/lib/protobuf/activerecord/version.rb +1 -1
 - data/protobuf-activerecord.gemspec +1 -1
 - data/spec/protoable/columns_spec.rb +99 -0
 - data/spec/protoable/persistence_spec.rb +2 -180
 - data/spec/protoable/serialization_spec.rb +149 -76
 - data/spec/protoable/transformation_spec.rb +214 -0
 - data/spec/support/definitions/user.proto +1 -1
 - data/spec/support/models/user.rb +4 -8
 - data/spec/support/protobuf/user.pb.rb +1 -1
 - metadata +15 -18
 - data/lib/protobuf/activerecord/protoable/convert.rb +0 -79
 - data/lib/protobuf/activerecord/protoable/fields.rb +0 -109
 - data/spec/protoable/convert_spec.rb +0 -265
 - data/spec/protoable/fields_spec.rb +0 -102
 
| 
         @@ -1,102 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            require 'spec_helper'
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
       3 
     | 
    
         
            -
            describe Protoable::Fields do
         
     | 
| 
       4 
     | 
    
         
            -
              describe "._protobuf_map_columns" do
         
     | 
| 
       5 
     | 
    
         
            -
                context "when the class has a table" do
         
     | 
| 
       6 
     | 
    
         
            -
                  let(:expected_column_names) {
         
     | 
| 
       7 
     | 
    
         
            -
                    User.columns.inject({}) do |hash, column|
         
     | 
| 
       8 
     | 
    
         
            -
                      hash[column.name.to_sym] = column
         
     | 
| 
       9 
     | 
    
         
            -
                      hash
         
     | 
| 
       10 
     | 
    
         
            -
                    end
         
     | 
| 
       11 
     | 
    
         
            -
                  }
         
     | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
       13 
     | 
    
         
            -
                  let(:expected_column_types) {
         
     | 
| 
       14 
     | 
    
         
            -
                    User.columns.inject({}) do |hash, column|
         
     | 
| 
       15 
     | 
    
         
            -
                      hash[column.type.to_sym] ||= []
         
     | 
| 
       16 
     | 
    
         
            -
                      hash[column.type.to_sym] << column.name.to_sym
         
     | 
| 
       17 
     | 
    
         
            -
                      hash
         
     | 
| 
       18 
     | 
    
         
            -
                    end
         
     | 
| 
       19 
     | 
    
         
            -
                  }
         
     | 
| 
       20 
     | 
    
         
            -
             
     | 
| 
       21 
     | 
    
         
            -
                  it "maps columns by name" do
         
     | 
| 
       22 
     | 
    
         
            -
                    User._protobuf_columns.should eq expected_column_names
         
     | 
| 
       23 
     | 
    
         
            -
                  end
         
     | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
       25 
     | 
    
         
            -
                  it "maps column names by column type" do
         
     | 
| 
       26 
     | 
    
         
            -
                    User._protobuf_column_types.should eq expected_column_types
         
     | 
| 
       27 
     | 
    
         
            -
                  end
         
     | 
| 
       28 
     | 
    
         
            -
                end
         
     | 
| 
       29 
     | 
    
         
            -
              end
         
     | 
| 
       30 
     | 
    
         
            -
             
     | 
| 
       31 
     | 
    
         
            -
              describe ".convert_field" do
         
     | 
| 
       32 
     | 
    
         
            -
                context "when the given converter is a hash" do
         
     | 
| 
       33 
     | 
    
         
            -
                  let(:method) { lambda { User.__send__(:convert_base64_to_string, value) } }
         
     | 
| 
       34 
     | 
    
         
            -
             
     | 
| 
       35 
     | 
    
         
            -
                  before { User.convert_field :public_key, :from => :base64, :to => :string }
         
     | 
| 
       36 
     | 
    
         
            -
             
     | 
| 
       37 
     | 
    
         
            -
                  it "determines the method using the hash's :to and :from keys" do
         
     | 
| 
       38 
     | 
    
         
            -
                    User.should_receive(:convert_base64_to_string)
         
     | 
| 
       39 
     | 
    
         
            -
                    User._protobuf_field_converters[:public_key].call(1)
         
     | 
| 
       40 
     | 
    
         
            -
                  end
         
     | 
| 
       41 
     | 
    
         
            -
                end
         
     | 
| 
       42 
     | 
    
         
            -
             
     | 
| 
       43 
     | 
    
         
            -
                context "when the given converter is a symbol" do
         
     | 
| 
       44 
     | 
    
         
            -
                  let(:callable) { lambda { User.__send__(:convert_email_to_lowercase, value) } }
         
     | 
| 
       45 
     | 
    
         
            -
             
     | 
| 
       46 
     | 
    
         
            -
                  before { User.convert_field :email, :convert_email_to_lowercase }
         
     | 
| 
       47 
     | 
    
         
            -
             
     | 
| 
       48 
     | 
    
         
            -
                  it "creates a callable method object from the converter" do
         
     | 
| 
       49 
     | 
    
         
            -
                    User.should_receive(:convert_email_to_lowercase)
         
     | 
| 
       50 
     | 
    
         
            -
                    User._protobuf_field_converters[:email].call(1)
         
     | 
| 
       51 
     | 
    
         
            -
                  end
         
     | 
| 
       52 
     | 
    
         
            -
                end
         
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
       54 
     | 
    
         
            -
                context "when the given converter is not callable" do
         
     | 
| 
       55 
     | 
    
         
            -
                  it "raises an exception" do
         
     | 
| 
       56 
     | 
    
         
            -
                    expect { User.convert_field :email, nil }.to raise_exception(Protoable::FieldConverterError)
         
     | 
| 
       57 
     | 
    
         
            -
                  end
         
     | 
| 
       58 
     | 
    
         
            -
                end
         
     | 
| 
       59 
     | 
    
         
            -
             
     | 
| 
       60 
     | 
    
         
            -
                context "when the given converter is callable" do
         
     | 
| 
       61 
     | 
    
         
            -
                  let(:callable) { lambda { |value| value } }
         
     | 
| 
       62 
     | 
    
         
            -
             
     | 
| 
       63 
     | 
    
         
            -
                  before { User.convert_field :email, callable }
         
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
     | 
    
         
            -
                  it "adds the given converter to list of field converters" do
         
     | 
| 
       66 
     | 
    
         
            -
                    User._protobuf_field_converters[:email].should eq callable
         
     | 
| 
       67 
     | 
    
         
            -
                  end
         
     | 
| 
       68 
     | 
    
         
            -
                end
         
     | 
| 
       69 
     | 
    
         
            -
              end
         
     | 
| 
       70 
     | 
    
         
            -
             
     | 
| 
       71 
     | 
    
         
            -
              describe ".attribute_from_proto" do
         
     | 
| 
       72 
     | 
    
         
            -
                context "when the given converter is a symbol" do
         
     | 
| 
       73 
     | 
    
         
            -
                  let(:callable) { lambda { |value| User.__send__(:extract_first_name) } }
         
     | 
| 
       74 
     | 
    
         
            -
             
     | 
| 
       75 
     | 
    
         
            -
                  before { User.attribute_from_proto :first_name, :extract_first_name }
         
     | 
| 
       76 
     | 
    
         
            -
             
     | 
| 
       77 
     | 
    
         
            -
                  it "creates a callable method object from the converter" do
         
     | 
| 
       78 
     | 
    
         
            -
                    User.should_receive(:extract_first_name)
         
     | 
| 
       79 
     | 
    
         
            -
                    User._protobuf_attribute_transformers[:first_name].call(1)
         
     | 
| 
       80 
     | 
    
         
            -
                  end
         
     | 
| 
       81 
     | 
    
         
            -
                end
         
     | 
| 
       82 
     | 
    
         
            -
             
     | 
| 
       83 
     | 
    
         
            -
                context "when the given converter is not callable" do
         
     | 
| 
       84 
     | 
    
         
            -
                  it "raises an exception" do
         
     | 
| 
       85 
     | 
    
         
            -
                    expect { User.attribute_from_proto :name, nil }.to raise_exception(Protoable::AttributeTransformerError)
         
     | 
| 
       86 
     | 
    
         
            -
                  end
         
     | 
| 
       87 
     | 
    
         
            -
                end
         
     | 
| 
       88 
     | 
    
         
            -
             
     | 
| 
       89 
     | 
    
         
            -
                context "when the given transformer is callable" do
         
     | 
| 
       90 
     | 
    
         
            -
                  let(:callable) { lambda { |proto| nil } }
         
     | 
| 
       91 
     | 
    
         
            -
             
     | 
| 
       92 
     | 
    
         
            -
                  before {
         
     | 
| 
       93 
     | 
    
         
            -
                    User.stub(:_protobuf_attribute_transformers).and_return(Hash.new)
         
     | 
| 
       94 
     | 
    
         
            -
                    User.attribute_from_proto :account_id, callable
         
     | 
| 
       95 
     | 
    
         
            -
                  }
         
     | 
| 
       96 
     | 
    
         
            -
             
     | 
| 
       97 
     | 
    
         
            -
                  it "adds the given converter to the list of protobuf field transformers" do
         
     | 
| 
       98 
     | 
    
         
            -
                    User._protobuf_attribute_transformers[:account_id] = callable
         
     | 
| 
       99 
     | 
    
         
            -
                  end
         
     | 
| 
       100 
     | 
    
         
            -
                end
         
     | 
| 
       101 
     | 
    
         
            -
              end
         
     | 
| 
       102 
     | 
    
         
            -
            end
         
     |