protobuf-activerecord 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -55,29 +55,26 @@ module Protoable
55
55
  #
56
56
  # Examples:
57
57
  # convert_field :created_at, :int64
58
- # convert_field :public_key, method(:extract_public_key_from_proto)
59
58
  # convert_field :public_key, :extract_public_key_from_proto
60
59
  # convert_field :status, lambda { |proto_field| ... }
61
- # convert_field :symmetric_key, :base64
62
60
  # convert_field :symmetric_key, :from => :base64, :to => :encoded_string
63
- # convert_field :symmetric_key, :from => :base64, :to => :raw_string
64
61
  #
65
- def convert_field(field, callable = nil, &blk)
66
- callable ||= blk
67
-
68
- if callable.is_a?(Hash)
69
- callable = :"convert_#{callable[:from]}_to_#{callable[:to]}"
70
- end
71
-
72
- if callable.is_a?(Symbol)
73
- unless self.respond_to?(callable)
74
- column = _protobuf_columns[field.to_sym]
75
- callable = :"convert_#{callable}_to_#{column.try(:type)}"
62
+ def convert_field(field, transformer = nil, &blk)
63
+ transformer ||= blk
64
+ transformer = :"convert_#{transformer[:from]}_to_#{transformer[:to]}" if transformer.is_a?(Hash)
65
+
66
+ if transformer.is_a?(Symbol)
67
+ unless self.respond_to?(transformer, true)
68
+ column = _protobuf_columns[field.to_sym]
69
+ transformer = :"convert_#{transformer}_to_#{column.try(:type)}"
76
70
  end
77
- callable = method(callable) if self.respond_to?(callable)
71
+
72
+ callable = lambda { |value| self.__send__(transformer, value) }
73
+ else
74
+ callable = transformer
78
75
  end
79
76
 
80
- if callable.nil? || !callable.respond_to?(:call)
77
+ unless callable.respond_to?(:call)
81
78
  raise FieldConverterError, 'Field converters must be a callable or block!'
82
79
  end
83
80
 
@@ -98,17 +95,16 @@ module Protoable
98
95
  # transform_column :public_key, :extract_public_key_from_proto
99
96
  # transform_column :status, lambda { |proto_field| ... }
100
97
  #
101
- def transform_column(field, callable = nil, &blk)
102
- callable ||= blk
98
+ def transform_column(field, transformer = nil, &blk)
99
+ transformer ||= blk
103
100
 
104
- if callable.is_a?(Symbol)
105
- unless self.respond_to?(callable)
106
- raise ColumnTransformerError, "#{callable} is not defined!"
107
- end
108
- callable = method(callable) if self.respond_to?(callable)
101
+ if transformer.is_a?(Symbol)
102
+ callable = lambda { |value| self.__send__(transformer, value) }
103
+ else
104
+ callable = transformer
109
105
  end
110
106
 
111
- if callable.nil? || !callable.respond_to?(:call)
107
+ unless callable.respond_to?(:call)
112
108
  raise ColumnTransformerError, 'Protoable casting needs a callable or block!'
113
109
  end
114
110
 
@@ -41,22 +41,22 @@ module Protoable
41
41
  # convert_column :status, lambda { |proto_field| ... }
42
42
  # convert_column :symmetric_key, :from => :base64, :to => :raw_string
43
43
  #
44
- def convert_column(field, callable = nil, &blk)
45
- callable ||= blk
44
+ def convert_column(field, converter = nil, &blk)
45
+ converter ||= blk
46
+ converter = :"convert_#{converter[:from]}_to_#{converter[:to]}" if converter.is_a?(Hash)
46
47
 
47
- if callable.is_a?(Hash)
48
- callable = :"convert_#{callable[:from]}_to_#{callable[:to]}"
49
- end
50
-
51
- if callable.is_a?(Symbol)
52
- unless self.respond_to?(callable)
48
+ if converter.is_a?(Symbol)
49
+ unless self.respond_to?(converter, true)
53
50
  column = _protobuf_columns[field.to_sym]
