Module: Usable

Defined in:
lib/usable.rb,
lib/usable/config.rb,
lib/usable/struct.rb,
lib/usable/version.rb,
lib/usable/config_multi.rb,
lib/usable/mod_extender.rb,
lib/usable/config_register.rb

Defined Under Namespace

Modules: ConfigMulti, ConfigRegister Classes: Config, ModExtender, Railtie

Constant Summary collapse

VERSION =
"3.5.0".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#usablesObject



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

def usables
  @usables ||= Config.new
end

Class Method Details

.extended(base) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/usable.rb', line 14

def self.extended(base)
  if base.is_a? Class
    # Define an instance level version of +usables+
    base.class_eval do
      def usables
        self.class.usables
      end

      def usable_method(method_name)
        self.class.usable_method(self, method_name)
      end
    end
  end

  unless base.respond_to?(:config)
    base.instance_eval do
      def config(&block)
        if block
          usables.instance_eval &block
        else
          usables
        end
      end
    end
  end
  extended_constants << base
end

.extended_constantsObject

Keep track of extended classes and modules so we can freeze all usables on boot in production environments



10
11
12
# File 'lib/usable.rb', line 10

def self.extended_constants
  @extended_constants ||= Set.new
end

.Struct(attributes = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/usable/struct.rb', line 2

def self.Struct(attributes = {})
  Class.new do
    extend Usable

    self.usables = Usable::Config.new(attributes)

    define_singleton_method(:inherited) do |child|
      child.usables = usables.clone
    end

    attributes.keys.each do |key|
      define_method(key) { @attrs[key] }
      define_method("#{key}=") { |new_val| @attrs[key] = new_val }
    end

    attr_accessor :attrs

    def initialize(attrs = {})
      @attrs = usables.merge(attrs)
    end
  end
end

Instance Method Details

#usable(*args, &block) ⇒ Object

Note:

Hides methods

Returns self.

Examples:


class Example
  extend Usable
  usable Mixin, only: [:foo, :bar] do
    baz "Available as `Example.usables.baz` or `Example.usables.mixin.baz`"
  end
end

Parameters:

  • mod (Module)
  • options (Hash)

    Customize the extension of the module as well as define config settings on the target

  • [Array,Symbol] (Hash)

    a customizable set of options

  • [String,Symbol] (Hash)

    a customizable set of options

Returns:

  • self



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
93
94
# File 'lib/usable.rb', line 65

def usable(*args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  args.each do |mod|
    ModExtender.new(mod, only: options.delete(:only), method: options.delete(:method)).call self
    # Define settings on @usables and on the scoped @usables
    scope = Config.new
    if mod.name
      scope_name = mod.name.split('::').last.gsub(/\B([A-Z])([a-z_0-9])/, '_\1\2').downcase
      usables[scope_name] = scope
    end
    if mod.respond_to? :usables
      scope += mod.usables
      self.usables += mod.usables
    end
    # any left over -options- are considered "config" settings
    if options
      [scope, usables].each { |x| options.each { |k, v| x[k] = v } }
    end
    if block_given?
      [scope, usables].each { |x| x.instance_eval &block }
    end
    if mod.const_defined?(:InstanceMethods, false)
      send :include, mod.const_get(:InstanceMethods, false)
    end
    if mod.const_defined?(:ClassMethods, false)
      send :extend, mod.const_get(:ClassMethods, false)
    end
  end
  self
end

#usable_method(context, method_name) ⇒ Method

Returns bound to the given -context-.

Returns:

  • (Method)

    bound to the given -context-



97
98
99
# File 'lib/usable.rb', line 97

def usable_method(context, method_name)
  usables.available_methods[method_name].bind(context)
end