Module: RoFaker

Defined in:
lib/ro_rspec_helpers/ro_faker.rb

Class Method Summary collapse

Class Method Details

.cp_dir(from, to) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 36

def cp_dir(from, to)
  if RoCommands.spec and RoCommands.fix
    from_path = File.join(RoCommands.spec, 'ro_faker_templates', from)
    to_path = File.join(RoCommands.fix, "for_rails")
    FileUtils.rmtree to_path
    FileUtils.cp_r from_path, to_path
  else
    raise "Can not copy #{from}"

  end
end

.directory(path) ⇒ Object Also known as: d



63
64
65
66
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 63

def directory(path)
  FileUtils.mkpath path
  FileUtils.rm_rf Dir[File.join(path, '**')]
end

.fakerObject



93
94
95
96
97
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 93

def faker
  rm_all
  dir('ro_support')
  try('ro_support.rb')
end

.file(name, ctn = "") ⇒ Object Also known as: f



70
71
72
73
74
75
76
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 70

def file(name, ctn="")
  path = name
  dir = File.dirname path
  FileUtils.mkpath dir
  Out.table("RoFaker writing", "#{path} file:#{File.basename(__FILE__)} line:#{__LINE__}")
  File.write path, ctn
end

.flow {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (RoFaker)

    the object that the method was called on



80
81
82
83
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 80

def flow
  rm_fix_dir
  yield self
end

.nameObject



20
21
22
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 20

def name
  ::Faker::Name.first_name
end

.parasObject

auto omit ‘create’ e.g. fake_files means create_fake_files ls means file hash lib file and ro_rspec_helpers e.g ls1: lib/file1.rb ro_rspec_helpers/file1_spec.rb



12
13
14
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 12

def paras
  ::Faker::Lorem.paragraphs
end

.rails4Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 24

def rails4
  if RoCommands.fix
    Dir.chdir RoCommands.fix
    rails_dir = File.join(RoCommands.fix, name)
    cp_dir("rails4", "for_rails")
  else
    raise "create a rails4 for rspec failed"
  end

  Dir.chdir File.join(RoCommands.fix, "for_rails")
end

.rm_allObject



110
111
112
113
114
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 110

def rm_all
  if Dir.pwd[/fixtures/]
    FileUtils.rm_r Dir["**"]
  end
end

.rm_fix_dirObject



85
86
87
88
89
90
91
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 85

def rm_fix_dir
  if Dir.pwd.match(%r{fixtures})
    basename = File.basename(Dir.pwd).gsub(%r{\.\w+$}, "")
    Out.table "clear", "dir:#{basename}", "file:#{File.basename __FILE__} line:#{__LINE__}"
    FileUtils.rmtree Dir['**']
  end
end

.sentenceObject



16
17
18
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 16

def sentence
  ::Faker::Lorem.sentence
end

.template(from, to) ⇒ Object Also known as: t



52
53
54
55
56
57
58
59
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 52

def template(from, to)
  if defined? RoCommands.spec and defined? RoCommands.fix
    path = template_path(from)
    ctn = ::RoFile.read(path)
    result = ERB.new(ctn).result(binding)
    ::RoFile.write(to, result)
  end
end

.template_path(from) ⇒ Object



48
49
50
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 48

def template_path(from)
  path = File.join(RoCommands.spec, 'ro_faker_templates', "#{from}.erb")
end

.try(name) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/ro_rspec_helpers/ro_faker.rb', line 99

def try(name)
  ctn = <<-CONTENT
class RoSupport
  def
end
  CONTENT

  result = ERB.new(ctn).result(binding)
  file(name, ctn)
end