Class: PhlexCustomElementGenerator::ManifestReader

Inherits:
Object
  • Object
show all
Defined in:
lib/phlex_custom_element_generator/manifest_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest:) ⇒ ManifestReader

Returns a new instance of ManifestReader.



20
21
22
23
# File 'lib/phlex_custom_element_generator/manifest_reader.rb', line 20

def initialize(manifest:)
  @manifest = manifest
  @manifest_json = read_manifest(manifest)
end

Instance Attribute Details

#manifestObject

Returns the value of attribute manifest.



5
6
7
# File 'lib/phlex_custom_element_generator/manifest_reader.rb', line 5

def manifest
  @manifest
end

#manifest_jsonObject

Returns the value of attribute manifest_json.



5
6
7
# File 'lib/phlex_custom_element_generator/manifest_reader.rb', line 5

def manifest_json
  @manifest_json
end

Instance Method Details

#find_all_custom_elementsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/phlex_custom_element_generator/manifest_reader.rb', line 25

def find_all_custom_elements
  json = @manifest_json
  custom_elements = {}

  json["modules"].flatten.each do |mod|
    parent_module = mod
    mod["declarations"].flatten.each do |dec|
      # Needs to be != true because == false fails nil checks.
      next if dec["customElement"] != true

      tag_name = dec["tagName"]

      dec["parent_module"] = parent_module
      custom_elements[tag_name] = dec if tag_name
    end
  end

  custom_elements
end

#list_tag_namesObject



45
46
47
# File 'lib/phlex_custom_element_generator/manifest_reader.rb', line 45

def list_tag_names
  find_all_custom_elements.keys
end

#read_manifest(str) ⇒ Object

Reads a given string and determins if it should fetch over HTTP, Read a file, or read the contents.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/phlex_custom_element_generator/manifest_reader.rb', line 8

def read_manifest(str)
  # return read_manifest_from_http(str) if str =~ /^https?:\/\//

  return JSON.parse(File.read(str)) if File.exist?(str)

  # Try finding it in node_modules?
  # return read_from_node_modules(str)

  # Fallback to the string and hope its valid JSON
  JSON.parse(str)
end