Class: Gitmoji::Regex::Reference
- Inherits:
-
Object
- Object
- Gitmoji::Regex::Reference
- Includes:
- Singleton
- Defined in:
- lib/gitmoji/regex/reference.rb,
sig/gitmoji/regex.rbs
Overview
Reference provides utility tools for maintaining and testing this gem.
It can compare the cached upstream gitmoji list with the latest fetched list, and can regenerate the library source from a template to include an up-to-date regular expression.
Constant Summary collapse
- GITMOJI_REFERENCE =
Remote source of truth for the gitmoji JSON document.
"https://raw.githubusercontent.com/carloscuesta/gitmoji/master/packages/gitmojis/src/gitmojis.json"- GITMOJI_PATH =
Path to the cached JSON document bundled in this repository.
"src/gitmojis.json"- LIB_SRC =
Path to the library file that contains the REGEX constant.
"lib/gitmoji/regex.rb"- TEMPLATE_SRC =
Path to the ERB-like template that is used to generate LIB_SRC.
"src/regex.rb"
Instance Method Summary collapse
-
#cached ⇒ String
Read the cached gitmoji JSON from disk.
-
#cached_pattern ⇒ Regexp
Regex generated from the cached JSON file.
-
#compare_json ⇒ Boolean
Compare the cached JSON-derived regex with the latest fetched regex.
-
#compare_src ⇒ Boolean
Compare the current library source with the next generated source.
-
#current_src ⇒ String
Read the current library source file that defines REGEX.
-
#fetch ⇒ String
Fetch the latest gitmoji JSON from the remote source.
-
#fetched_pattern ⇒ Regexp
Regex generated from the fetched remote JSON.
-
#next_src ⇒ String
Build the next library source content from the template and fetched data.
-
#pattern(body) ⇒ Regexp
Build the union regex from a JSON document body.
-
#to_a(body = nil) ⇒ Array<String>
Parse a JSON body and return the list of emoji strings.
-
#write_json ⇒ Integer
Write the freshly fetched JSON to the cache file.
-
#write_src ⇒ Integer
Regenerate the library file from the template and fetched data.
Instance Method Details
#cached ⇒ String
Read the cached gitmoji JSON from disk.
118 119 120 |
# File 'lib/gitmoji/regex/reference.rb', line 118 def cached @cached ||= File.read(GITMOJI_PATH) end |
#cached_pattern ⇒ Regexp
Regex generated from the cached JSON file.
40 41 42 |
# File 'lib/gitmoji/regex/reference.rb', line 40 def cached_pattern pattern(cached) end |
#compare_json ⇒ Boolean
Compare the cached JSON-derived regex with the latest fetched regex.
32 33 34 35 36 |
# File 'lib/gitmoji/regex/reference.rb', line 32 def compare_json return true if cached_pattern == fetched_pattern false end |
#compare_src ⇒ Boolean
Compare the current library source with the next generated source.
53 54 55 56 57 |
# File 'lib/gitmoji/regex/reference.rb', line 53 def compare_src return true if current_src == next_src false end |
#current_src ⇒ String
Read the current library source file that defines REGEX.
61 62 63 |
# File 'lib/gitmoji/regex/reference.rb', line 61 def current_src File.read(LIB_SRC) end |
#fetch ⇒ String
Fetch the latest gitmoji JSON from the remote source.
112 113 114 |
# File 'lib/gitmoji/regex/reference.rb', line 112 def fetch @fetch ||= HTTP.get(GITMOJI_REFERENCE).body end |
#fetched_pattern ⇒ Regexp
Regex generated from the fetched remote JSON.
46 47 48 |
# File 'lib/gitmoji/regex/reference.rb', line 46 def fetched_pattern pattern(fetch) end |
#next_src ⇒ String
Build the next library source content from the template and fetched data.
67 68 69 70 71 |
# File 'lib/gitmoji/regex/reference.rb', line 67 def next_src template_src = File.read(TEMPLATE_SRC) template_src = template_src.sub("% gitmojiRegex %", pattern(fetch).to_s) template_src.to_s end |
#pattern(body) ⇒ Regexp
Build the union regex from a JSON document body.
106 107 108 |
# File 'lib/gitmoji/regex/reference.rb', line 106 def pattern(body) Regexp.union(to_a(body)) end |
#to_a(body = nil) ⇒ Array<String>
Parse a JSON body and return the list of emoji strings.
77 78 79 80 81 82 |
# File 'lib/gitmoji/regex/reference.rb', line 77 def to_a(body = nil) body ||= cached json = JSON.parse(body) gitmoji = json["gitmojis"] gitmoji.map { |g| g["emoji"] } end |
#write_json ⇒ Integer
Write the freshly fetched JSON to the cache file.
Also clears memoized cached values.
88 89 90 91 92 93 |
# File 'lib/gitmoji/regex/reference.rb', line 88 def write_json file = File.write(GITMOJI_PATH, fetch) @cached = nil @cached_pattern = nil file end |
#write_src ⇒ Integer
Regenerate the library file from the template and fetched data.
97 98 99 |
# File 'lib/gitmoji/regex/reference.rb', line 97 def write_src File.write(LIB_SRC, next_src) end |