Skip to content

Instantly share code, notes, and snippets.

@splattael
Created October 23, 2017 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save splattael/8fd7b3d349a28b2e41182d8f26c866ed to your computer and use it in GitHub Desktop.
Save splattael/8fd7b3d349a28b2e41182d8f26c866ed to your computer and use it in GitHub Desktop.
module ROM
end
require "dry/struct"
require "dry/types"
require "rom-factory"
module Types
include Dry::Types.module
end
class User < Dry::Struct
constructor_type :strict_with_defaults
attribute :id, String
attribute :name, Types.Instance(String)
attribute :email, Types.Instance(String)
end
FakeAttribute = Struct.new(:name)
class FakeRelation
def initialize(name, klass)
@klass = klass
@attributes = klass.schema.map { |name, _| FakeAttribute.new(name) }
end
def schema
self
end
def attributes
@attributes
end
def with(*)
self
end
def combine(*)
self
end
def mapper
self
end
def model
@klass
end
def reject(*)
@attributes
end
def primary_key
:id
end
end
class FakeRelations
def [](name)
FakeRelation.new(name, User)
end
end
class RomFake
def relations
FakeRelations.new
end
end
Factory = ROM::Factory.configure do |config|
config.rom = RomFake.new
end
Factory.define(:user) do |f|
f.name { fake(:name, :first_name) }
f.sequence :email do |n|
"janjiss#{n}@gmail.com"
end
end
p Factory.structs[:user]
p Factory.structs[:user]
p Factory.structs[:user]
# #<User id=1 name="Krystal" email="janjiss1@gmail.com">
# #<User id=2 name="Jeff" email="janjiss2@gmail.com">
# #<User id=3 name="Genoveva" email="janjiss3@gmail.com">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment