Skip to content

Commit 73f3043

Browse files
authored
Merge pull request #37 from gravitystorm/layer_dsl
Use a separate DSL class for Layer evaluation
2 parents fe2c68b + cd3b7fc commit 73f3043

File tree

6 files changed

+73
-9
lines changed

6 files changed

+73
-9
lines changed

.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: 206
17+
Max: 208
1818

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

lib/glug.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ module Glug # :nodoc:
1111
require_relative 'glug/layer'
1212
require_relative 'glug/extensions'
1313
require_relative 'glug/stylesheet_dsl'
14+
require_relative 'glug/layer_dsl'

lib/glug/layer.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,17 @@ def initialize(stylesheet, args = {})
7878
@kv[:source] ||= stylesheet.sources.find { |_k, v| v[:default] }[0]
7979
@kv[:source_layer] ||= args[:id] if stylesheet.sources[@kv[:source]][:type] == 'vector'
8080
@child_num = 0 # incremented sublayer suffix
81+
@dsl = LayerDSL.new(self)
82+
end
83+
84+
def dsl_eval(&block)
85+
@dsl.instance_eval(&block)
8186
end
8287

8388
# Handle all missing 'method' calls
8489
# If we can match it to a GL property, it's an assignment:
8590
# otherwise it's an OSM key
86-
def method_missing(method_sym, *arguments)
91+
def add_property(method_sym, *arguments)
8792
if EXPRESSIONS.include?(method_sym)
8893
Condition.new.from_list(method_sym, arguments)
8994
elsif LAYOUT.include?(method_sym) || PAINT.include?(method_sym) || TOP_LEVEL.include?(method_sym)
@@ -135,7 +140,7 @@ def on(*args, &block)
135140
sub_cond = nilsafe_merge(sub_cond, @condition)
136141
end
137142
r._set_filter(nilsafe_merge(sub_cond, @uncascaded))
138-
r.instance_eval(&block)
143+
r.dsl_eval(&block)
139144
@stylesheet._add_layer(r)
140145

141146
# Create cascaded layers
@@ -159,7 +164,7 @@ def nilsafe_merge(a, b) # rubocop:disable Naming/MethodParameterName
159164
def cascade(*args, &block)
160165
cond = args.length == 1 ? args[0] : Condition.new.from_list(:any, args)
161166
@cascade_cond = cond
162-
instance_eval(&block)
167+
dsl_eval(&block)
163168
@cascade_cond = nil
164169
end
165170

lib/glug/layer_dsl.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
module Glug
4+
# Defines the methods available to the DSL
5+
class LayerDSL
6+
def initialize(impl)
7+
@__impl = impl
8+
end
9+
10+
def literal(*args)
11+
@__impl.literal(*args)
12+
end
13+
14+
def current_value(key)
15+
@__impl.current_value(key)
16+
end
17+
18+
def on(*args, &block)
19+
@__impl.on(*args, &block)
20+
end
21+
22+
def cascade(*args, &block)
23+
@__impl.cascade(*args, &block)
24+
end
25+
26+
def uncascaded(*args)
27+
@__impl.uncascaded(*args)
28+
end
29+
30+
def filter(*args)
31+
@__impl.filter(*args)
32+
end
33+
34+
def id(name)
35+
@__impl.id(name)
36+
end
37+
38+
def suppress
39+
@__impl.suppress
40+
end
41+
42+
def any
43+
@__impl.any
44+
end
45+
46+
def all
47+
@__impl.all
48+
end
49+
50+
def respond_to_missing?(*)
51+
true
52+
end
53+
54+
def method_missing(method_sym, *arguments)
55+
@__impl.add_property(method_sym, *arguments)
56+
end
57+
end
58+
end

lib/glug/stylesheet.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def source(source_name, opts = {})
3030
def layer(id, opts = {}, &block)
3131
r = Layer.new(self, id: id, kv: opts)
3232
@layers << r
33-
r.instance_eval(&block)
33+
r.dsl_eval(&block)
3434
end
3535

3636
# Assemble into GL JSON format

spec/lib/glug/layer_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
source :osm_data, type: 'vector', url: 'http://example.com/osm.tilejson', default: true
88
end
99
l = described_class.new(stylesheet, { zoom: 7 })
10-
l.line_width 1
10+
l.add_property(:line_width, 1)
1111

1212
h = l.to_hash
1313
expect(h['minzoom']).to be(7)
@@ -19,7 +19,7 @@
1919
source :osm_data, type: 'vector', url: 'http://example.com/osm.tilejson', default: true
2020
end
2121
l = described_class.new(stylesheet, { zoom: 1..5 })
22-
l.line_width 1
22+
l.add_property(:line_width, 1)
2323

2424
h = l.to_hash
2525
expect(h['minzoom']).to be(1)
@@ -32,7 +32,7 @@
3232
source :osm_data, type: 'vector', url: 'http://example.com/osm.tilejson', default: true
3333
end
3434
l = described_class.new(stylesheet, { zoom: 1.. })
35-
l.line_width 1
35+
l.add_property(:line_width, 1)
3636

3737
h = l.to_hash
3838
expect(h).to have_key('minzoom')
@@ -45,7 +45,7 @@
4545
source :osm_data, type: 'vector', url: 'http://example.com/osm.tilejson', default: true
4646
end
4747
l = described_class.new(stylesheet, { zoom: ..5 })
48-
l.line_width 1
48+
l.add_property(:line_width, 1)
4949

5050
h = l.to_hash
5151
expect(h).not_to have_key('minzoom')

0 commit comments

Comments
 (0)