scarpe-team / scarpe

Scarpe - shoes but running on webview
Other
163 stars 28 forks source link

Positional styles in initialize() for Lacci drawables #420

Closed noahgibbs closed 11 months ago

noahgibbs commented 1 year ago

A lot of Shoes drawables have a positional-arg init form and a keyword-arg (technically hashes because Shoes is really old) form.

For instance, these are equivalent:

oval(25, 50, 100)
oval(top: 25, left: 50, radius: 100) # Same thing

Right now it's awkward to handle this in initialize. We usually do something like this:

module Shoes
  class Star < Shoes::Drawable
    shoes_styles :left, :top, :draw_context

    shoes_style(:points) { |val| convert_to_integer(val, "points") }
    shoes_style(:outer) { |val| convert_to_float(val, "outer") }
    shoes_style(:inner) { |val| convert_to_float(val, "inner") }

    def initialize(left, top, points = 10, outer = 100, inner = 50)
      super
      self.left, self.top, self.points, self.outer, self.inner = left, top, points, outer, inner

      @draw_context = Shoes::App.instance.current_draw_context

      create_display_drawable
    end

Technically this is... fine. We get validation on the positional args because we do the self-dot assignments. And the positional args override the keyword args, which they should. But it's still awkward looking. One thing we could do is to have a method like "positional_init :left, :top, :points, :outer, :inner" above, next to the shoes_styles declarations, and then the "super" call could handle it too.

noahgibbs commented 11 months ago

Fixed.