lib/rom/factory/dsl.rb in rom-factory-0.10.2 vs lib/rom/factory/dsl.rb in rom-factory-0.11.0

- old
+ new

@@ -1,28 +1,32 @@ # frozen_string_literal: true -require 'faker' -require 'dry/core/inflector' +require "faker" -require 'rom/factory/builder' -require 'rom/factory/attribute_registry' -require 'rom/factory/attributes' +require "rom/factory/builder" +require "rom/factory/attribute_registry" +require "rom/factory/attributes" module ROM module Factory + extend ::Dry::Core::Cache + class << self # @api private - def fake(type, *args) - api = ::Faker.const_get(::Dry::Core::Inflector.classify(type.to_s)) + def fake(*args, **options) + api = fetch_or_store(:faker, *args) do + *ns, method_name = args - if args[0].is_a?(Symbol) - api.public_send(*args) - else - api.public_send(type, *args) + const = ns.reduce(::Faker) do |obj, name| + obj.const_get(::Dry::Core::Inflector.camelize(name)) + end + + const.method(method_name) end + + api.(**options) end - ruby2_keywords(:fake) if respond_to?(:ruby2_keywords, true) end # Factory builder DSL # # @api public @@ -31,11 +35,11 @@ attr_reader :_name, :_relation, :_attributes, :_factories, :_struct_namespace, :_valid_names attr_reader :_traits # @api private - def initialize(name, attributes: AttributeRegistry.new, relation:, factories:, struct_namespace:) + def initialize(name, relation:, factories:, struct_namespace:, attributes: AttributeRegistry.new) @_name = name @_relation = relation @_factories = factories @_struct_namespace = struct_namespace @_attributes = attributes.dup @@ -81,32 +85,40 @@ # @example # f.email { fake(:name) } # # @param [Symbol] type The value type to generate # - # @overload fake(api, type) + # @overload fake(genre, type) # @example # f.email { fake(:internet, :email) } # - # @param [Symbol] api The faker API identifier ie. :internet, :product etc. + # @param [Symbol] genre The faker API identifier ie. :internet, :product etc. # @param [Symbol] type The value type to generate # - # @overload fake(api, type, *args) + # @overload fake(genre, type, **options) # @example - # f.email { fake(:number, :between, 10, 100) } + # f.email { fake(:number, :between, from: 10, to: 100) } # - # @param [Symbol] api The faker API identifier ie. :internet, :product etc. + # @param [Symbol] genre The faker API identifier ie. :internet, :product etc. # @param [Symbol] type The value type to generate - # @param [Array] args Additional arguments + # @param [Hash] options Additional arguments # - # @see https://github.com/stympy/faker/tree/master/doc + # @overload fake(genre, subgenre, type, **options) + # @example + # f.quote { fake(:books, :dune, :quote, character: 'stilgar') } # + # @param [Symbol] genre The Faker genre of API i.e. :books, :creature, :games etc + # @param [Symbol] subgenre The subgenre of API i.e. :dune, :bird, :myst etc + # @param [Symbol] type the value type to generate + # @param [Hash] options Additional arguments + # + # @see https://github.com/faker-ruby/faker/tree/master/doc + # # @api public - def fake(*args) - ::ROM::Factory.fake(*args) + def fake(type, *args, **options) + ::ROM::Factory.fake(type, *args, **options) end - ruby2_keywords(:fake) if respond_to?(:ruby2_keywords, true) def trait(name, parents = [], &block) _traits[name] = DSL.new( "#{_name}_#{name}", attributes: _traits.values_at(*parents).flat_map(&:elements).inject( @@ -134,10 +146,10 @@ # @api public def association(name, *traits, **options) assoc = _relation.associations[name] if assoc.is_a?(::ROM::SQL::Associations::OneToOne) && options.fetch(:count, 1) > 1 - ::Kernel.raise ::ArgumentError, 'count cannot be greater than 1 on a OneToOne' + ::Kernel.raise ::ArgumentError, "count cannot be greater than 1 on a OneToOne" end builder = -> { _factories.for_relation(assoc.target) } _attributes << attributes::Association.new(assoc, builder, *traits, **options)