Class: Gitmoji::Regex::Reference

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/gitmoji/regex/reference.rb

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

Instance Method Details

#cached_patternRegexp

Regex generated from the cached JSON file.

Returns:

  • (Regexp)


40
41
42
# File 'lib/gitmoji/regex/reference.rb', line 40

def cached_pattern
  pattern(cached)
end

#compare_jsonBoolean

Compare the cached JSON-derived regex with the latest fetched regex.

Returns:

  • (Boolean)

    true if the regex generated from the cached JSON matches the regex generated from the freshly fetched JSON.



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_srcBoolean

Compare the current library source with the next generated source.

Returns:

  • (Boolean)

    true if no changes are needed (files are equal).



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_srcString

Read the current library source file that defines REGEX.

Returns:

  • (String)


61
62
63
# File 'lib/gitmoji/regex/reference.rb', line 61

def current_src
  File.read(LIB_SRC)
end

#fetched_patternRegexp

Regex generated from the fetched remote JSON.

Returns:

  • (Regexp)


46
47
48
# File 'lib/gitmoji/regex/reference.rb', line 46

def fetched_pattern
  pattern(fetch)
end

#next_srcString

Build the next library source content from the template and fetched data.

Returns:

  • (String)

    the prospective library source content.



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

#to_a(body = nil) ⇒ Array<String>

Parse a JSON body and return the list of emoji strings.

Parameters:

  • body (String, nil) (defaults to: nil)

    the JSON document to parse; defaults to cached JSON

Returns:

  • (Array<String>)

    list of emoji characters



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_jsonInteger

Write the freshly fetched JSON to the cache file.

Also clears memoized cached values.

Returns:

  • (Integer)

    number of bytes written



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_srcInteger

Regenerate the library file from the template and fetched data.

Returns:

  • (Integer)

    number of bytes written



97
98
99
# File 'lib/gitmoji/regex/reference.rb', line 97

def write_src
  File.write(LIB_SRC, next_src)
end