Class: Card::Env::Success

Inherits:
Object
  • Object
show all
Includes:
Location
Defined in:
lib/card/env/success.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Location

#card_path, #card_url, #page_path

Constructor Details

#initialize(name_context = nil, success_params = nil) ⇒ Success

Returns a new instance of Success.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/card/env/success.rb', line 8

def initialize name_context=nil, success_params=nil
  @name_context = name_context
  @new_args = {}
  @params = OpenStruct.new
  case success_params
  when Hash
    apply(success_params)
  when ActionController::Parameters
    apply(success_params.to_unsafe_h)
  when nil then  self.name = "_self"
  else;  self.target = success_params
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/card/env/success.rb', line 145

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.



6
7
8
# File 'lib/card/env/success.rb', line 6

def card
  @card
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/card/env/success.rb', line 6

def id
  @id
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/card/env/success.rb', line 6

def name
  @name
end

#name_contextObject

Returns the value of attribute name_context.



6
7
8
# File 'lib/card/env/success.rb', line 6

def name_context
  @name_context
end

#paramsObject

Returns the value of attribute params.



6
7
8
# File 'lib/card/env/success.rb', line 6

def params
  @params
end

#redirectObject

Returns the value of attribute redirect.



6
7
8
# File 'lib/card/env/success.rb', line 6

def redirect
  @redirect
end

Instance Method Details

#<<(value) ⇒ Object



22
23
24
25
26
27
# File 'lib/card/env/success.rb', line 22

def << value
  case value
  when Hash then apply value
  else; self.target = value
  end
end

#[](key) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/card/env/success.rb', line 112

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



102
103
104
105
106
107
108
109
110
# File 'lib/card/env/success.rb', line 102

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(args) ⇒ Object



80
81
82
83
84
# File 'lib/card/env/success.rb', line 80

def apply args
  args.each_pair do |key, value|
    self[key] = value
  end
end

#content=(content) ⇒ Object



58
59
60
# File 'lib/card/env/success.rb', line 58

def content= content
  @new_args[:content] = content
end

#flash(message = nil) ⇒ Object



122
123
124
125
126
# File 'lib/card/env/success.rb', line 122

def flash message=nil
  @params[:flash] ||= []
  @params[:flash] << message if message
  @params[:flash]
end

#hard_redirect?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/card/env/success.rb', line 29

def hard_redirect?
  @redirect == true || @redirect == "true"
end

#mark=(value) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/card/env/success.rb', line 38

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

#process_target(value) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/card/env/success.rb', line 67

def process_target value
  case value
  when ""                     then ""
  when "*previous", :previous then :previous
  when %r{^(http|/)}          then value
  when /^TEXT:\s*(.+)/        then  Regexp.last_match(1)
  when /^REDIRECT:\s*(.+)/
    @redirect = true
    process_target Regexp.last_match(1)
  else self.mark = value
  end
end

#raw_paramsObject



132
133
134
# File 'lib/card/env/success.rb', line 132

def raw_params
  @params
end

#sessionObject



156
157
158
# File 'lib/card/env/success.rb', line 156

def session
  Card::Env.session
end

#soft_redirect?Boolean

reset card object and override params with success params

Returns:

  • (Boolean)


34
35
36
# File 'lib/card/env/success.rb', line 34

def soft_redirect?
  @redirect == :soft
end

#target(name_context = @name_context) ⇒ Object



96
97
98
99
100
# File 'lib/card/env/success.rb', line 96

def target name_context=@name_context
  card(name_context) ||
    (@target == :previous ? Card::Env.previous_location : @target) ||
    Card.fetch(name_context)
end

#target=(value) ⇒ Object



62
63
64
65
# File 'lib/card/env/success.rb', line 62

def target= value
  @id = @name = @card = nil
  @target = process_target value
end

#to_url(name_context = @name_context) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/card/env/success.rb', line 136

def to_url name_context=@name_context
  case (target = target(name_context))
  when Card
    page_path target.name, params
  else
    target
  end
end

#type=(type) ⇒ Object



54
55
56
# File 'lib/card/env/success.rb', line 54

def type= type
  @new_args[:type] = type
end