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.
11
12
13
14
15
16
|
# File 'lib/card/env/success.rb', line 11
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
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/card/env/success.rb', line 168
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
109
110
111
112
113
114
115
116
117
|
# File 'lib/card/env/success.rb', line 109
def card name_context=@name_context
if @card
@card
elsif @id
Card.find @id
elsif @name
Card.fetch @name.to_name.absolute(name_context), new: @new_args
end
end
|
#id ⇒ Object
Returns the value of attribute id.
9
10
11
|
# File 'lib/card/env/success.rb', line 9
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
151
152
153
|
# File 'lib/card/env/success.rb', line 151
def params
@params.marshal_dump
end
|
#redirect ⇒ Object
Returns the value of attribute redirect.
7
8
9
|
# File 'lib/card/env/success.rb', line 7
def redirect
@redirect
end
|
#reload ⇒ Object
Returns the value of attribute reload.
7
8
9
|
# File 'lib/card/env/success.rb', line 7
def reload
@reload
end
|
Instance Method Details
#<<(value) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/card/env/success.rb', line 35
def << value
if value.is_a? Hash
apply value
else
self.target = value
end
end
|
#[](key) ⇒ Object
135
136
137
138
139
140
141
142
143
|
# File 'lib/card/env/success.rb', line 135
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
125
126
127
128
129
130
131
132
133
|
# File 'lib/card/env/success.rb', line 125
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
103
104
105
106
107
|
# File 'lib/card/env/success.rb', line 103
def apply hash
hash.each_pair do |key, value|
self[key] = value
end
end
|
#content=(content) ⇒ Object
82
83
84
|
# File 'lib/card/env/success.rb', line 82
def content= content
@new_args[:content] = content
end
|
#flash(message = nil) ⇒ Object
145
146
147
148
149
|
# File 'lib/card/env/success.rb', line 145
def flash message=nil
@params[:flash] ||= []
@params[:flash] << message if message
@params[:flash]
end
|
#hard_redirect? ⇒ Boolean
43
44
45
|
# File 'lib/card/env/success.rb', line 43
def hard_redirect?
@redirect == true || @redirect == "true"
end
|
#in_context(name_context) ⇒ Object
18
19
20
21
|
# File 'lib/card/env/success.rb', line 18
def in_context name_context
self.name_context = name_context
self
end
|
#mark=(value) ⇒ Object
TODO: refactor to use cardish
57
58
59
60
61
62
63
64
65
|
# File 'lib/card/env/success.rb', line 57
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
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/card/env/success.rb', line 23
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
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/card/env/success.rb', line 91
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
155
156
157
|
# File 'lib/card/env/success.rb', line 155
def raw_params
@params
end
|
#reload? ⇒ Boolean
52
53
54
|
# File 'lib/card/env/success.rb', line 52
def reload?
@reload == true || @reload == "true"
end
|
#session ⇒ Object
179
180
181
|
# File 'lib/card/env/success.rb', line 179
def session
Card::Env.session
end
|
#soft_redirect? ⇒ Boolean
reset card object and override params with success params
48
49
50
|
# File 'lib/card/env/success.rb', line 48
def soft_redirect?
@redirect == :soft
end
|
#target(name_context = @name_context) ⇒ Object
119
120
121
122
123
|
# File 'lib/card/env/success.rb', line 119
def target name_context=@name_context
card(name_context) ||
(@target == :previous ? Card::Env.previous_location : @target) ||
Card.fetch(name_context)
end
|
#target=(value) ⇒ Object
86
87
88
89
|
# File 'lib/card/env/success.rb', line 86
def target= value
@id = @name = @card = nil
@target = process_target value
end
|
#to_url(name_context = @name_context) ⇒ Object
159
160
161
162
163
164
165
166
|
# File 'lib/card/env/success.rb', line 159
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
74
75
76
|
# File 'lib/card/env/success.rb', line 74
def type= type
@new_args[:type] = type
end
|
#type_id=(type_id) ⇒ Object
78
79
80
|
# File 'lib/card/env/success.rb', line 78
def type_id= type_id
@new_args[:type_id] = type_id.to_i
end
|