Skip to content

Support ruby variables #32

@gravitystorm

Description

@gravitystorm

This might just be a duplicate of #19 , feel free to close it if so.

Currently Glug supports defining constants, e.g.

FOO = 3

layer(:roads, source: :shortbread) do
  line_width FOO
end

However, using either local_variables or @class_variables doesn't work, e.g.

foo = 3

layer(:roads, source: :shortbread) do
  line_width foo
end
@foo = 3

layer(:roads, source: :shortbread) do
  line_width @foo
end

In either case, the variable evaluates to nil, rather than 3.

-      "paint":{"line-width":3},
+      "paint":{"line-width":null},

The root cause appears to be that the whole stylesheet is evaluated in the context of a Stylesheet instance, but each layer is separately evaluated in its own Layer instance. This means that variables defined at the top level belong to the Stylesheet and aren't available within a layer. Constants have a global scope so are handled differently, and that's why they work.

I've watched the "Ruby ate my DSL" talk again, and I think there's a lot from that talk that's applicable here. It describes many of the issues I'm running into and there are several suggestions in that talk that we might want to try.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions