Class: Card::Env::Success
- Inherits:
-
Object
- Object
- Card::Env::Success
show all
- Includes:
- Location
- Defined in:
- lib/card/env/success.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Location
#card_path, #card_url, #protocol_and_host
Constructor Details
#initialize(name_context = nil, success_args = nil) ⇒ Success
Returns a new instance of Success.
9
10
11
12
13
14
|
# File 'lib/card/env/success.rb', line 9
def initialize name_context=nil, success_args=nil
@name_context = name_context
@new_args = {}
@params = OpenStruct.new
self << normalize_success_args(success_args)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/card/env/success.rb', line 162
def method_missing method, *args
case method
when /^(\w+)=$/
self[Regexp.last_match(1).to_sym] = args[0]
when /^(\w+)$/
self[Regexp.last_match(1).to_sym]
else
super
end
end
|
Instance Attribute Details
#card(name_context = @name_context) ⇒ Object
Returns the value of attribute card.
7
8
9
|
# File 'lib/card/env/success.rb', line 7
def card
@card
end
|
#id ⇒ Object
Returns the value of attribute id.
7
8
9
|
# File 'lib/card/env/success.rb', line 7
def id
@id
end
|
#name ⇒ Object
Returns the value of attribute name.
7
8
9
|
# File 'lib/card/env/success.rb', line 7
def name
@name
end
|
#name_context ⇒ Object
Returns the value of attribute name_context.
7
8
9
|
# File 'lib/card/env/success.rb', line 7
def name_context
@name_context
end
|
#params ⇒ Object
Returns the value of attribute params.
7
8
9
|
# File 'lib/card/env/success.rb', line 7
def params
@params
end
|
#redirect ⇒ Object
Returns the value of attribute redirect.
7
8
9
|
# File 'lib/card/env/success.rb', line 7
def redirect
@redirect
end
|
Instance Method Details
#<<(value) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/card/env/success.rb', line 33
def << value
if value.is_a? Hash
apply value
else
self.target = value
end
end
|
#[](key) ⇒ Object
129
130
131
132
133
134
135
136
137
|
# File 'lib/card/env/success.rb', line 129
def [] key
if respond_to? key.to_sym
send key.to_sym
elsif key.to_sym == :soft_redirect
@redirect == :soft
else
@params.send key.to_sym
end
end
|
#[]=(key, value) ⇒ Object
119
120
121
122
123
124
125
126
127
|
# File 'lib/card/env/success.rb', line 119
def []= key, value
if respond_to? "#{key}="
send "#{key}=", value
elsif key.to_sym == :soft_redirect
@redirect = :soft
else
@params.send "#{key}=", value
end
end
|
#apply(hash) ⇒ Object
97
98
99
100
101
|
# File 'lib/card/env/success.rb', line 97
def apply hash
hash.each_pair do |key, value|
self[key] = value
end
end
|
#content=(content) ⇒ Object
76
77
78
|
# File 'lib/card/env/success.rb', line 76
def content= content
@new_args[:content] = content
end
|
#flash(message = nil) ⇒ Object
139
140
141
142
143
|
# File 'lib/card/env/success.rb', line 139
def flash message=nil
@params[:flash] ||= []
@params[:flash] << message if message
@params[:flash]
end
|
#hard_redirect? ⇒ Boolean
41
42
43
|
# File 'lib/card/env/success.rb', line 41
def hard_redirect?
@redirect == true || @redirect == "true"
end
|
#in_context(name_context) ⇒ Object
16
17
18
19
|
# File 'lib/card/env/success.rb', line 16
def in_context name_context
self.name_context = name_context
self
end
|
#mark=(value) ⇒ Object
TODO: refactor to use cardish
51
52
53
54
55
56
57
58
59
|
# File 'lib/card/env/success.rb', line 51
def mark= value
case value
when Integer then @id = value
when String then @name = value
when Card then @card = value
else
self.target = value
end
end
|
#normalize_success_args(success_args) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/card/env/success.rb', line 21
def normalize_success_args success_args
case success_args
when nil
self.mark = "_self"
{}
when ActionController::Parameters
success_args.to_unsafe_h
else
success_args
end
end
|
#process_target(value) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/card/env/success.rb', line 85
def process_target value
case value
when "" then ""
when "*previous", :previous then :previous
when %r{^(http|/)} then value
when /^REDIRECT:\s*(.+)/
@redirect = true
process_target Regexp.last_match(1)
else self.mark = value
end
end
|
#raw_params ⇒ Object
149
150
151
|
# File 'lib/card/env/success.rb', line 149
def raw_params
@params
end
|
#session ⇒ Object
173
174
175
|
# File 'lib/card/env/success.rb', line 173
def session
Card::Env.session
end
|
#soft_redirect? ⇒ Boolean
reset card object and override params with success params
46
47
48
|
# File 'lib/card/env/success.rb', line 46
def soft_redirect?
@redirect == :soft
end
|
#target(name_context = @name_context) ⇒ Object
113
114
115
116
117
|
# File 'lib/card/env/success.rb', line 113
def target name_context=@name_context
card(name_context) ||
(@target == :previous ? Card::Env.previous_location : @target) ||
Card.fetch(name_context)
end
|
#target=(value) ⇒ Object
80
81
82
83
|
# File 'lib/card/env/success.rb', line 80
def target= value
@id = @name = @card = nil
@target = process_target value
end
|
#to_url(name_context = @name_context) ⇒ Object
153
154
155
156
157
158
159
160
|
# File 'lib/card/env/success.rb', line 153
def to_url name_context=@name_context
case (target = target(name_context))
when Card
target.format.path params
else
target
end
end
|
#type=(type) ⇒ Object
68
69
70
|
# File 'lib/card/env/success.rb', line 68
def type= type
@new_args[:type] = type
end
|
#type_id=(type_id) ⇒ Object
72
73
74
|
# File 'lib/card/env/success.rb', line 72
def type_id= type_id
@new_args[:type_id] = type_id.to_i
end
|