active_remote 3.2.2 → 3.3.0

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
  SHA256:
3
- metadata.gz: 2878525c112088bd33495008b9b277abc79d5e5eea871d9e54e9fd9e63f44db8
4
- data.tar.gz: f06dac077bfcd9d1662d74631c859d96a9959904bc3502328ea8fa2c90757ec9
3
+ metadata.gz: ae8cf64c394e8f550a86ae5249454544d8a80c26f895f5358cd553a45d7d7a4c
4
+ data.tar.gz: 6ebd6029726c068e9f08ee8ec77e89309f9582c4b35f870b38edfd1b154a1138
5
5
  SHA512:
6
- metadata.gz: 38159822cec60490ba7ae82bc8cedc3c65aefa0991d50ac849630ebfbec47c5896a341786914ac70f4b4e5b3991d37d7c899bbd19f7dd055618d95d0579a0d8f
7
- data.tar.gz: cee03e271600b1dc3edadba8934e2fa77500918fe8945c004ab2b8cc6cc54c7188119bba524339217dfeb4a5c30f8515f3a5e07ac41e35185084c79ac88187fb
6
+ metadata.gz: bf3efedb46cd28754bc7ddad9c7c8a5b69483c8c68d77df57423ae34f60bccc98215a0239a920831ae92e1fab52fa2cd7e58a5788d0908df33569a75338d85f2
7
+ data.tar.gz: 553dc23127a1dd631ca176a427c08110295cbc128d38119eb4597fe520642b9ed700a41f3a629bdcc6de52883b8f411b1afe476e2d2050b23a7f24a129717afb
data/CHANGES.md CHANGED
@@ -1,5 +1,49 @@
1
1
  # ActiveRemote Changes
2
2
 
