liveh2o / active_remote

Active Remote provides Active Record-like object-relational mapping over RPC. It's Active Record for your platform.
MIT License
63 stars 23 forks source link

Update to Rails 5.0/5.1 #71

Closed liveh2o closed 5 years ago

liveh2o commented 5 years ago

Use type casting from Active Model

Active Model 5.0 includes type casting from Active Record, and a type registry to register and use custom types. Bring Active Remote inline with Active Record by replacing the custom type casters extracted from ActiveAttr with the type casting from Active Model.

This change removes the old method of definition custom types. Instead of a providing a class that responds to call, custom types must be registered, and respond to deserialize (value for RPC), and cast (value from user).

class CustomEnumType < ActiveModel::Type::Value
  def cast(value)
    ProtobufEnum.fetch(value)
  end

  def deserialize(value)
    ProtobufEnum.fetch(value)
  end
end

ActiveModel::Type.register(:custom_enum_type, CustomEnumType)

class MyModel < ActiveRemote::Base
  attribute :enum_attribute, :custom_enum_type
end

See ActiveModel::Type::Value for details.

It also brings a significant speed boost (~30% faster initialization, and ~70% faster instantiation):

Ruby 2.5.3

# ActiveAttr types

Warming up --------------------------------------
          initialize     1.335k i/100ms
         instantiate     3.611k i/100ms
     init attributes     1.429k i/100ms
     inst attributes     3.519k i/100ms
Calculating -------------------------------------
          initialize     14.808k (±11.0%) i/s -    292.365k in  20.033342s
         instantiate     39.668k (±14.8%) i/s -    772.754k in  20.083307s
     init attributes     14.555k (±10.8%) i/s -    287.229k in  20.019188s
     inst attributes     37.593k (±11.6%) i/s -    738.990k in  20.060324s

# Active Model types

Warming up --------------------------------------
          initialize     1.985k i/100ms
         instantiate     7.012k i/100ms
     init attributes     1.905k i/100ms
     inst attributes     5.307k i/100ms
Calculating -------------------------------------
          initialize     20.274k (± 4.7%) i/s -    404.940k in  20.038435s
         instantiate     73.663k (± 2.8%) i/s -      1.473M in  20.007175s
     init attributes     19.592k (± 5.4%) i/s -    390.525k in  20.001697s
     inst attributes     66.223k (± 5.0%) i/s -      1.321M in  20.017327s

JRuby 9.2.5

# ActiveAttr types

Warming up --------------------------------------
          initialize     1.330k i/100ms
         instantiate     5.152k i/100ms
     init attributes     1.770k i/100ms
     inst attributes     5.211k i/100ms
Calculating -------------------------------------
          initialize     21.175k (± 4.6%) i/s -    422.940k in  20.022394s
         instantiate     55.949k (± 8.7%) i/s -      1.108M in  20.022917s
     init attributes     18.940k (±14.4%) i/s -    368.160k in  20.042572s
     inst attributes     54.155k (± 8.6%) i/s -      1.073M in  20.008969s

# Active Model types

Warming up --------------------------------------
          initialize     1.573k i/100ms
         instantiate     9.877k i/100ms
     init attributes     2.441k i/100ms
     inst attributes    10.440k i/100ms
Calculating -------------------------------------
          initialize     25.341k (±11.9%) i/s -    497.068k in  20.038812s
         instantiate    120.435k (± 9.8%) i/s -      2.380M in  20.040166s
     init attributes     23.572k (±13.6%) i/s -    461.349k in  20.038992s
     inst attributes    107.820k (±15.7%) i/s -      2.067M in  20.179653s

Use query attributes from Active Record

Attribute predicate methods (i.e., attribute?) in Active Record assume the value has already been type cast. This means numeric attributes are only considered present when zero? is false, and other attributes are only considered present when blank? is false. Update the attribute predicate methods in Active Remote to behave the same way.

This means that string values such as 'FALSE', and '0' are now considered present.