Module: Grape::Cookies::Configuration

Extended by:
ActiveSupport::Concern
Defined in:
lib/grape/cookies/configuration.rb

Defined Under Namespace

Classes: SettingsContainer

Class Method Summary collapse

Class Method Details

.config_class(*args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/grape/cookies/configuration.rb', line 16

def self.config_class(*args)
  new_config_class = Class.new(SettingsContainer)

  args.each do |setting_name|
    str = if setting_name.respond_to? :values
            nested_settings_methods(setting_name)
          else
            simple_settings_methods(setting_name)
          end

    new_config_class.class_eval str
  end

  new_config_class
end

.create_class_mod(args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/grape/cookies/configuration.rb', line 81

def self.create_class_mod(args)
  new_module = Module.new do
    def config_context
      @config_context ||= config_class.new
    end
  end

  new_module.tap do |class_mod|
    new_config_class = config_class(*args)

    class_mod.send(:define_method, :config_class) do
      @config_context ||= new_config_class
    end
  end
end

.module(*args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/grape/cookies/configuration.rb', line 66

def self.module(*args)
  new_module = Module.new do
    extend ActiveSupport::Concern
    include Cookies::DSL::Configuration
  end

  new_module.tap do |mod|

    class_mod = create_class_mod(args)

    mod.const_set(:ClassMethods, class_mod)

  end
end

.nested_settings_methods(setting_name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/grape/cookies/configuration.rb', line 40

def self.nested_settings_methods(setting_name)
  StringIO.new.tap do |new_str|

    setting_name.each_with_object(new_str) do |(key, value), str|
      str.puts <<-EVAL
              def #{key}_context
                @#{key}_context ||= Grape::Cookies::Configuration.config_class(*#{value.inspect}).new
              end

              def #{key}(&block)
                #{key}_context.instance_exec(&block)
              end
      EVAL
    end

    new_str.puts <<-EVAL
            def to_hash
              @settings.to_hash.merge(
                #{setting_name.keys.map { |key| "#{key}: #{key}_context.to_hash" }.join(",\n")}
              )
            end
    EVAL

  end.string
end

.simple_settings_methods(setting_name) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/grape/cookies/configuration.rb', line 32

def self.simple_settings_methods(setting_name)
  <<-EVAL
             def #{setting_name}(new_value)
               @settings[:#{setting_name}] = new_value
             end
  EVAL
end