Class: Card::Success

Inherits:
Object show all
Includes:
Location
Defined in:
lib/card/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.



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

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 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



127
128
129
130
131
132
133
134
135
136
# File 'lib/card/success.rb', line 127

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.



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

def card
  @card
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#name_contextObject

Returns the value of attribute name_context.



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

def name_context
  @name_context
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#redirectObject

Returns the value of attribute redirect.



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

def redirect
  @redirect
end

Instance Method Details

#<<(value) ⇒ Object



19
20
21
22
23
24
# File 'lib/card/success.rb', line 19

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

#[](key) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/card/success.rb', line 104

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



94
95
96
97
98
99
100
101
102
# File 'lib/card/success.rb', line 94

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



74
75
76
77
78
# File 'lib/card/success.rb', line 74

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

#content=(content) ⇒ Object



52
53
54
# File 'lib/card/success.rb', line 52

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

#hard_redirect?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/card/success.rb', line 26

def hard_redirect?
  @redirect == true
end

#mark=(value) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/card/success.rb', line 35

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



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/card/success.rb', line 61

def process_target value
  case value
  when ''                     then ''
  when '*previous', :previous then :previous
  when /^(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

#sessionObject



138
139
140
# File 'lib/card/success.rb', line 138

def session
  Card::Env.session
end

#soft_redirect?Boolean

reset card object and override params with success params

Returns:

  • (Boolean)


31
32
33
# File 'lib/card/success.rb', line 31

def soft_redirect?
  @redirect == :soft
end

#target(name_context = @name_context) ⇒ Object



90
91
92
# File 'lib/card/success.rb', line 90

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

#target=(value) ⇒ Object



56
57
58
59
# File 'lib/card/success.rb', line 56

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

#to_url(name_context = @name_context) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/card/success.rb', line 118

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

#type=(type) ⇒ Object



48
49
50
# File 'lib/card/success.rb', line 48

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