Module: Equipment

Overview

This module provides the facilities for equipments. If included in an app, it will provide a basic set of extensions. If extended in an equipment, it will make it ready to provide new functionnalities to Camping apps.

Constant Summary collapse

LIB_PATH =
File.expand_path(File.dirname(__FILE__))
DATA_PATH =
File.join(LIB_PATH, '..', 'data', 'equipment')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



23
24
25
# File 'lib/equipment.rb', line 23

def debug
  @debug
end

Class Method Details

.global_extensionsObject

List of global extensions from #equip_all



117
# File 'lib/equipment.rb', line 117

def self.global_extensions; @global_extensions; end

.included(app) ⇒ Object

If you include Equipment in your app, you’ll get some default extensions.



112
113
114
# File 'lib/equipment.rb', line 112

def self.included(app)
  app.equip Ext::AppUtil, Ext::View, Ext::Forward
end

Instance Method Details

#dependenciesObject

An array of equipment modules on which that equipment depends on for working. Works with #included.



96
97
98
# File 'lib/equipment.rb', line 96

def dependencies
  @__dependencies ||= []
end

#depends_on(mod) ⇒ Object

Asynchronous dependencies for later inclusion in a camping app. Fills #dependencies for #included.



102
# File 'lib/equipment.rb', line 102

def depends_on(mod); dependencies << mod; end

#equip(app) ⇒ Object

Utility for equipments. Automatically sets a set of rule for importing methods, classes, class methods in your application for your extension.

Example

module MyUtil
  extend Equipment
  module ViewsClassMethod; end #=> YourApp::Views's class methods
  module Controllers; class X; end; end #=> New X controller
  module Helpers; def somemethod; end; end #=> New somemethoer
end

Camping.goes :YourApp

module YourApp
  MyUtil.equip(self)
end

# or

YourApp.extend CampingExt
YourApp.equip(MyUtil)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/equipment.rb', line 49

def equip(app)
#    if not app.respond_to? :goes and defined? Camping
#      raise "Cannot equip #{self} to #{app}, only for Camping apps"
#    end
  app.extend Ext unless app.metaclass.ancestors.include? Ext

  # Only equip once
  return false if app.equipments.include?(self)
  app.equipments << self

  # dependencies
  dependencies.each { |ext| ext.equip(app) }

  puts "** equipped #{self} in #{app}" if debug

  app_mods = app.constants.sort.select do |name|
    app.const_get(name).kind_of?(Module)
  end

  puts "App Mods  : #{app_mods.inspect}" if debug
  puts "Ext Mods  : #{constants.inspect}" if debug

  app_mods.each do |name|
    app_mod = app.const_get(name)
    if const_defined?(name) and mod = const_get(name) and 
       not mod.kind_of?(Class) and mod != app_mod
      if mod.public_instance_methods.size > 0 or
         mod.protected_instance_methods.size > 0 or
         mod.private_instance_methods.size > 0
        app_mod.insert(mod)
#          app_mod.include(mod)
        puts "Inserted  : #{mod} -> #{app_mod}" if debug
      elsif mod.constants.size > 0
        mod.transfer_classes_to(app_mod, :force)
        puts "Cls trans : #{mod} -> #{app_mod}" if debug
      end
    end
    if const_defined?("#{name}ClassMethods")
      mod_ext = const_get("#{name}ClassMethods")
      app_mod.extend(mod_ext)
      puts "Extended  : #{mod_ext} -> #{app_mod}" if debug
    end
  end
end

#equip_allObject

If called, your equipment will be available to all apps. Call this before defining any application.



106
107
108
# File 'lib/equipment.rb', line 106

def equip_all
  ::Equipment.global_extensions << self unless ::Equipment.global_extensions.include? self
end