Sha256: d0dc7d42804322c9ed8555d9a41fec1b4b58c4bc22c23d9254b02660dd06c282

Contents?: true

Size: 916 Bytes

Versions: 4

Compression:

Stored size: 916 Bytes

Contents

require 'dry/struct'

require 'active_record'
require 'benchmark/ips'

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')

ActiveRecord::Schema.define do
  create_table :users do |table|
    table.column :name, :string
    table.column :age, :integer
  end
end

class ARUser < ActiveRecord::Base
  self.table_name = :users
end

module Types
  include Dry.Types
end

class DryStructUser < Dry::Struct
  attribute :id, Types::Params::Integer
  attribute :name, Types::Strict::String.constrained(size: 3..64)
  attribute :age, Types::Params::Integer.constrained(gt: 18)
end

puts ARUser.new(id: 1, name: 'Jane', age: '21').inspect
puts DryStructUser.new(id: 1, name: 'Jane', age: '21').inspect

Benchmark.ips do |x|
  x.report('active record') { ARUser.new(id: 1, name: 'Jane', age: '21') }
  x.report('dry-struct') { DryStructUser.new(id: 1, name: 'Jane', age: '21') }

  x.compare!
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-struct-1.2.0 benchmarks/constrained.rb
dry-struct-1.1.1 benchmarks/constrained.rb
dry-struct-1.1.0 benchmarks/constrained.rb
dry-struct-1.0.0 benchmarks/constrained.rb