-1

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.

Kazik
  • 705
  • 1
  • 8
  • 20
  • What is `moreFoo` and where is it defined? – Santhosh Jun 02 '14 at 05:07
  • moreFoo is initialize variable of foo.rb file just like Foo – Kazik Jun 02 '14 at 05:14
  • You say `@nazwisko = moreFoo`. but `moreFoo` is not defined – Santhosh Jun 02 '14 at 05:16
  • both should be strings – Kazik Jun 02 '14 at 05:17
  • if you just want strings, in initialize of Foo, you can just do `@nazwa = 'Foo' @nazwisko = 'moreFoo'` – Santhosh Jun 02 '14 at 05:19
  • the Foo.file is generated by loop, ass a rsult of POST, I tried 'Foo' and it is not working – Kazik Jun 02 '14 at 05:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54898/discussion-between-santosh-and-kazik). – Santhosh Jun 02 '14 at 05:25
  • I have read the discussion, and you better add your factory to the question (and how you use it), as I think it does not create what you think it creates, and has many issues which might be the root cause behind this. – Uri Agassi Jun 02 '14 at 06:54
  • Added the whole main.rb and the method. I have a basic version, build on my other question http://stackoverflow.com/questions/23857877/how-to-pass-class-name-as-variable-in-ruby which works perfect – Kazik Jun 02 '14 at 07:10
  • Checked on my dry 'deck function', and the problem is with the created files. Yet I don`t know why? – Kazik Jun 02 '14 at 08:56

1 Answers1

0

The problem was with method that generates .rb files, it was creating two mistakes:

  1. The loop created "," at the end of attr_accessor symbol list.
  2. The Names where not strings, and there for could not be pass as strings.

Here is new method which works:

def rb_file_filer(name, attr_hash)
    imie = attr_hash[:nazwa]
    name.write("class #{imie.capitalize}\n")
    name.write("attr_accessor")
    attr_hash.each_with_index do |(key, value), index|
        if index == attr_hash.length - 1
            name.write(":#{key}")
        else
            name.write(":#{key}, ") 
        end
    end
    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
Kazik
  • 705
  • 1
  • 8
  • 20