54
- callable = :"convert_#{callable}_to_#{column.try(:type)}"
51
+ converter = :"convert_#{converter}_to_#{column.try(:type)}"
55
52
  end
56
- callable = method(callable) if self.respond_to?(callable)
53
+
54
+ callable = lambda { |value| __send__(converter, value) }
55
+ else
56
+ callable = converter
57
57
  end
58
58
 
59
- if callable.nil? || !callable.respond_to?(:call)
59
+ unless callable.respond_to?(:call)
60
60
  raise ColumnConverterError, 'Column converters must be a callable or block!'
61
61
  end
62
62
 
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module ActiveRecord
3
- VERSION = "1.0.3"
3
+ VERSION = "1.0.4"
4
4
  end
5
- end
5
+ end
@@ -30,46 +30,30 @@ describe Protoable::Fields do
30
30
 
31
31
  describe ".convert_field" do
32
32
  context "when the given converter is a hash" do
33
- let(:method) { User.method(:convert_base64_to_string) }
33
+ let(:method) { lambda { User.__send__(:convert_base64_to_string, value) } }
34
34
 
35
35
  before { User.convert_field :public_key, :from => :base64, :to => :string }
36
36
 
37
37
  it "determines the method using the hash's :to and :from keys" do
38
- User._protobuf_field_converters[:public_key].should eq method
38
+ User.should_receive(:convert_base64_to_string)
39
+ User._protobuf_field_converters[:public_key].call(1)
39
40
  end
40
41
  end
41
42
 
42
43
  context "when the given converter is a symbol" do
43
- context "when the converter is not a defined method" do
44
- let(:callable) { User.method(:convert_base64_to_string) }
44
+ let(:callable) { lambda { User.__send__(:convert_email_to_lowercase, value) } }
45
45
 
46
- before { User.convert_field :email, :base64 }
46
+ before { User.convert_field :email, :convert_email_to_lowercase }
47
47
 
48
- it "determines the method using the converter as the 'from' and the column type as the 'to'" do
49
- User._protobuf_field_converters[:email].should eq callable
50
- end
51
- end
52
-
53
- context "when the converter is a defined method" do
54
- let(:callable) { User.method(:convert_email_to_lowercase) }
55
-
56
- before { User.convert_field :email, :convert_email_to_lowercase }
57
-
58
- it "creates a callable method object from the converter" do
59
- User._protobuf_field_converters[:email].should eq callable
60
- end
61
- end
62
- end
63
-
64
- context "when the given converter is nil" do
65
- it "raises an exception" do
66
- expect { User.convert_field :email, nil }.to raise_exception(Protoable::FieldConverterError)
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)
67
51
  end
68
52
  end
69
53
 
70
54
  context "when the given converter is not callable" do
71
55
  it "raises an exception" do
72
- expect { User.convert_field :email, :foo }.to raise_exception(Protoable::FieldConverterError)
56
+ expect { User.convert_field :email, nil }.to raise_exception(Protoable::FieldConverterError)
73
57
  end
74
58
  end
75
59
 
@@ -86,32 +70,19 @@ describe Protoable::Fields do
86
70
 
87
71
  describe ".transform_column" do
88
72
  context "when the given converter is a symbol" do
89
- context "when the converter is not a defined method" do
90
- it "raises an exception" do
91
- expect { User.transform_column :name, :foo }.to raise_exception(Protoable::ColumnTransformerError)
92
- end
93
- end
73
+ let(:callable) { lambda { |value| User.__send__(:extract_first_name) } }
94
74
 
95
- context "when the converter is a defined method" do
96
- let(:callable) { User.method(:extract_first_name) }
75
+ before { User.transform_column :first_name, :extract_first_name }
97
76
 