3
+ 3.3.0
4
+ ----------
5
+
6
+ - Add support for registering types that can be used to define attributes without using the existing `:type` or
7
+ `:typecaster` options `attribute :name, :string` [#69]
8
+
9
+ 3.2.2
10
+ ----------
11
+
12
+ - Speed up boolean typecasting [#67, @abrandoned]
13
+
14
+ 3.2.1
15
+ ----------
16
+
17
+ - Use `:remote_call` instead of of `rpc.execute` in persistence, search
18
+
19
+ 3.2.0
20
+ ----------
21
+
22
+ - Add ability to override default RPC endpoints [#66]
23
+ - Require Active Model 4.x to 5.1 for compatibility
24
+
25
+ 3.1.3
26
+ ----------
27
+
28
+ - Require Active Model 4.x for compatibility
29
+ - Cache and dup default attributes instead of building from scratch (4x speed boost on initialize) [#63, @film42]
30
+
31
+ 3.1.2
32
+ ----------
33
+
34
+ - Allow primary_key to be set on create [#61, @mattnichols]
35
+ - Change the behavior of DateTime types to gracefully handle invalid dates [#62, @brianstien]
36
+
37
+ 3.1.1
38
+ ----------
39
+
40
+ - Guard against undefined method errors in the Protobuf adapter [#59, @brianstien]
41
+
42
+ 3.1.0
43
+ ----------
44
+
45
+ - Bubble up the type of error given from Protobuf instead of a generic `ActiveRemoteError` [#58, @ryanbjones]
46
+
3
47
  3.0.0
4
48
  ----------
5
49
 
@@ -33,4 +33,5 @@ Gem::Specification.new do |s|
33
33
  s.add_development_dependency "pry"
34
34
  s.add_development_dependency "protobuf-rspec", ">= 1.1.2"
35
35
  s.add_development_dependency "simplecov"
36
+ s.add_development_dependency "benchmark-ips"
36
37
  end
data/bin/benchmark ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+
5
+ require "active_remote"
6
+ require "benchmark/ips"
7
+
8
+ require "./spec/support/models/typecasted_author"
9
+
10
+ ATTRIBUTES = {
11
+ :guid => "0c030733-3b78-4587-b94b-5e0cf26497c5",
12
+ :name => "Charles Dickens",
13
+ :age => 68,
14
+ :birthday => "1812-02-07 00:00:00",
15
+ :writes_fiction => true,
16
+ :net_sales => 1_000_000.0
17
+ }
18
+
19
+ ::Benchmark.ips do |x|
20
+ x.config(:time => 20, :warmup => 10)
21
+ x.report("initialize") do
22
+ ::TypecastedAuthor.new(ATTRIBUTES)
23
+ end
24
+
25
+ x.report("instantiate") do
26
+ ::TypecastedAuthor.instantiate(ATTRIBUTES)
27
+ end
28
+
29
+ x.report("init attributes") do
30
+ ::TypecastedAuthor.new(ATTRIBUTES).attributes
31
+ end
32
+
33
+ x.report("inst attributes") do
34
+ ::TypecastedAuthor.instantiate(ATTRIBUTES).attributes
35
+ end
36
+ end
@@ -1,3 +1,5 @@
1
+ require "active_remote/type"
2
+
1
3
  module ActiveRemote
2
4
  # Represents an attribute for reflection
3
5
  #
@@ -47,15 +49,17 @@ module ActiveRemote
47
49
  # AttributeDefinition.new(:amount)
48
50
  #
49
51
  # @param [Symbol, String, #to_sym] name attribute name
52
+ # @param [Symbol] type attribute type
50
53
  # @param [Hash{Symbol => Object}] options attribute options
51
54
  #
52
55
  # @return [ActiveAttr::AttributeDefinition]
53
56
  #
54
57
  # @since 0.2.0
55
- def initialize(name, options={})
58
+ def initialize(name, type = :unknown, **options)
56
59
  raise TypeError, "can't convert #{name.class} into Symbol" unless name.respond_to? :to_sym
57
60
  @name = name.to_sym
58
61
  @options = options
62
+ @options[:typecaster] = ::ActiveRemote::Type.lookup(type) unless type == :unknown
59
63
 
60
64
  if @options[:type]
61
65
  typecaster = ::ActiveRemote::Typecasting::TYPECASTER_MAP[@options[:type]]
@@ -101,6 +105,6 @@ module ActiveRemote
101
105
 
102
106
  # The attribute options
103
107
  # @since 0.5.0
104
- attr_reader :options
108
+ attr_reader :options, :type
105
109
  end
106
110
  end
@@ -1,28 +1,32 @@
1
- require "active_remote/typecasting"
1
+ require "active_remote/type"
2
2
 
3
3
  module ActiveRemote
4
4
  module Serializers
5
5
  module Protobuf
6
6
  extend ActiveSupport::Concern
7
7
 
8
- TYPECASTER_MAP = {
9
- ::Protobuf::Field::BoolField => ActiveRemote::Typecasting::BooleanTypecaster,
10
- ::Protobuf::Field::BytesField => ActiveRemote::Typecasting::StringTypecaster,
11
- ::Protobuf::Field::DoubleField => ActiveRemote::Typecasting::FloatTypecaster,
12
- ::Protobuf::Field::Fixed32Field => ActiveRemote::Typecasting::IntegerTypecaster,
13
- ::Protobuf::Field::Fixed64Field => ActiveRemote::Typecasting::IntegerTypecaster,
14
- ::Protobuf::Field::FloatField => ActiveRemote::Typecasting::FloatTypecaster,
15
- ::Protobuf::Field::Int32Field => ActiveRemote::Typecasting::IntegerTypecaster,
16
- ::Protobuf::Field::Int64Field => ActiveRemote::Typecasting::IntegerTypecaster,
17
- ::Protobuf::Field::Sfixed32Field => ActiveRemote::Typecasting::IntegerTypecaster,
18
- ::Protobuf::Field::Sfixed64Field => ActiveRemote::Typecasting::IntegerTypecaster,
19
- ::Protobuf::Field::Sint32Field => ActiveRemote::Typecasting::IntegerTypecaster,
20
- ::Protobuf::Field::Sint64Field => ActiveRemote::Typecasting::IntegerTypecaster,
21
- ::Protobuf::Field::StringField => ActiveRemote::Typecasting::StringTypecaster,
22
- ::Protobuf::Field::Uint32Field => ActiveRemote::Typecasting::IntegerTypecaster,
23
- ::Protobuf::Field::Uint64Field => ActiveRemote::Typecasting::IntegerTypecaster
8
+ FIELD_TYPE_MAP = {
9
+ ::Protobuf::Field::BoolField => :boolean,
10
+ ::Protobuf::Field::BytesField => :string,
11
+ ::Protobuf::Field::DoubleField => :float,
12
+ ::Protobuf::Field::Fixed32Field => :float,
13
+ ::Protobuf::Field::Fixed64Field => :float,
14
+ ::Protobuf::Field::FloatField => :float,
15
+ ::Protobuf::Field::Int32Field => :integer,
16
+ ::Protobuf::Field::Int64Field => :integer,
17
+ ::Protobuf::Field::Sfixed32Field => :float,
18
+ ::Protobuf::Field::Sfixed64Field => :float,
19
+ ::Protobuf::Field::Sint32Field => :integer,
20
+ ::Protobuf::Field::Sint64Field => :integer,
21
+ ::Protobuf::Field::StringField => :string,
22
+ ::Protobuf::Field::Uint32Field => :integer,
23
+ ::Protobuf::Field::Uint64Field => :integer
24
24
  }
25
25
 
26
+ def self.type_name_for_field(field)
27
+ FIELD_TYPE_MAP[field.type_class]
28
+ end
29
+
26
30
  module ClassMethods
27
31
  def fields_from_attributes(message_class, attributes)
28
32
  Fields.from_attributes(message_class, attributes)
@@ -118,6 +122,10 @@ module ActiveRemote
118
122
  value.is_a?(Array) ? value : [ value ]
119
123
  end
120
124
 
125
+ def type_name
126
+ Serializers::Protobuf.type_name_for_field(field)
127
+ end
128
+
121
129
  def typecast(value)
122
130
  return value if value.nil?
123
131
 
@@ -129,7 +137,7 @@ module ActiveRemote
129
137
  end
130
138
 
131
139
  def typecaster
132
- @typecaster ||= TYPECASTER_MAP[field.type_class]
140
+ @typecaster ||= Type.lookup(type_name)
133
141
  end
134
142
 
135
143
  def typecaster?
@@ -0,0 +1,35 @@
1
+ # Based on the type registry from Rails 5.0
2
+ # https://github.com/rails/rails/blob/5-0-stable/activemodel/lib/active_model/type.rb
3
+ #
4
+ # TODO: Replace this with the Active Model type registry once Rails 4.2 support is dropped.
5
+ #
6
+ require "active_remote/type/registry"
7
+ require "active_remote/typecasting"
8
+
9
+ module ActiveRemote
10
+ module Type
11
+ @registry = Registry.new
12
+
13
+ class << self
14
+ attr_accessor :registry # :nodoc:
15
+
16
+ # Add a new type to the registry, allowing it to be gotten through ActiveRemote::Type#lookup
17
+ def register(type_name, klass)
18
+ registry.register(type_name, klass)
19
+ end
20
+
21
+ def lookup(type_name) # :nodoc:
22
+ registry.lookup(type_name)
23
+ end
24
+ end
25
+
26
+ register(:boolean, Typecasting::BooleanTypecaster)
27
+ register(:date, Typecasting::DateTypecaster)
28
+ register(:datetime, Typecasting::DateTimeTypecaster)
29
+ register(:decimal, Typecasting::BigDecimalTypecaster)
30
+ register(:float, Typecasting::FloatTypecaster)
31
+ register(:integer, Typecasting::IntegerTypecaster)
32
+ register(:object, Typecasting::ObjectTypecaster)
33
+ register(:string, Typecasting::StringTypecaster)
34
+ end
35
+ end
@@ -0,0 +1,45 @@
1
+ module ActiveRemote
2
+ module Type
3
+ class Registry
4
+ attr_reader :registrations
5
+
6
+ def initialize
7
+ @registrations = []
8
+ end
9
+
10
+ def register(type_name, typecaster)
11
+ registrations << registration_klass.new(type_name, typecaster)
12
+ end
13
+
14
+ def lookup(symbol)
15
+ registration = find_registration(symbol)
16
+ raise ArgumentError, "Unknown type #{symbol.inspect}" unless registration
17
+
18
+ registration.typecaster
19
+ end
20
+
21
+ private
22
+
23
+ def registration_klass
24
+ Registration
25
+ end
26
+
27
+ def find_registration(symbol)
28
+ registrations.find { |r| r.matches?(symbol) }
29
+ end
30
+ end
31
+
32
+ class Registration
33
+ attr_reader :name, :typecaster
34
+
35
+ def initialize(name, typecaster)
36
+ @name = name
37
+ @typecaster = typecaster
38
+ end
39
+
40
+ def matches?(type_name)
41
+ type_name == name
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveRemote
2
- VERSION = "3.2.2"
2
+ VERSION = "3.3.0"
3
3
  end
@@ -1,8 +1,8 @@
1
1
  class TypecastedAuthor < ::ActiveRemote::Base
2
2
  attribute :guid, :type => String
3
3
  attribute :name, :typecaster => StringTypecaster
4
- attribute :age, :type => Integer
4
+ attribute :age, :integer
5
5
  attribute :birthday, :type => DateTime
6
- attribute :writes_fiction, :type => Boolean
7
- attribute :net_sales, :type => Float
6
+ attribute :writes_fiction, :boolean
7
+ attribute :net_sales, :typecaster => FloatTypecaster
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_remote
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hutchison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-31 00:00:00.000000000 Z
11
+ date: 2019-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -156,12 +156,27 @@ dependencies:
156
156
  - - ">="
157
157
  - !ruby/object:Gem::Version
158
158
  version: '0'
159
+ - !ruby/object:Gem::Dependency
160
+ name: benchmark-ips
161
+ requirement: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
159
173
  description: Active Remote provides Active Record-like object-relational mapping over
160
174
  RPC. It was written for use with Google Protocol Buffers, but could be extended
161
175
  to use any RPC data format.
162
176
  email:
163
177
  - liveh2o@gmail.com
164
178
  executables:
179
+ - benchmark
165
180
  - console
166
181
  extensions: []
167
182
  extra_rdoc_files: []
@@ -175,6 +190,7 @@ files:
175
190
  - README.md
176
191
  - Rakefile
177
192
  - active_remote.gemspec
193
+ - bin/benchmark
178
194
  - bin/console
179
195
  - lib/active_remote.rb
180
196
  - lib/active_remote/association.rb
@@ -197,6 +213,8 @@ files:
197
213
  - lib/active_remote/search.rb
198
214
  - lib/active_remote/serialization.rb
199
215
  - lib/active_remote/serializers/protobuf.rb
216
+ - lib/active_remote/type.rb
217
+ - lib/active_remote/type/registry.rb
200
218
  - lib/active_remote/typecasting.rb
201
219
  - lib/active_remote/typecasting/big_decimal_typecaster.rb
202
220
  - lib/active_remote/typecasting/boolean.rb