Class: ViewAssets::AssetsFinder

Inherits:
Struct
  • Object
show all
Defined in:
lib/view_assets/assets_finder.rb

Overview

It’s an abstract class.

Direct Known Subclasses

JavascriptAssets, StyleSheetAssets

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ AssetsFinder

Returns a new instance of AssetsFinder.



17
18
19
20
21
22
# File 'lib/view_assets/assets_finder.rb', line 17

def initialize(*args)
  @all_assets = []
  @retrieved = false
  
  super(*args)
end

Instance Attribute Details

#action_nameObject

Returns the value of attribute action_name

Returns:

  • (Object)

    the current value of action_name



15
16
17
# File 'lib/view_assets/assets_finder.rb', line 15

def action_name
  @action_name
end

#controller_nameObject

Returns the value of attribute controller_name

Returns:

  • (Object)

    the current value of controller_name



15
16
17
# File 'lib/view_assets/assets_finder.rb', line 15

def controller_name
  @controller_name
end

#rootObject

Returns the value of attribute root

Returns:

  • (Object)

    the current value of root



15
16
17
# File 'lib/view_assets/assets_finder.rb', line 15

def root
  @root
end

Instance Method Details

#action_assetsObject

If the action assets is only a file, finder will also consider it a manifest file. If the action assets is a foler consisting several asset files, finder will includes all the assets inside this folder. Among these files, a file named index. will be taken as manifest file.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/view_assets/assets_finder.rb', line 109

def action_assets
  return @action_assets unless @action_assets.nil?
  
  @action_assets = []
  action_path = "#{ root }/#{ app_path }/#{ controller_name }"
  single_action_path = "#{ action_path }/#{ action_name }.#{ asset_extension }"
  indexed_action_path = "#{ action_path }/#{ action_name }/index.#{ asset_extension }"

  # find files in the conventional directory
  manifest = nil
  manifest = single_action_path if FileTest.exist?(single_action_path)
  manifest = indexed_action_path if FileTest.exist?(indexed_action_path)

  # TODO add rspec example
  return @action_assets if manifest.nil?

  @action_assets = manifest.nil? ? [] : retrieve_assets_from(manifest)
  @action_assets << unabsolutely_pathize(manifest)
end

#allObject

This method is the ENTRY of assets finder after its initializtion. It returns all asset paths wrapped inside a appropriated html tag(‘script` | `link`).



34
35
36
37
# File 'lib/view_assets/assets_finder.rb', line 34

def all
  # todo remove this quick fix used for adding a leading slash to make 
  all_assets.map { |asset| tag "/#{asset}" } # tag should be realized in a subclass
end

#all_assetsObject

“untagged” means hasn’t been wrapped inside a appropriated html tag like ‘script` or `link`



49
50
51
52
53
54
55
# File 'lib/view_assets/assets_finder.rb', line 49

def all_assets
  retrieve unless retrieved?

  verify if TO_VERIFY
  
  @all_assets
end

#controller_assetsObject

The env assets are assets that will be required before action assets. The function of env assets is to allow user to require some assets that would be used throughout the whole application or controller. Like views in rails, assets finder will use application. existed in /app/assets/ folder if:controller. in /app/assets//:controller is not existed.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/view_assets/assets_finder.rb', line 85

def controller_assets
  return @controller_assets unless @controller_assets.nil?
  
  @controller_assets = []
  application_manifest = "#{ root }/#{ app_path }/application.#{ asset_extension }"
  controller_manifest = "#{ root }/#{ app_path }/#{ controller_name }/#{ controller_name }.#{ asset_extension }"

  manifest = nil
  manifest = application_manifest if FileTest.exist?(application_manifest)
  manifest = controller_manifest if FileTest.exist?(controller_manifest)
  
  # TODO add rspec example
  return @controller_assets if manifest.nil?

  @controller_assets = manifest.nil? ? [] : retrieve_assets_from(manifest)
  @controller_assets << unabsolutely_pathize(manifest)
end

#fullObject

get all the asset paths in full path TODO realize this method



42
43
44
# File 'lib/view_assets/assets_finder.rb', line 42

def full
  all_assets.map { |asset| absolutely_pathize(asset) }
end

#retrieveObject

TODO document



58
59
60
61
# File 'lib/view_assets/assets_finder.rb', line 58

def retrieve
  @all_assets = controller_assets.concat(action_assets).uniq if @all_assets.empty?
  @retrieved = true
end

#retrieved?Boolean

TODO document

Returns:

  • (Boolean)


64
65
66
# File 'lib/view_assets/assets_finder.rb', line 64

def retrieved?
  @retrieved
end

#verifyObject

Check out whether all assets is existed or not It is better to be turned off in production



71
72
73
74
75
76
# File 'lib/view_assets/assets_finder.rb', line 71

def verify
  all_assets.each do |asset| 
    asset_file = absolutely_pathize(asset)
    raise AssetNotFound.new("File #{ asset } DOEST EXIST") if FileTest.exist?(asset_file)
  end
end