98
- before { User.transform_column :first_name, :extract_first_name }
99
-
100
- it "creates a callable method object from the converter" do
101
- User._protobuf_column_transformers[:first_name].should eq callable
102
- end
103
- end
104
- end
105
-
106
- context "when the given converter is nil" do
107
- it "raises an exception" do
108
- expect { User.transform_column :name, nil }.to raise_exception(Protoable::ColumnTransformerError)
77
+ it "creates a callable method object from the converter" do
78
+ User.should_receive(:extract_first_name)
79
+ User._protobuf_column_transformers[:first_name].call(1)
109
80
  end
110
81
  end
111
82
 
112
83
  context "when the given converter is not callable" do
113
84
  it "raises an exception" do
114
- expect { User.transform_column :name, double(:not_callable) }.to raise_exception(Protoable::ColumnTransformerError)
85
+ expect { User.transform_column :name, nil }.to raise_exception(Protoable::ColumnTransformerError)
115
86
  end
116
87
  end
117
88
 
@@ -5,46 +5,30 @@ describe Protoable::Serialization do
5
5
 
6
6
  describe ".convert_column" do
7
7
  context "when the given converter is a hash" do
8
- let(:method) { User.method(:convert_base64_to_string) }
8
+ let(:method) { lambda { |value| User.__send__(:convert_base64_to_string, value) } }
9
9
 
10
10
  before { User.convert_column :public_key, :from => :base64, :to => :string }
11
11
 
12
12
  it "determines the method using the hash's :to and :from keys" do
13
- User._protobuf_column_converters[:public_key].should eq method
13
+ User.should_receive(:convert_base64_to_string)
14
+ User._protobuf_column_converters[:public_key].call(1)
14
15
  end
15
16
  end
16
17
 
17
18
  context "when the given converter is a symbol" do
18
- context "when the converter is not a defined method" do
19
- let(:callable) { User.method(:convert_base64_to_string) }
19
+ let(:callable) { lambda { |value| User.__send__(:convert_email_to_lowercase, value) } }
20
20
 
21
- before { User.convert_column :email, :base64 }
21
+ before { User.convert_column :email, :convert_email_to_lowercase }
22
22
 
23
- it "determines the method using the converter as the 'from' and the column type as the 'to'" do
24
- User._protobuf_column_converters[:email].should eq callable
25
- end
26
- end
27
-
28
- context "when the converter is a defined method" do
29
- let(:callable) { User.method(:convert_email_to_lowercase) }
30
-
31
- before { User.convert_column :email, :convert_email_to_lowercase }
32
-
33
- it "creates a callable method object from the converter" do
34
- User._protobuf_column_converters[:email].should eq callable
35
- end
36
- end
37
- end
38
-
39
- context "when the given converter is nil" do
40
- it "raises an exception" do
41
- expect { User.convert_column :email, nil }.to raise_exception(Protoable::ColumnConverterError)
23
+ it "creates a callable method object from the converter" do
24
+ User.should_receive(:convert_email_to_lowercase)
25
+ User._protobuf_column_converters[:email].call(1)
42
26
  end
43
27
  end
44
28
 
45
29
  context "when the given converter is not callable" do
46
30
  it "raises an exception" do
47
- expect { User.convert_column :email, :foo }.to raise_exception(Protoable::ColumnConverterError)
31
+ expect { User.convert_column :email, nil }.to raise_exception(Protoable::ColumnConverterError)
48
32
  end
49
33
  end
50
34
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-05 00:00:00.000000000 Z
12
+ date: 2012-11-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -222,7 +222,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
222
222
  version: '0'
223
223
  segments:
224
224
  - 0
225
- hash: 763481842519497626
225
+ hash: 3415879493130708479
226
226
  required_rubygems_version: !ruby/object:Gem::Requirement
227
227
  none: false
228
228
  requirements:
@@ -231,7 +231,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
231
231
  version: '0'
232
232
  segments:
233
233
  - 0
234
- hash: 763481842519497626
234
+ hash: 3415879493130708479
235
235
  requirements: []
236
236
  rubyforge_project:
237
237
  rubygems_version: 1.8.24