Class: KCommercial::Resources::StarSource::PathSource

Inherits:
Object
  • Object
show all
Defined in:
lib/KCommercialPipeline/core/resource/source/star_source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(assets_name, assets_type, path, app = 'default') ⇒ PathSource

Returns a new instance of PathSource.



13
14
15
16
17
18
19
20
# File 'lib/KCommercialPipeline/core/resource/source/star_source.rb', line 13

def initialize(assets_name, assets_type, path, app = 'default')
  @is_common_asset = app == 'default'
  @app = app
  @assets_name = assets_name
  @assets_type = assets_type
  @assets = []
  prepare(path)
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



11
12
13
# File 'lib/KCommercialPipeline/core/resource/source/star_source.rb', line 11

def app
  @app
end

#assetsObject

Returns the value of attribute assets.



11
12
13
# File 'lib/KCommercialPipeline/core/resource/source/star_source.rb', line 11

def assets
  @assets
end

#assets_nameObject

Returns the value of attribute assets_name.



11
12
13
# File 'lib/KCommercialPipeline/core/resource/source/star_source.rb', line 11

def assets_name
  @assets_name
end

#assets_typeObject

Returns the value of attribute assets_type.



11
12
13
# File 'lib/KCommercialPipeline/core/resource/source/star_source.rb', line 11

def assets_type
  @assets_type
end

#is_common_assetObject (readonly)

is common resources



10
11
12
# File 'lib/KCommercialPipeline/core/resource/source/star_source.rb', line 10

def is_common_asset
  @is_common_asset
end

Instance Method Details

#assets_from_directory(directory, asset_class) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/KCommercialPipeline/core/resource/source/star_source.rb', line 22

def assets_from_directory(directory, asset_class)
  asset_paths = directory.glob('**/*.cdasset')
  asset_paths.map do |path|
    name = path.basename('.*').to_s
    asset_class.new(name, path)
  end
end

#merge(path_source) ⇒ Object

合并数据源,被merge的优先级较高,会替换前边的



60
61
62
63
64
65
66
67
# File 'lib/KCommercialPipeline/core/resource/source/star_source.rb', line 60

def merge(path_source)
  raise 'merge error,raise' unless path_source.assets_type == assets_type

  if path_source != self
    result = @assets + path_source.assets
    @assets  = result.uniq(&:name)
  end
end

#merge_assets(assets) ⇒ Object



53
54
55
56
57
58
# File 'lib/KCommercialPipeline/core/resource/source/star_source.rb', line 53

def merge_assets(assets)
  if @assets != assets && assets
    result = assets + @assets
    @assets = result.uniq(&:name)
  end
end

#prepare(path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/KCommercialPipeline/core/resource/source/star_source.rb', line 30

def prepare(path)
  return [] unless @assets_name && path
  asset_paths = []
  if is_common_asset
    asset_paths << path.join(@assets_name.to_s)
  else
    asset_paths = path.glob("**/#{@assets_name}")

  end
  asset_paths.each do |path|
    if [ KCommercial::Resources::AssetType::Font].include? @assets_type
      dir_assets = assets_from_directory path, HashAsset
    elsif [KCommercial::Resources::AssetType::Color, KCommercial::Resources::AssetType::I18n].include? @assets_type
      dir_assets = assets_from_directory path, StringAsset
    elsif [KCommercial::Resources::AssetType::Image].include? @assets_type
      dir_assets = assets_from_directory path, ArrayHashAsset
    else
      dir_assets = assets_from_directory path, Asset
    end
    merge_assets(dir_assets)
  end
end