ruflet_core 0.0.22 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f3e8b0865b88790b85de1ad455a9b262603722e1919b25af614994765794d704
4
- data.tar.gz: 8f16bbf7b16594ef9a4ff31d91e7fac5b67d36edd26021f297be09a110da95cf
3
+ metadata.gz: b7c6e921c44aa57801c14bb2bce64088b8b92b3759d6476a76db284cc74f5e00
4
+ data.tar.gz: 92970e55579384280481a14a37d76724a0f820be79281f668b462321bb4eb005
5
5
  SHA512:
6
- metadata.gz: e97396572fb70e02900174cc177dc435159083d844f4f44a6dd9c854543af13eb866cbfd17265ac6c4239f260bc78e171eb02cfd16c564bf1e2d3203241d8661
7
- data.tar.gz: e1db85c6c73756534c68018ee32803beda36d50237511e259ba693c269a0c671e0432e4417083b99bad0feb50b984244c16f33eab36d755930c833496fff27fc
6
+ metadata.gz: 733714ee9e1c916d6c5b4f402163bd7caaa34df1fd4c4f07e0e49dbcb4e324b8287c70fe753d35b376039e02d227810a7856bc5a02cb8920ee51069862533462
7
+ data.tar.gz: fbf901236bac15ec93b15175e93bc2b49bfb3c17ac26dd0fd453085900d7e180fec4b76fc7b54fca265823f3e1b618cdfc91e1ed12b09e3b59bb5ad21a921c79
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ruflet
4
- VERSION = "0.0.22" unless const_defined?(:VERSION)
4
+ VERSION = "0.0.23" unless const_defined?(:VERSION)
5
5
  end
@@ -5,46 +5,46 @@ require_relative "../icon_constant_names"
5
5
  require_relative "../cupertino_icon_lookup"
6
6
 
7
7
  module Ruflet
8
+ # Cupertino icon names, exposed as constants. Materialized on demand for the
9
+ # same reason as Ruflet::MaterialIcons -- see the note there.
8
10
  module CupertinoIcons
9
11
  module_function
10
12
 
11
- ICONS = begin
12
- source = Ruflet::CupertinoIconLookup.icon_map
13
- if source.empty?
14
- {
15
- HOME: "house",
16
- SEARCH: "search",
17
- SETTINGS: "gear",
18
- ADD: "add",
19
- CLOSE: "clear"
20
- }
21
- else
22
- source.keys.each_with_object({}) do |name, result|
23
- text = Ruflet::IconNames.constant_for(name)
24
- result[text.upcase.to_sym] = name
25
- end
26
- end.freeze
27
- end
28
-
29
- ICONS.each do |const_name, icon_name|
30
- next if const_defined?(const_name, false)
13
+ FALLBACK_ICONS = {
14
+ HOME: "house",
15
+ SEARCH: "search",
16
+ SETTINGS: "gear",
17
+ ADD: "add",
18
+ CLOSE: "clear"
19
+ }.freeze
31
20
 
32
- const_set(const_name, icon_name.to_s.downcase)
21
+ def icons
22
+ @icons ||= begin
23
+ source = Ruflet::CupertinoIconLookup.icon_map
24
+ if source.empty?
25
+ FALLBACK_ICONS
26
+ else
27
+ source.keys.each_with_object({}) do |name, result|
28
+ text = Ruflet::IconNames.constant_for(name)
29
+ result[text.upcase.to_sym] = name
30
+ end
31
+ end.freeze
32
+ end
33
33
  end
34
34
 
35
35
  def [](name)
36
- key = name.to_s.upcase.to_sym
37
- return const_get(key) if const_defined?(key, false)
36
+ icon = icons[name.to_s.upcase.to_sym]
37
+ return icon.to_s.downcase unless icon.nil?
38
38
 
39
39
  Ruflet::CupertinoIconLookup.canonical_name_for(name) || name.to_s
40
40
  end
41
41
 
42
42
  def constants(_inherit = true)
43
- ICONS.keys
43
+ icons.keys
44
44
  end
45
45
 
46
46
  def all
47
- ICONS.keys.map { |k| const_get(k) }
47
+ icons.values.map { |name| name.to_s.downcase }
48
48
  end
49
49
 
50
50
  def random
@@ -52,7 +52,27 @@ module Ruflet
52
52
  end
53
53
 
54
54
  def names
55
- ICONS.values
55
+ icons.values
56
+ end
57
+
58
+ class << self
59
+ # See Ruflet::MaterialIcons.resolve_constant.
60
+ def resolve_constant(name)
61
+ return const_get(name) if const_defined?(name, false)
62
+ return const_set(:ICONS, icons) if name == :ICONS
63
+
64
+ key = name.to_s
65
+ source = Ruflet::CupertinoIconLookup.icon_map
66
+ return const_set(name, key.downcase) if source.key?(key)
67
+
68
+ icon = icons[name]
69
+ icon.nil? ? nil : const_set(name, icon.to_s.downcase)
70
+ end
71
+
72
+ def const_missing(name)
73
+ resolved = resolve_constant(name)
74
+ resolved.nil? ? super : resolved
75
+ end
56
76
  end
57
77
  end
58
78
  end
@@ -5,46 +5,55 @@ require_relative "../icon_constant_names"
5
5
  require_relative "../material_icon_lookup"
6
6
 
7
7
  module Ruflet
