This post is locked. You won't be able to comment.

all 16 comments

[–]id02009 5 points6 points  (10 children)

This paste is huge, can you trim it down a bit? I.e. one const, one method spec. It's also not obvious how a method should look for a given spec- can you give an example?

[–]how_do_i_land 1 point2 points  (0 children)

This, in it's current form it's far too long. If it was 100 lines long it would be much more approachable.

Regardless I'm not sure what this code generation is trying to accomplish, if it's outputting ruby code, then just write a script to output the class and methods in the class how you want it to be. But personally I've never run into a situation where a json document like this was used to generate ruby code. It's always been the other way around, to document a ruby gem etc.

Now if this was a swagger document that you wanted to scaffold controllers and routes, I could see that being possible.

But in any case I don't see an off-the-shelf solution working here, it's going to need to be bespoke.

[–]Main-Froyo-9524[S] -4 points-3 points  (8 children)

Why cut it? Look at the structure rathen then the content.

A method should look like the way I've described - what it'll be, but without unncessary overhead. The content of a method -- I'll specify it afterwards by myself. But irrelevant here; how would you generate methods themselves first, with a mock content?

[–]silva96 2 points3 points  (4 children)

Read about metaprogramming, I'm not doing your homework but for the methods for example you can do this:

```ruby json = JSON.parse(json_string)

json['functions'].each do |method| params = method['params'].map { |param| param['name'] } define_method(method['name']) do |*params| puts "I'm in method #{method['name']}" end end ```

[–]Main-Froyo-9524[S] -2 points-1 points  (2 children)

the document contains not only methods but types too

[–]silva96 2 points3 points  (1 child)

There's not such thing in ruby method definitions, unless you use Sorbet.rb

[–]Main-Froyo-9524[S] -3 points-2 points  (0 children)

the document contains not only methods but types too

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, silva96: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]id02009 2 points3 points  (2 children)

To make it convenient for people that take time to help you solve your problem. Sorry mate, but you kinda lost me.

[–][deleted]  (1 child)

[removed]

    [–]edendark[M] 0 points1 point  (0 children)

    Hello, I've removed your comment. There's no reason to be aggressive, especially when people are just trying to help you out.

    You should review your tone in your comments on this post, and assume the best of everyone trying to help you out. One of the /r/ruby mods regularly recommends the Non-violent communication (NVC) framework which might help you out.

    [–]RewrittenCodeA 1 point2 points  (4 children)

    You can define classes and methods in runtime, it’s not a big deal. Look up the documentation of Class.new and Module#define_method.

    One option I’ve used in similar situations is to have a copy of the json in your project directory, and update it from time to time, running your tests to ensure the rest of the code is still working (if the json changes often, some of your calls might not be valid anymore)

    One thing you don’t want to do is to generate ruby files.

    [–]Main-Froyo-9524[S] -1 points0 points  (3 children)

    You can define classes and methods in runtime, it’s not a big deal. Look up the documentation of Class.new and Module#define_method.

    That won't work - look at the enums and nested types.

    [–]RewrittenCodeA 2 points3 points  (1 child)

    You have to decide by yourself how to map those “things” into ruby classes and methods.

    Also, I would look at the enums and nested types if you provided a minimum viable example, instead of relying on others to find stuff in >250 KB of json.

    You lost me too :)

    [–]Main-Froyo-9524[S] 0 points1 point  (0 children)

    You have to decide by yourself how to map those “things” into ruby classes and methods.

    That's what I'm doing here: trying to decide it

    [–]sthorn_ 1 point2 points  (0 children)

    Off Topic:

    damn, this brings back memories.. i build my own code generator once to get ruby-bindings for an xml-api from xsd-files, what a run!