Skip to content

Conversation

@gravitystorm
Copy link
Collaborator

(Note: this is built on top of #37 and that should be handled first. The diff shown here currently includes those changes).

This allows you to define and use instance variables in both the stylesheet context and also within layers. They operate as if the
glug files were all part of one large ruby class.

This is similar behaviour to "@" variables in LESS, so will feel familiar to anyone coming from a CartoCSS background.

The tests show how it works, but here's a copy of the example stylesheet to give you a good idea:

version 8
name 'My first stylesheet'
source :shortbread, type: 'vector', url: 'https://vector.openstreetmap.org/shortbread_v1/tilejson.json'

# set at stylesheet level
@width = 6

layer(:roads, zoom: 10..13, source: :shortbread) do
  # get at layer level
  line_width @width
  line_color 0x888888
end

layer(:water, source: :shortbread) do
  # set at layer level
  @water = 'blue'
  fill_color @water
end

layer(:water_line, source: :shortbread) do
  # get from other layer
  fill_color @water
end

The main complexity here is that although ruby allows you to get and set instance variables programmatically, there's no hook for intercepting code that's trying to do so with "@foo = bar" statements, like you would find in the stylesheet. Or in other works, there's no equivalent of method_missing but for ivars. Instead, we shuffle the ivars back and forward between the LayerDSL and StylesheetDSL, as each layer is evaluated. Slightly clunky but it works.

Feedback welcome!

This allows you to define and use instance variables in both the
stylesheet context and also within layers. They operate as if the
glug files were all part of one large ruby class.

This is similar behaviour to "@" variables in LESS, so will feel
familiar to anyone coming from a CartoCSS background.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant