Class: SessionAttributes
- Inherits:
-
Object
- Object
- SessionAttributes
- Includes:
- ActiveSupport::Callbacks
- Defined in:
- lib/session_attributes.rb
Constant Summary collapse
- INVALID_ATTRIBUTE_NAMES =
:nodoc:
[:set, :reset, :resets, :instance, :before_reset, :after_reset, :reset_all, :clear_all]
Class Method Summary collapse
- .attribute(*names, default: nil) ⇒ Object
- .attributes ⇒ Object
- .attributes=(attrs) ⇒ Object
- .before_reset(*methods) ⇒ Object
-
.clear_all ⇒ Object
:nodoc:.
- .instance ⇒ Object
-
.reset_all ⇒ Object
:nodoc:.
- .resets(*methods) ⇒ Object (also: after_reset)
-
.respond_to_missing?(name, _) ⇒ Boolean
:nodoc: I had to move this to a self.
Instance Method Summary collapse
-
#initialize ⇒ SessionAttributes
constructor
A new instance of SessionAttributes.
- #reset ⇒ Object
- #resolve_defaults ⇒ Object
- #set(attributes) ⇒ Object
Constructor Details
#initialize ⇒ SessionAttributes
Returns a new instance of SessionAttributes.
121 122 123 |
# File 'lib/session_attributes.rb', line 121 def initialize @attributes = resolve_defaults end |
Class Method Details
.attribute(*names, default: nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/session_attributes.rb', line 20 def attribute(*names, default: nil) invalid_attribute_names = names.map(&:to_sym) & INVALID_ATTRIBUTE_NAMES if invalid_attribute_names.any? raise ArgumentError, "Restricted attribute names: #{invalid_attribute_names.join(", ")}" end ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |owner| names.each do |name| owner.define_cached_method(name, namespace: :session_attributes) do |batch| batch << "def #{name}" << "attributes[:#{name}]" << "end" end owner.define_cached_method("#{name}=", namespace: :session_attributes) do |batch| batch << "def #{name}=(value)" << "attributes[:#{name}] = value" << "end" end end end ActiveSupport::Delegation.generate(singleton_class, names, to: :instance, nilable: false, signature: "") ActiveSupport::Delegation.generate(singleton_class, names.map { |n| "#{n}=" }, to: :instance, nilable: false, signature: "value") self.defaults = defaults.merge(names.index_with { default }) end |
.attributes ⇒ Object
74 75 76 77 |
# File 'lib/session_attributes.rb', line 74 def attributes session_instances[session_instances_key] ||= {} session_instances[session_instances_key][:attributes] ||= {} end |
.attributes=(attrs) ⇒ Object
69 70 71 72 |
# File 'lib/session_attributes.rb', line 69 def attributes=(attrs) session_instances[session_instances_key] ||= {} session_instances[session_instances_key][:attributes] = attrs end |
.before_reset(*methods) ⇒ Object
49 50 51 |
# File 'lib/session_attributes.rb', line 49 def before_reset(*methods, &) set_callback(:reset, :before, *methods, &) end |
.clear_all ⇒ Object
:nodoc:
64 65 66 67 |
# File 'lib/session_attributes.rb', line 64 def clear_all # :nodoc: reset_all session_instances.clear end |
.instance ⇒ Object
16 17 18 |
# File 'lib/session_attributes.rb', line 16 def instance new end |
.reset_all ⇒ Object
:nodoc:
60 61 62 |
# File 'lib/session_attributes.rb', line 60 def reset_all # :nodoc: session_instances.each_value(&:reset) end |
.resets(*methods) ⇒ Object Also known as: after_reset
53 54 55 |
# File 'lib/session_attributes.rb', line 53 def resets(*methods, &) set_callback(:reset, :after, *methods, &) end |
.respond_to_missing?(name, _) ⇒ Boolean
:nodoc: I had to move this to a self. method because it was erroring out with stack level too deep
115 116 117 |
# File 'lib/session_attributes.rb', line 115 def self.respond_to_missing?(name, _) instance.respond_to?(name) end |
Instance Method Details
#reset ⇒ Object
129 130 131 132 133 |
# File 'lib/session_attributes.rb', line 129 def reset run_callbacks :reset do self.attributes = resolve_defaults end end |
#resolve_defaults ⇒ Object
107 108 109 110 111 |
# File 'lib/session_attributes.rb', line 107 def resolve_defaults defaults.transform_values do |value| (Proc === value) ? value.call : value.dup end end |
#set(attributes) ⇒ Object
125 126 127 |
# File 'lib/session_attributes.rb', line 125 def set(attributes, &) with(**attributes, &) end |