Sha256: 3e3ffc13121c9a18af741ee8e099556ce739d59c7da6b3ea69980248326c9a02

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

require 'faker'

module Qwerty
  class Lipsum
    attr_accessor :nodes, :total, :destructive, :parent_document

    def initialize(nodes = Node.root.children, options = {})
      @nodes = nodes
      @total = options[:total] || 10
      @destructive = options[:destructive] || true
      @parent_document = options[:parent_document]
      @options = options
    end

    # Create Lipsum documents
    def run!
      nodes.each do | node |            
        destructive? ? node.documents.destroy_all : next unless node.documents.empty?
        (node.depth == 1? 1 : total).times do
          doc = node.documents.new
          doc.title = node.depth == 1 ? node.name.capitalize : Faker::Lorem.words(rand(5)+1).join(' ').capitalize
          doc.body = Faker::Lorem.paragraphs(rand(20)+1).collect { |p| "<p>#{p}</p>"}.join("\n")
          doc.published = true
          doc.parent = parent_document
          doc.save
          self.class.new(node.children, @options.merge({:parent_document => doc})).run!
        end
      end
    end

    def destructive?
      @destructive
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
qwerty-0.0.7.pre lib/qwerty/lipsum.rb
qwerty-0.0.5.pre lib/qwerty/lipsum.rb
qwerty-0.0.4.pre lib/qwerty/lipsum.rb
qwerty-0.0.3.pre lib/qwerty/lipsum.rb
qwerty-0.0.1.pre lib/qwerty/lipsum.rb