protobuf-activerecord 3.0.0.pre → 3.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 222a25f863e0432deaee84af24cdb1afcf54d470
4
- data.tar.gz: 2db3c1dce2882e7495cfe2d7b3eed18f01677b19
3
+ metadata.gz: f05a18edbd1975b9bfa100ca10a9cba378f9bb0c
4
+ data.tar.gz: 8b5e67786002299711c1166c4138df5b7361f3fc
5
5
  SHA512:
6
- metadata.gz: 96dd33216999495aca093656d30918711af9c1641e4476ea028b929ba162607510ce0d36253762821e4b48c210c4834188166a3c12e9b7d0a77a6ef58045bdf4
7
- data.tar.gz: c0ef76053aff3bcdbf00a3a8d9bb894367491c753a4e583799fef3d1a2d928445306cb898b1cd6b9d1d94ae8795050c016958ad9f262f8419c2671e173f027f0
6
+ metadata.gz: 020fe794814c24474431013da37691656fdfcf9d6de94daf40fd060c4c7fbdad53678b22dc1d79d13e21eab997cb8e831129879ad0475f70e910de6d8042ad53
7
+ data.tar.gz: 5ff23bd27c06791f05426c8470812bd6f0c7521616cdcde5ba6cff47208658a97f3d38caf32f8cfda6f862e5cd62cf37eab63500688f3de012fe7ca158706f29
@@ -9,16 +9,6 @@ require 'protobuf/active_record/version'
9
9
 
10
10
  module Protobuf
11
11
  module ActiveRecord
12
- module LoadHooks
13
- def inherited(klass)
14
- super
15
-
16
- klass.class_eval do
17
- include Protobuf::ActiveRecord::Model
18
- end
19
- end
20
- end
21
-
22
12
  def self.config
23
13
  @config ||= Protobuf::ActiveRecord::Config.new
24
14
  end
@@ -9,11 +9,30 @@ module Protobuf
9
9
  # Raised by `attribute_from_proto` when the transformer method
10
10
  # given is not callable.
11
11
  class AttributeTransformerError < ProtobufActiveRecordError
12
+ def message
13
+ "Attribute transformers must be called with a callable or block!"
14
+ end
12
15
  end
13
16
 
14
17
  # Raised by `field_from_record` when the convert method
15
18
  # given not callable.
16
19
  class FieldTransformerError < ProtobufActiveRecordError
20
+ def message
21
+ "Field transformers must be called with a callable or block!"
22
+ end
23
+ end
24
+
25
+ # Raised by `to_proto` when no protobuf message is defined.
26
+ class MessageNotDefined < ProtobufActiveRecordError
27
+ attr_reader :class_name
28
+
29
+ def initialize(klass)
30
+ @class_name = klass.name
31
+ end
32
+
33
+ def message
34
+ "#{class_name} does not define a protobuf message"
35
+ end
17
36
  end
18
37
 
19
38
  # Raised by `field_scope` when given scope is not defined.
@@ -4,7 +4,11 @@ module Protobuf
4
4
  config.protobuf_active_record = Protobuf::ActiveRecord.config
5
5
 
6
6
  ActiveSupport.on_load(:active_record) do
7
- extend Protobuf::ActiveRecord::LoadHooks if Protobuf::ActiveRecord.config.autoload
7
+ if Protobuf::ActiveRecord.config.autoload
8
+ on_inherit do
9
+ include Protobuf::ActiveRecord::Model
10
+ end
11
+ end
8
12
  end
9
13
  end
10
14
  end
@@ -96,7 +96,7 @@ module Protobuf
96
96
  next unless proto.respond_to_and_has_and_present?(field)
97
97
 
98
98
  unless self.respond_to?(scope_name)
99
- raise Protobuf::ActiveRecord::SearchScopeError, "Undefined scope :#{scope_name}."
99
+ raise SearchScopeError, "Undefined scope :#{scope_name}."
100
100
  end
101
101
 
102
102
  search_values = parse_search_values(proto, field)
@@ -9,19 +9,16 @@ module Protobuf
9
9
  included do
10
10
  include ::Heredity
11
11
 
12
- class << self
13
- attr_accessor :_protobuf_field_transformers, :_protobuf_field_options
14
- end
15
-
16
- @_protobuf_field_transformers = {}
17
- @_protobuf_field_options = {}
18
-
19
12
  inheritable_attributes :_protobuf_field_transformers,
20
13
  :_protobuf_field_options,
21
14
  :protobuf_message
22
15
 
16
+ @_protobuf_field_transformers = {}
17
+ @_protobuf_field_options = {}
18
+
23
19
  private :_protobuf_convert_attributes_to_fields
24
20
  private :_protobuf_field_transformers
21
+ private :_protobuf_message
25
22
  end
26
23
 
27
24
  module ClassMethods
@@ -72,12 +69,33 @@ module Protobuf
72
69
  end
73
70
 
74
71
  unless callable.respond_to?(:call)
75
- raise FieldTransformerError, 'Attribute transformers need a callable or block!'
72
+ raise FieldTransformerError
76
73
  end
77
74
 
