Class: Parcels::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/parcels/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sprockets_environment) ⇒ Environment



9
10
11
12
13
14
15
16
# File 'lib/parcels/environment.rb', line 9

def initialize(sprockets_environment)
  @sprockets_environment = sprockets_environment
  @widget_trees = [ ]
  @workaround_directories_for_widget_trees = { }
  @workaround_directories_root = nil

  register_engines!
end

Instance Attribute Details

#sprockets_environmentObject (readonly)

Returns the value of attribute sprockets_environment.



5
6
7
# File 'lib/parcels/environment.rb', line 5

def sprockets_environment
  @sprockets_environment
end

Instance Method Details

#add_all_widgets_to!(sprockets_context, set_names) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/parcels/environment.rb', line 52

def add_all_widgets_to!(sprockets_context, set_names)
  if widget_trees.length == 0
    raise %{Error: You have not defined any widget trees -- directories containing Fortitude widgets.
You must call #add_widget_tree! on the Parcels environment, which usually is accessible
as #parcels from your Sprockets environment.}
  end

  widget_trees.each { |wt| wt.add_all_widgets_to_sprockets_context!(sprockets_context, set_names) }
end

#add_widget_tree!(widget_tree_root) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/parcels/environment.rb', line 23

def add_widget_tree!(widget_tree_root)
  widget_tree_root = File.expand_path(widget_tree_root, root)
  widget_tree = widget_trees.detect { |wt| wt.root == widget_tree_root }

  if (! widget_tree)
    widget_tree = WidgetTree.new(self, widget_tree_root)
    widget_trees << widget_tree
  end

  widget_tree.add_workaround_directory_to_sprockets!(sprockets_environment)
end

#create_and_add_all_workaround_directories!Object



46
47
48
49
50
# File 'lib/parcels/environment.rb', line 46

def create_and_add_all_workaround_directories!
  # widget_trees.each do |widget_tree|
  #   widget_tree.add_workaround_directory_to_sprockets!(sprockets_environment)
  # end
end

#is_underneath_root?(filename) ⇒ Boolean



18
19
20
21
# File 'lib/parcels/environment.rb', line 18

def is_underneath_root?(filename)
  filename = File.expand_path(filename)
  filename.length > root.length && filename[0..(root.length - 1)] == root
end

#widget_class_from_file(full_path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/parcels/environment.rb', line 35

def widget_class_from_file(full_path)
  widget_trees.each do |widget_tree|
    if (removed = widget_tree.remove_workaround_directory_from(full_path))
      full_path = removed
      break
    end
  end

  ::Fortitude::Widget.widget_class_from_file(full_path, :root_dirs => widget_trees.map(&:widget_naming_root_dirs).flatten.uniq)
end

#workaround_directories_rootObject



72
73
74
# File 'lib/parcels/environment.rb', line 72

def workaround_directories_root
  @workaround_directories_root
end

#workaround_directories_root=(new_root) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/parcels/environment.rb', line 76

def workaround_directories_root=(new_root)
  new_root = File.expand_path(new_root)

  if @workaround_directories_for_widget_trees.size > 0 && @workaround_directories_root != new_root
    raise "You can't set the workaround directories root to:
#{new_root}
...it's already set to:
#{@workaround_directories_root}"
  end

  @workaround_directories_root = new_root
end

#workaround_directory_root_for_widget_tree(widget_tree) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/parcels/environment.rb', line 62

def workaround_directory_root_for_widget_tree(widget_tree)
  @workaround_directories_for_widget_trees[widget_tree] ||= begin
    if @workaround_directories_root
      File.join(@workaround_directories_root, workaround_directory_name_for(widget_tree))
    else
      File.join(widget_tree.root, PARCELS_WORKAROUND_DIRECTORY_NAME)
    end
  end
end