8
+ # Material icon names, exposed as constants: Ruflet::MaterialIcons::HOME.
9
+ #
10
+ # The constants are materialized on demand rather than at load. Building all
11
+ # of them eagerly meant interning ~8,800 symbols, filling a hash with them and
12
+ # calling const_set ~8,800 times every time the VM opened -- on the embedded
13
+ # mruby runtime that was the single largest item in application cold start,
14
+ # and an app typically names a handful of icons. const_missing keeps the
15
+ # constant syntax working, so nothing at the call site changes.
8
16
  module MaterialIcons
9
17
  module_function
10
18
 
11
- ICONS = begin
12
- source = Ruflet::MaterialIconLookup.icon_map
13
- if source.empty?
14
- {
15
- HOME: "home",
16
- SETTINGS: "settings",
17
- SEARCH: "search",
18
- ADD: "add",
19
- CLOSE: "close"
20
- }
21
- else
22
- source.keys.each_with_object({}) do |name, result|
23
- text = Ruflet::IconNames.constant_for(name)
24
- result[text.upcase.to_sym] = name
25
- end
26
- end.freeze
27
- end
28
-
29
- ICONS.each do |const_name, icon_name|
30
- next if const_defined?(const_name, false)
19
+ FALLBACK_ICONS = {
20
+ HOME: "home",
21
+ SETTINGS: "settings",
22
+ SEARCH: "search",
23
+ ADD: "add",
24
+ CLOSE: "close"
25
+ }.freeze
31
26
 
32
- const_set(const_name, icon_name.to_s.downcase)
27
+ # Constant name -> icon name, e.g. :ACCESS_ALARM => "ACCESS_ALARM". Built on
28
+ # first use by anything that needs the whole table; single constant lookups
29
+ # do not need it at all (see const_missing).
30
+ def icons
31
+ @icons ||= begin
32
+ source = Ruflet::MaterialIconLookup.icon_map
33
+ if source.empty?
34
+ FALLBACK_ICONS
35
+ else
36
+ source.keys.each_with_object({}) do |name, result|
37
+ text = Ruflet::IconNames.constant_for(name)
38
+ result[text.upcase.to_sym] = name
39
+ end
40
+ end.freeze
41
+ end
33
42
  end
34
43
 
35
44
  def [](name)
36
- key = name.to_s.upcase.to_sym
37
- return const_get(key) if const_defined?(key, false)
45
+ icon = icons[name.to_s.upcase.to_sym]
46
+ return icon.to_s.downcase unless icon.nil?
38
47
 
39
48
  Ruflet::MaterialIconLookup.canonical_name_for(name) || name.to_s
40
49
  end
41
50
 
42
51
  def constants(_inherit = true)
43
- ICONS.keys
52
+ icons.keys
44
53
  end
45
54
 
46
55
  def all
47
- ICONS.keys.map { |k| const_get(k) }
56
+ icons.values.map { |name| name.to_s.downcase }
48
57
  end
49
58
 
50
59
  def random
@@ -52,8 +61,34 @@ module Ruflet
52
61
  end
53
62
 
54
63
  def names
55
- ICONS.values
64
+ icons.values
56
65
  end
57
66
 
67
+ class << self
68
+ # Materializes one icon constant, or returns nil when the name is not an
69
+ # icon. Callers that used to probe with const_defined?(name, false) must
70
+ # come through here instead: with deferred constants, const_defined? is
71
+ # false for every icon nobody has referenced yet.
72
+ def resolve_constant(name)
73
+ return const_get(name) if const_defined?(name, false)
74
+
75
+ # ICONS stayed a public constant for callers that want the whole table.
76
+ return const_set(:ICONS, icons) if name == :ICONS
77
+
78
+ # Most constant names are already a key of the compiled map, so the
79
+ # common case is one hash hit and never builds the reverse table.
80
+ key = name.to_s
81
+ source = Ruflet::MaterialIconLookup.icon_map
82
+ return const_set(name, key.downcase) if source.key?(key)
83
+
84
+ icon = icons[name]
85
+ icon.nil? ? nil : const_set(name, icon.to_s.downcase)
86
+ end
87
+
88
+ def const_missing(name)
89
+ resolved = resolve_constant(name)
90
+ resolved.nil? ? super : resolved
91
+ end
92
+ end
58
93
  end
59
94
  end
data/lib/ruflet_ui.rb CHANGED
@@ -90,9 +90,8 @@ module Ruflet
90
90
  end
91
91
 
92
92
  def const_missing(name)
93
- return @icon_module.const_get(name) if @icon_module.const_defined?(name, false)
94
-
95
- super
93
+ resolved = @icon_module.resolve_constant(name)
94
+ resolved.nil? ? super : resolved
96
95
  end
97
96
  end
98
97
 
@@ -106,13 +105,11 @@ module Ruflet
106
105
  end
107
106
 
108
107
  def const_missing(name)
109
- if Ruflet::MaterialIcons.const_defined?(name, false)
110
- return Ruflet::MaterialIcons.const_get(name)
111
- end
108
+ resolved = Ruflet::MaterialIcons.resolve_constant(name)
109
+ return resolved unless resolved.nil?
112
110
 
113
- if Ruflet::CupertinoIcons.const_defined?(name, false)
114
- return Ruflet::CupertinoIcons.const_get(name)
115
- end
111
+ resolved = Ruflet::CupertinoIcons.resolve_constant(name)
112
+ return resolved unless resolved.nil?
116
113
 
117
114
  super
118
115
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruflet_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.22
4
+ version: 0.0.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - AdamMusa