Created
October 23, 2017 19:48
-
-
Save splattael/8fd7b3d349a28b2e41182d8f26c866ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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