Sha256: 99ba0565562d7caf3fcc84302b6ee5b49c3ebb9e52f359a3699b9ab765d01949

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

module ToFactory
  module Generation
    class Factory
      def initialize(representation)
        @representation = representation
      end

      def name
        add_quotes @representation.name
      end

      def to_factory
        header do
          attributes.map do |attr, value|
            factory_attribute(attr, value)
          end.sort.join("\n") << "\n"
        end
      end

      def header(&block)
        if ToFactory.new_syntax?
          modern_header &block
        else
          header_factory_girl_1 &block
        end
      end

      def modern_header(&block)
        generic_header("factory", "", &block)
      end

      def header_factory_girl_1(&block)
        generic_header("Factory.define", " |o|", &block)
      end

      def factory_attribute(attr, value)
        Attribute.new(attr, value).to_s
      end

      private

      def attributes
        to_skip = [:id, :created_at, :updated_at]

        @representation.attributes.delete_if{|key, _| to_skip.include? key.to_sym}
      end

      def parent_name
        @representation.parent_name
      end

      def generic_header(factory_start, block_arg, &block)
        out =  "  #{factory_start}(:#{name}#{parent_clause}) do#{block_arg}\n"
        out << yield.to_s
        out << "  end\n"
      end

      def parent_clause
        has_parent? ?  ", :parent => :#{add_quotes parent_name}" : ""
      end

      def has_parent?
        parent_name.to_s.length > 0
      end

      def add_quotes(name)
        name = name.to_s

        if name["/"]
          if name[/^".*"$/]
            name
          else
            "\"#{name}\""
          end
        else
          name
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
to_factory-0.2.0 lib/to_factory/generation/factory.rb