78
75
  _protobuf_field_transformers[field.to_sym] = callable
79
76
  end
80
77
 
78
+ # Define the protobuf fields that will be automatically serialized (by default,
79
+ # all fields will be serialized). Accepts any number of field names and is
80
+ # equivalent to passing the :only option to `protobuf_message`.
81
+ #
82
+ # If :except is specified, all fields except the specified fields will be serialized.
83
+ #
84
+ # By default, deprecated fields will be serialized. To exclude deprecated
85
+ # fields, pass :deprecated => false in the options hash.
86
+ #
87
+ # Examples:
88
+ # protobuf_fields :guid, :name
89
+ # protobuf_fields :except => :email_domain
90
+ # protobuf_fields :except => :email_domain, :deprecated => false
91
+ #
92
+ def protobuf_fields(*fields)
93
+ options = fields.extract_options!
94
+ options[:only] = fields
95
+
96
+ self._protobuf_field_options = options
97
+ end
98
+
81
99
  # Define the protobuf message class that should be used to serialize the
82
100
  # object to protobuf. Accepts a string or symbol and an options hash.
83
101
  #
@@ -104,12 +122,7 @@ module Protobuf
104
122
  def protobuf_message(message = nil, options = {})
105
123
  unless message.nil?
106
124
  @protobuf_message = message.to_s.classify.constantize
107
-
108
125
  self._protobuf_field_options = options
109
-
110
- define_method(:to_proto) do |options = {}|
111
- self.class.protobuf_message.new(self.fields_from_record(options))
112
- end
113
126
  end
114
127
 
115
128
  @protobuf_message
@@ -189,6 +202,19 @@ module Protobuf
189
202
  def _protobuf_field_transformers
190
203
  self.class._protobuf_field_transformers
191
204
  end
205
+
206
+ # :nodoc:
207
+ def _protobuf_message
208
+ self.class.protobuf_message
209
+ end
210
+
211
+ # :nodoc:
212
+ def to_proto(options = {})
213
+ raise MessageNotDefined.new(self.class) if _protobuf_message.nil?
214
+
215
+ fields = self.fields_from_record(options)
216
+ _protobuf_message.new(fields)
217
+ end
192
218
  end
193
219
  end
194
220
  end
@@ -100,7 +100,7 @@ module Protobuf
100
100
  end
101
101
 
102
102
  unless callable.respond_to?(:call)
103
- raise AttributeTransformerError, 'Attribute transformers need a callable or block!'
103
+ raise AttributeTransformerError
104
104
  end
105
105
 
106
106
  _protobuf_attribute_transformers[attribute.to_sym] = callable
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module ActiveRecord
3
- VERSION = "3.0.0.pre"
3
+ VERSION = "3.0.0.rc2"
4
4
  end
5
5
  end
@@ -22,7 +22,7 @@ Gem::Specification.new do |gem|
22
22
  #
23
23
  gem.add_dependency "activerecord", ">= 3.2.0"
24
24
  gem.add_dependency "activesupport", ">= 3.2.0"
25
- gem.add_dependency "heredity"
25
+ gem.add_dependency "heredity", ">= 0.1.1"
26
26
  gem.add_dependency "protobuf", ">= 2.2.0"
27
27
 
28
28
  ##
@@ -1,5 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
+ # Used to test calling #to_proto when no protobuf message is configured.
4
+ class UnconfiguredUser
5
+ include Protobuf::ActiveRecord::Model
6
+ end
7
+
3
8
  describe Protobuf::ActiveRecord::Serialization do
4
9
  let(:protobuf_message) { UserMessage }
5
10
 
@@ -233,13 +238,23 @@ describe Protobuf::ActiveRecord::Serialization do
233
238
  end
234
239
 
235
240
  describe "#to_proto" do
236
- let(:proto) { protobuf_message.new(proto_hash) }
237
- let(:proto_hash) { { :name => "foo" } }
241
+ context "when a protobuf message is configured" do
242
+ let(:proto) { protobuf_message.new(proto_hash) }
243
+ let(:proto_hash) { { :name => "foo" } }
238
244
 
239
- before { user.stub(:fields_from_record).and_return(proto_hash) }
245
+ before { user.stub(:fields_from_record).and_return(proto_hash) }
240
246
 
241
- it "intializes a new protobuf message with attributes from #to_proto_hash" do
242
- user.to_proto.should eq proto
247
+ it "intializes a new protobuf message with attributes from #to_proto_hash" do
248
+ user.to_proto.should eq proto
249
+ end
250
+ end
251
+
252
+ context "when a protobuf message is not configured" do
253
+ let(:user) { UnconfiguredUser.new }
254
+
255
+ it "raises an exception" do
256
+ expect { user.to_proto }.to raise_exception(Protobuf::ActiveRecord::MessageNotDefined)
257
+ end
243
258
  end
244
259
  end
245
260
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.pre
4
+ version: 3.0.0.rc2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hutchison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-27 00:00:00.000000000 Z
11
+ date: 2013-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - '>='
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 0.1.1
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 0.1.1
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: protobuf
57
57
  requirement: !ruby/object:Gem::Requirement