Class: ViewAssets::Finder::Directive

Inherits:
Object
  • Object
show all
Defined in:
lib/view_assets/finder/directive.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(asset_type) ⇒ Directive

Returns a new instance of Directive.



5
6
7
8
9
10
11
12
13
# File 'lib/view_assets/finder/directive.rb', line 5

def initialize(asset_type)
  # TODO find out asset_types that can run in both directive.rb and asset_finders
  # raise ConfigurationError.new('asset type should be "js" or "css"') unless %w(css js).include?(asset_type)
  if [JS_TYPE, CSS_TYPE].include?(asset_type)
    @asset_type = asset_type
  else
    raise ConfigurationError.new("asset type should be '#{JS_TYPE}' or '#{CSS_TYPE}'")
  end
end

Instance Attribute Details

#asset_typeObject (readonly)

Returns the value of attribute asset_type.



4
5
6
# File 'lib/view_assets/finder/directive.rb', line 4

def asset_type
  @asset_type
end

Instance Method Details

#app_directiveObject



73
74
75
# File 'lib/view_assets/finder/directive.rb', line 73

def app_directive
  @app_directive ||= generate_formula
end

TODO add docs

Returns:

  • (Boolean)


22
23
24
# File 'lib/view_assets/finder/directive.rb', line 22

def legal_directive?(primitive_params)
  [vendor_directive, lib_directive, app_directive].any? { |d| d =~ primitive_params }
end

#lib_directiveObject



69
70
71
# File 'lib/view_assets/finder/directive.rb', line 69

def lib_directive
  @lib_directive ||= generate_formula 'require_lib'
end

#parse(primitive_params) ⇒ Object

return root folder and all the path params that have been split TODO this method bellow need refactor



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/view_assets/finder/directive.rb', line 28

def parse(primitive_params)
  asset_root = ''
  path_param_str = ''
  path_params = []
  # unknown_directive = false

  # TODO make sure path_param_str will return nil for non-matched result and array for matched result
  # rememer to write tests for this section of codes
  if vendor_directive =~ primitive_params
    asset_root = 'vendor'
    path_param_str = primitive_params.match(vendor_directive)[:path_params]
  elsif lib_directive =~ primitive_params
    asset_root = 'lib'
    path_param_str = primitive_params.match(lib_directive)[:path_params]
  elsif app_directive =~ primitive_params
    asset_root = 'app'
    path_param_str = primitive_params.match(app_directive)[:path_params]
  # else
  #   # TODO remove UnknownDirectiveError or try to find another way to get thing done
  #   # raise UnknownDirectiveError.new "'#{primitive_params}' is not in legal directive format"
  #   unknown_directive = true
  end

  # TODO refactor codes bellow after the above paragraph was refactored
  would_be_path_params = path_param_str.strip.split(/,\s?/)
  # would_be_path_params = [would_be_path_params] if would_be_path_params.kind_of?(String)
  path_params = would_be_path_params unless would_be_path_params.empty?

  # unknown_directive ? [nil, nil] : [asset_root, path_params.strip.split(/,\s?/)]
  # TODO: add rspec examples for returning a ['', []] when the primitive_params is illegal
  [asset_root, path_params]
end

#vendor_directiveObject

def all_directives

/#{tree_directive}|#{file_directive}/

end



65
66
67
# File 'lib/view_assets/finder/directive.rb', line 65

def vendor_directive
  @vendor_directive ||= generate_formula 'require_vendor'
end