Class: TsAssets::ApplicationGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/ts_assets/application_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(include:, es_module_interop: false) ⇒ ApplicationGenerator

Returns a new instance of ApplicationGenerator.

Parameters:

  • include (String)


14
15
16
17
18
19
# File 'lib/ts_assets/application_generator.rb', line 14

def initialize(include:, es_module_interop: false)
  @mapping = build_mapping(include)
  @es_module_interop = es_module_interop

  environment.append_path(include)
end

Instance Attribute Details

#es_module_interopBoolean (readonly)

Returns:

  • (Boolean)


11
12
13
# File 'lib/ts_assets/application_generator.rb', line 11

def es_module_interop
  @es_module_interop
end

#mappingHash (readonly)

Returns mapping.

Returns:

  • (Hash)

    mapping



8
9
10
# File 'lib/ts_assets/application_generator.rb', line 8

def mapping
  @mapping
end

Instance Method Details

#build_mapping(include_path) ⇒ Hash

Parameters:

  • include_path (String)

Returns:

  • (Hash)


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ts_assets/application_generator.rb', line 50

def build_mapping(include_path)
  mapping = {}

  Dir.glob("#{include_path}/**/*.{png,svg,jpg,jpeg,gif}").sort.each do |full_path|
    mapping[full_path] = TsAssets::Models::AssetMetaInfo.new(
      full_path: full_path,
      include_path: include_path,
      environment: environment,
    )
  end

  mapping
end

#const_contentTsAssets::Models::Content



39
40
41
# File 'lib/ts_assets/application_generator.rb', line 39

def const_content
  @const_content ||= TsAssets::Generators::ConstGenerator.new(mapping).generate
end

#environmentSprockets::Environment

Returns:

  • (Sprockets::Environment)


22
23
24
# File 'lib/ts_assets/application_generator.rb', line 22

def environment
  @environment ||= Sprockets::Environment.new
end

#generateString

Returns:

  • (String)


27
28
29
30
31
32
33
34
35
36
# File 'lib/ts_assets/application_generator.rb', line 27

def generate
  [ # header
    react_content.header,

    # body
    const_content.body,
    react_content.body,

  ].join("\n")
end

#react_contentTsAssets::Models::Content



44
45
46
# File 'lib/ts_assets/application_generator.rb', line 44

def react_content
  @react_content ||= TsAssets::Generators::ReactGenerator.new(mapping, es_module_interop: es_module_interop).generate
end