Sha256: b138946c80bb172d8bf190d1b134e30fd85ba2e247bc61897dd818c83ef9a466

Contents?: true

Size: 1.53 KB

Versions: 54

Compression:

Stored size: 1.53 KB

Contents

module FactoryGirl
  class Definition
    attr_reader :callbacks, :defined_traits, :declarations, :constructor

    def initialize(name = nil, base_traits = [])
      @declarations      = DeclarationList.new(name)
      @callbacks         = []
      @defined_traits    = []
      @to_create         = ->(instance) { instance.save! }
      @base_traits       = base_traits
      @additional_traits = []
      @constructor       = nil
    end

    delegate :declare_attribute, to: :declarations

    def attributes
      @attributes ||= declarations.attribute_list
    end

    def compile
      attributes
    end

    def processing_order
      base_traits + [self] + additional_traits
    end

    def overridable
      declarations.overridable
      self
    end

    def inherit_traits(new_traits)
      @additional_traits += new_traits
    end

    def add_callback(callback)
      @callbacks << callback
    end

    def to_create(&block)
      if block_given?
        @to_create = block
      else
        @to_create
      end
    end

    def define_trait(trait)
      @defined_traits << trait
    end

    def define_constructor(&block)
      @constructor = block
    end

    private

    def base_traits
      @base_traits.map { |name| trait_by_name(name) }
    end

    def additional_traits
      @additional_traits.map { |name| trait_by_name(name) }
    end

    def trait_by_name(name)
      trait_for(name) || FactoryGirl.trait_by_name(name)
    end

    def trait_for(name)
      defined_traits.detect {|trait| trait.name == name }
    end
  end
end

Version data entries

54 entries across 54 versions & 4 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.7.4 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.7.3 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.7.2 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.7.1 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.7.0 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.6.9 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.6.8 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.6.7 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.6.6 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.6.5 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.6.4 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.6.3 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.6.2 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.6.1 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.6.0 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.5.17 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.5.16 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.5.15 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb
classiccms-0.5.14 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/definition.rb