flame-flash 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/flame/flash.rb +81 -0
  3. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bb9c8e821cdb9423f6c18fb769243358df8dd3e2
4
+ data.tar.gz: b628c3f91146b335a463e9b542d8f4b9ebb6bade
5
+ SHA512:
6
+ metadata.gz: 8945311d436109970959544cec303a98792a981aead8a34927b1106b1230849a12a3bc3d4fb1236d2f5df142e75a8f951bc276f98ad8381436b21fbcd23c3258
7
+ data.tar.gz: a24878f14f9673cb99d6e0c8777218b26a6b0aab5543cecd7c754226b9d9c09b86bd6a73a6e06e4062178c98fbcc6d17b263c86e12e55ee6b7469f0c5405cc46
@@ -0,0 +1,81 @@
1
+ # Dir[File.join(__dir__, 'flash', '*.rb')].each { |file| require file }
2
+
3
+ module Flame
4
+ # Module for Flame::Flash extension with helper methods and base class
5
+ module Flash
6
+ def self.mount
7
+ proc do
8
+ # This callback rotates any flash structure we referenced,
9
+ # placing the 'next' hash into the session for the next request.
10
+ after :* do
11
+ session[:flash] = flash.next
12
+ end
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def flash(key = nil)
19
+ (
20
+ @flash ||= FlashArray.new(
21
+ (session ? session[:flash] : [])
22
+ )
23
+ ).scope(key)
24
+ end
25
+
26
+ # A subclass of Array that "remembers forward" by exactly one action.
27
+ # Tastes just like the API of Rails's ActionController::Flash::FlashHash,
28
+ # but with fewer calories.
29
+ class FlashArray
30
+ attr_reader :now, :next
31
+
32
+ # Builds a new FlashHash. It takes the hash for this action's values
33
+ # as an initialization variable.
34
+ def initialize(session, parent = nil, scope = nil)
35
+ @now = session || []
36
+ @next = []
37
+ @parent = parent || self
38
+ @scope = scope
39
+ end
40
+
41
+ def scope(scope = nil)
42
+ # p 'scope', scope
43
+ return self unless scope
44
+ self.class.new(
45
+ @now.select { |hash| condition(hash, scope: scope) },
46
+ self,
47
+ scope
48
+ )
49
+ end
50
+
51
+ # We assign to the _next_ hash, but retrieve values
52
+ # from the _now_ hash. Freaky, huh?
53
+ def []=(type, text)
54
+ hash = { type: type, text: text }
55
+ # p @parent == self, @scope
56
+ hash.merge!(scope: @scope) if @parent != self
57
+ # p hash
58
+ @parent.next.push(hash)
59
+ # p @parent.next
60
+ end
61
+
62
+ def [](type = nil)
63
+ selected = @parent.now.select do |hash|
64
+ condition(hash, type: type, scope: @scope)
65
+ end
66
+ # p 'flash[]', type, @scope, selected
67
+ selected.map { |hash| hash[:text] }
68
+ end
69
+
70
+ def each(&block)
71
+ @now.each(&block)
72
+ end
73
+
74
+ private
75
+
76
+ def condition(hash, options = {}) # kind, section)
77
+ options.reject { |key, val| hash[key] == val || val.nil? }.empty?
78
+ end
79
+ end
80
+ end
81
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flame-flash
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Popov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Show messages (notifies, errors, warnings) in current or next routes
14
+ after redirect.
15
+ email:
16
+ - alex.wayfer@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/flame/flash.rb
22
+ homepage: https://gitlab.com/AlexWayfer/flame-flash
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.4.5.1
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Flash plugin for Flame-framework
46
+ test_files: []