Skip to content

Commit b5664c0

Browse files
authored
Tidy up creation of predicate methods. (#64)
1 parent 85ae9db commit b5664c0

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

lib/h3/bindings/base.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ def self.extended(base)
1818
base.typedef :pointer, :output_buffer
1919
base.const_set("H3_INDEX", :ulong_long)
2020
end
21+
22+
def attach_predicate_function(name, *args)
23+
stripped_name = name.to_s.gsub("?", "")
24+
attach_function(stripped_name, *args).tap do
25+
rename_function stripped_name, name
26+
end
27+
end
28+
29+
private
30+
31+
def rename_function(from, to)
32+
alias_method to, from
33+
undef_method from
34+
end
2135
end
2236
end
2337
end

lib/h3/inspection.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ def string_to_h3(string)
7676
# true
7777
#
7878
# @return [Boolean] True if the H3 index is a pentagon.
79-
attach_function :pentagon, :h3IsPentagon, %i[h3_index], :bool
80-
alias_method :pentagon?, :pentagon
81-
undef_method :pentagon
79+
attach_predicate_function :pentagon?, :h3IsPentagon, %i[h3_index], :bool
8280

8381
# @deprecated Please use {#pentagon?} instead.
8482
def h3_pentagon?(h3_index)
@@ -98,9 +96,7 @@ def h3_pentagon?(h3_index)
9896
# true
9997
#
10098
# @return [Boolean] True if the H3 index has a class III resolution.
101-
attach_function :class_3_resolution, :h3IsResClassIII, %i[h3_index], :bool
102-
alias_method :class_3_resolution?, :class_3_resolution
103-
undef_method :class_3_resolution
99+
attach_predicate_function :class_3_resolution?, :h3IsResClassIII, %i[h3_index], :bool
104100

105101
# @deprecated Please use {#class_3_resolution?} instead.
106102
def h3_res_class_3?(h3_index)
@@ -119,9 +115,7 @@ def h3_res_class_3?(h3_index)
119115
# true
120116
#
121117
# @return [Boolean] True if the H3 index is valid.
122-
attach_function :valid, :h3IsValid, %i[h3_index], :bool
123-
alias_method :valid?, :valid
124-
undef_method :valid
118+
attach_predicate_function :valid?, :h3IsValid, %i[h3_index], :bool
125119

126120
# @deprecated Please use {#valid?} instead.
127121
def h3_valid?(h3_index)

lib/h3/unidirectional_edges.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ module UnidirectionalEdges
1717
# true
1818
#
1919
# @return [Boolean] True if indexes are neighbors
20-
attach_function :neighbors, :h3IndexesAreNeighbors, %i[h3_index h3_index], :bool
21-
alias_method :neighbors?, :neighbors
22-
undef_method :neighbors
20+
attach_predicate_function :neighbors?, :h3IndexesAreNeighbors, %i[h3_index h3_index], :bool
2321

2422
# @deprecated Please use {#neighbors?} instead.
2523
def h3_indexes_neighbors?(origin, destination)
@@ -39,12 +37,10 @@ def h3_indexes_neighbors?(origin, destination)
3937
# true
4038
#
4139
# @return [Boolean] True if H3 index is a valid unidirectional edge
42-
attach_function :unidirectional_edge_valid,
43-
:h3UnidirectionalEdgeIsValid,
44-
%i[h3_index],
45-
:bool
46-
alias_method :unidirectional_edge_valid?, :unidirectional_edge_valid
47-
undef_method :unidirectional_edge_valid
40+
attach_predicate_function :unidirectional_edge_valid?,
41+
:h3UnidirectionalEdgeIsValid,
42+
%i[h3_index],
43+
:bool
4844

4945
# @deprecated Please use {#unidirectional_edge_valid?} instead.
5046
def h3_unidirectional_edge_valid?(h3_index)

0 commit comments

Comments
 (0)