Class: Cyborg::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/cyborg/plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Plugin

Returns a new instance of Plugin.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cyborg/plugin.rb', line 6

def initialize(options)
  @name            = options.delete(:name) 
  @module_name     = parent_module.name
  @gem             = Gem.loaded_specs[@name]
  config(options)
  expand_asset_paths

  # Store the gem path for access later when overriding root
  parent_module.instance_variable_set(:@gem_path, root)
  add_assets

  @engine = create_engine if defined?(Rails)
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



3
4
5
# File 'lib/cyborg/plugin.rb', line 3

def destination
  @destination
end

#engineObject (readonly)

Returns the value of attribute engine.



3
4
5
# File 'lib/cyborg/plugin.rb', line 3

def engine
  @engine
end

#gemObject (readonly)

Returns the value of attribute gem.



3
4
5
# File 'lib/cyborg/plugin.rb', line 3

def gem
  @gem
end

#javascriptsObject (readonly)

Returns the value of attribute javascripts.



3
4
5
# File 'lib/cyborg/plugin.rb', line 3

def javascripts
  @javascripts
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



3
4
5
# File 'lib/cyborg/plugin.rb', line 3

def module_name
  @module_name
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/cyborg/plugin.rb', line 3

def name
  @name
end

#stylesheetsObject (readonly)

Returns the value of attribute stylesheets.



3
4
5
# File 'lib/cyborg/plugin.rb', line 3

def stylesheets
  @stylesheets
end

#svgsObject (readonly)

Returns the value of attribute svgs.



3
4
5
# File 'lib/cyborg/plugin.rb', line 3

def svgs
  @svgs
end

Instance Method Details

#add_assetsObject



44
45
46
47
48
# File 'lib/cyborg/plugin.rb', line 44

def add_assets
  @javascripts = Assets::Javascripts.new(self, paths[:javascripts])
  @stylesheets = Assets::Stylesheets.new(self, paths[:stylesheets])
  @svgs        = Assets::Svgs.new(self, paths[:svgs])
end

#asset_rootObject



97
98
99
100
101
102
103
# File 'lib/cyborg/plugin.rb', line 97

def asset_root
  if Cyborg.production? 
    plugin.production_asset_root
  else
    plugin.asset_root
  end
end

#assetsObject



50
51
52
# File 'lib/cyborg/plugin.rb', line 50

def assets
  [@svgs, @stylesheets, @javascripts]
end

#buildObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cyborg/plugin.rb', line 54

def build
  Command.from_root {
    FileUtils.mkdir_p(File.join(destination, asset_root))
  }
  threads = []
  assets.each do |asset|
    threads << Thread.new { asset.build }
  end

  threads
end

#config(options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cyborg/plugin.rb', line 70

def config(options)
  options = {
    production_asset_root: "/assets/#{@name}",
    asset_root:    "/assets/#{@name}",
    destination:   "public/",
    root:          @gem.full_gem_path,
    version:       @gem.version.to_s,
    paths: {
      stylesheets: "app/assets/stylesheets/#{@name}",
      javascripts: "app/assets/javascripts/#{@name}",
      svgs:        "app/assets/svgs/#{@name}",
    }
  }.merge(options)

  options.each do |k,v|
    set_instance(k.to_s,v) 
  end
end

#create_engineObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cyborg/plugin.rb', line 20

def create_engine
  # Create a new Rails::Engine
  return parent_module.const_set('Engine', Class.new(Rails::Engine) do

    def get_plugin_path
      parent = Object.const_get(self.class.to_s.split('::').first)
      path = parent.instance_variable_get("@gem_path")
      Pathname.new path
    end

    def config
      @config ||= Rails::Engine::Configuration.new(get_plugin_path)
    end

    initializer "#{name.to_s.downcase}.static_assets" do |app|
      app.middleware.insert_before(::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public")
    end
  end)
end

#expand_asset_pathsObject



90
91
92
93
94
95
# File 'lib/cyborg/plugin.rb', line 90

def expand_asset_paths
  @paths.each do |type, path|
    @paths[type] = File.join(root, path)
  end
  @destination = File.join(root, @destination)
end

#parent_moduleObject



40
41
42
# File 'lib/cyborg/plugin.rb', line 40

def parent_module
  Object.const_get(self.class.to_s.split('::').first)
end

#watchObject



66
67
68
# File 'lib/cyborg/plugin.rb', line 66

def watch
  assets.map(&:watch)
end