Module: IncludableWithOptions

Defined in:
lib/dolzenko/includable_with_options.rb

Overview

Check Thomas Sawyer take on the problem github.com/rubyworks/paramix

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.last_default_optionsObject

Returns the value of attribute last_default_options.



4
5
6
# File 'lib/dolzenko/includable_with_options.rb', line 4

def last_default_options
  @last_default_options
end

Class Method Details

.included(includable_with_options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dolzenko/includable_with_options.rb', line 7

def self.included(includable_with_options)
  %w(string/methodize kernel/constant module/basename module/spacename).each { |facets_core_ext| require "facets/#{ facets_core_ext }" }

  raise "IncludableWithOptions should be included by the Module" unless includable_with_options.instance_of?(Module)

  options_class_var_name = "@@#{ includable_with_options.basename.methodize }_options"

  unless IncludableWithOptions.last_default_options.nil?
    includable_with_options.send(:class_variable_set, options_class_var_name, IncludableWithOptions.last_default_options)
    IncludableWithOptions.last_default_options = nil
  end

  context = Kernel.constant(includable_with_options.spacename)

  option_setting_duplicator = <<-CODE
    def #{ context != Kernel ? "self." : "" }#{ includable_with_options.basename }(options = nil)
      m = Kernel.constant("#{ includable_with_options.name }").dup
      m.send(:class_variable_set, "#{ options_class_var_name }", options)
      m
    end    
  CODE

  context.module_eval(option_setting_duplicator)
end