I have a code, that`s generates a files. And I want to be able to require a initialize variables.
lib/main.rb
require_relative "l5k_cms/version"
require "./new_files"
require "./methods"
require "sinatra"
require "erb"
def load_pictures
Dir.glob("public/image/*.{jpg,JPG}")
end
get "/" do
@pictures = load_pictures
erb :index
end
get "/new" do
erb :new
end
post "/new" do
@nazwa = params[:name]
@nazwisko = params[:family]
@klasa = params[:class]
@ogien = params[:fire]
@powietrze = params[:air]
@woda = params[:water]
@ziemia = params[:earth]
@pustka = params[:void]
cechy = { :nazwa => @nazwa, :nazwisko => @nazwisko, :klasa => @klasa, :ogien => @ogien, :powietrze => @powietrze, :woda => @woda, :ziemia => @ziemia, :pustka => @pustka }
dir = Dir_methods.new
dir.create_NPC_folder()
Dir.chdir("public/NPC") do
npc = File.new("#{@nazwa}", "w+")
npc_rb = File.new("#{@nazwa}.rb", "w+")
writer = File_Methods.new()
writer.txt_file_filer(npc, cechy)
npc.close()
writer.rb_file_filer(npc_rb, cechy)
npc_rb.close()
end
erb :show_new_NPC
end
get '/show' do
@test = []
@npc = ["plik"]
Dir.chdir("public/NPC") do
@npc = Dir.glob("*.rb")
@npc.each do |element|
require("./#{element}")
class_name = Object.const_get(element.capitalize[/^[^.]*/])
a = class_name.new()
@test.push("To jest imie: #{a.nazwa}, a to nazwisko: #{a.nazwisko}, a to ogien #{a.ogien}")
end
end
erb :show_NPC
end
lib/Public/NPC/foo.rb
class Foo
attr_accessor :nazwa, :nazwisko
def initialize
@nazwa = Foo
@nazwisko = moreFoo
end
end
lib/views/show.erb
<html>
<head>
<tile>Show</title>
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="CSS/styles.css"/>
</head>
<body>
<h1>Hello </h1>
<h3> This is the test array >> <%= @test %> </h3>
<a href="/">Strona Glowna </a>
</body>
</html>
The file is create by method in methods.rb which gets attr_hash from main.rb
def rb_file_filer(name, attr_hash)
imie = attr_hash[:nazwa]
name.write("class #{imie.capitalize}\n")
name.write("attr_accessor")
attr_hash.each { |key, value| name.write(":#{key}, ") }
name.write("\n")
name.write("def initialize\n")
attr_hash.each { |key, value| name.write("@#{key} = #{value}\n") }
name.write("end\n")
name.write("end")
end
And the values are getting from POST in views/new.erb
<html>
<head>
<tile>Tworzenie Postaci</title>
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="CSS/styles.css"/>
</head>
<body>
<h1>Kto to bedzie?</h1>
<form action="/new" method="POST">
<pre>
Imie: <input type="text" name="name"><br/>
Rodzina: <input type="text" name="family"><br/>
Profrsja: <input type="text" name="class"><br/>
Ogien: <input type="text" name="fire"><br/>
Powietrze: <input type="text" name="air"><br/>
Ziemia: <input type="text" name="earth"><br/>
Woda: <input type="text" name="water"><br/>
Pustka: <input type="text" name="void"><br/>
<input type="submit">
</pre>
</form>
</body>
</html>
And when I run the code instead of getting Foo, moreFoo, I get nothing just as the initialize variables where 'nil'. Why?
The file is requested as intended, then loads all the initialize variables just instead of returning values it returns nil.