Skip to content

Commit b416588

Browse files
authored
Merge pull request #38 from gravitystorm/ivars
Support using ivars in stylesheets
2 parents 73f3043 + 022714e commit b416588

12 files changed

Lines changed: 176 additions & 3 deletions

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Metrics/AbcSize:
1414
# Offense count: 2
1515
# Configuration parameters: CountComments, CountAsOne.
1616
Metrics/ClassLength:
17-
Max: 208
17+
Max: 217
1818

1919
# Offense count: 3
2020
# Configuration parameters: AllowedMethods, AllowedPatterns.

lib/glug/layer.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,23 @@ def initialize(stylesheet, args = {})
8282
end
8383

8484
def dsl_eval(&block)
85+
# Copy ivars from stylesheet DSL to layer DSL (before eval)
86+
style_dsl = @stylesheet.dsl
87+
style_dsl.instance_variables.each do |ivar|
88+
next if ivar.to_s.start_with?('@__') # skip internal ivars
89+
90+
@dsl.instance_variable_set(ivar, style_dsl.instance_variable_get(ivar))
91+
end
92+
93+
# Run the layer evaluation
8594
@dsl.instance_eval(&block)
95+
96+
# Copy ivars back to stylesheet DSL (after eval)
97+
@dsl.instance_variables.each do |ivar|
98+
next if ivar.to_s.start_with?('@__') # skip internal ivars
99+
100+
style_dsl.instance_variable_set(ivar, @dsl.instance_variable_get(ivar))
101+
end
86102
end
87103

88104
# Handle all missing 'method' calls

lib/glug/stylesheet.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Glug
44
# the main document object
55
class Stylesheet
6-
attr_accessor :sources, :kv, :base_dir, :params
6+
attr_accessor :sources, :kv, :base_dir, :params, :dsl
77

88
def initialize(base_dir: nil, params: nil, &block)
99
@sources = {}

lib/glug/stylesheet_dsl.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ def respond_to_missing?(*)
2626

2727
# Set a property, e.g. 'bearing 29'
2828
def method_missing(method_sym, *args)
29-
@__impl.add_property(method_sym, *args)
29+
if Layer::EXPRESSIONS.include?(method_sym)
30+
Condition.new.from_list(method_sym, args)
31+
else
32+
@__impl.add_property(method_sym, *args)
33+
end
3034
end
3135
end
3236
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version 8
2+
name 'Expression ivars test'
3+
source :shortbread, type: 'vector', url: 'https://vector.openstreetmap.org/shortbread_v1/tilejson.json'
4+
5+
# Expression assigned to ivar at stylesheet level
6+
@my_halo = rgba(255, 255, 255, 0.8)
7+
@my_color = rgb(100, 200, 50)
8+
9+
layer(:labels, zoom: 5.., source: :shortbread) do
10+
text_halo_color @my_halo
11+
text_color @my_color
12+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version":8,
3+
"name":"Expression ivars test",
4+
"sources":{
5+
"shortbread":{
6+
"type":"vector",
7+
"url":"https://vector.openstreetmap.org/shortbread_v1/tilejson.json"
8+
}
9+
},
10+
"layers":[
11+
{
12+
"paint":{
13+
"text-halo-color":["rgba",255,255,255,0.8],
14+
"text-color":["rgb",100,200,50]
15+
},
16+
"source":"shortbread",
17+
"id":"labels",
18+
"source-layer":"labels",
19+
"type":"symbol",
20+
"minzoom":5
21+
}
22+
]
23+
}

spec/fixtures/ivars.glug

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version 8
2+
name 'My first stylesheet'
3+
source :shortbread, type: 'vector', url: 'https://vector.openstreetmap.org/shortbread_v1/tilejson.json'
4+
5+
# set at stylesheet level
6+
@width = 6
7+
8+
layer(:roads, zoom: 10..13, source: :shortbread) do
9+
# get at layer level
10+
line_width @width
11+
line_color 0x888888
12+
end
13+
14+
layer(:water, source: :shortbread) do
15+
# set at layer level
16+
@water = 'blue'
17+
fill_color @water
18+
end
19+
20+
layer(:water_line, source: :shortbread) do
21+
# get from other layer
22+
fill_color @water
23+
end

spec/fixtures/ivars.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"version":8,
3+
"name":"My first stylesheet",
4+
"sources":{
5+
"shortbread":{
6+
"type":"vector",
7+
"url":"https://vector.openstreetmap.org/shortbread_v1/tilejson.json"
8+
}
9+
},
10+
"layers":[
11+
{
12+
"paint":{"line-width":6,"line-color":"#888888"},
13+
"source":"shortbread",
14+
"id":"roads",
15+
"source-layer":"roads",
16+
"type":"line",
17+
"minzoom":10,
18+
"maxzoom":13
19+
},
20+
{
21+
"paint":{"fill-color":"blue"},
22+
"source":"shortbread",
23+
"id":"water",
24+
"source-layer":"water",
25+
"type":"fill"
26+
},
27+
{
28+
"paint":{"fill-color":"blue"},
29+
"source":"shortbread",
30+
"id":"water_line",
31+
"source-layer":"water_line",
32+
"type":"fill"
33+
}
34+
]
35+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version 8
2+
name 'My first stylesheet'
3+
source :shortbread, type: 'vector', url: 'https://vector.openstreetmap.org/shortbread_v1/tilejson.json'
4+
5+
# set at stylesheet level
6+
@width = 6
7+
8+
include_file 'ivars_with_include_sub.glug'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version":8,
3+
"name":"My first stylesheet",
4+
"sources":{
5+
"shortbread":{
6+
"type":"vector",
7+
"url":"https://vector.openstreetmap.org/shortbread_v1/tilejson.json"
8+
}
9+
},
10+
"layers":[
11+
{
12+
"paint":{"line-width":6,"line-color":"#888888"},
13+
"source":"shortbread",
14+
"id":"roads",
15+
"source-layer":"roads",
16+
"type":"line",
17+
"minzoom":10,
18+
"maxzoom":13
19+
}
20+
]
21+
}

0 commit comments

Comments
 (0)