Module: TripIt::ParamUtil

Included in:
Base
Defined in:
lib/trip_it/util/param_util.rb

Instance Method Summary collapse

Instance Method Details

#address_param(*names) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/trip_it/util/param_util.rb', line 165

def address_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}=" do |val|
      if val.is_a?(Address)
        instance_variable_set("@#{name}",val)
      else
        raise ArgumentError, "#{name} must be an Address"
      end
    end
  end      
end

#airportcode_param(*names) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/trip_it/util/param_util.rb', line 149

def airportcode_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}=" do |val|
      if val.match("[A-Z][A-Z][A-Z]").nil?
        raise ArgumentError, "#{name} must be a valid airport code"
      else
        instance_variable_set("@#{name}",val)
      end
    end
  end      
end

#array_param(*names) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/trip_it/util/param_util.rb', line 85

def array_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}=" do |val|
      if val.is_a?(Array)
        instance_variable_set("@#{name}",val)
      else
        raise ArgumentError, "#{name} must be an Array"
      end
    end
  end
end

#boolean_param(*names) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/trip_it/util/param_util.rb', line 53

def boolean_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}=" do |val|
      if val.is_a?(TrueClass) || val.is_a?(FalseClass)
        instance_variable_set("@#{name}",val)
      else
        raise ArgumentError, "#{name} must be a Boolean"
      end
    end
  
    define_method "#{name}?" do
      !!instance_variable_get("@#{name}")
    end
  end
end

#boolean_read_param(*names) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/trip_it/util/param_util.rb', line 73

def boolean_read_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}?" do
      !!instance_variable_get("@#{name}")
    end
  end
end

#camelize(word) ⇒ Object



231
232
233
# File 'lib/trip_it/util/param_util.rb', line 231

def camelize(word)
  word.split(/[^a-z0-9]/i).map{|w| w.capitalize}.join
end

#date_param(*names) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/trip_it/util/param_util.rb', line 117

def date_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}=" do |val|
      if val.is_a?(Date)
        instance_variable_set("@#{name}",val)
      else
        raise ArgumentError, "#{name} must be a Date"
      end
    end
  end
end

#datetime_param(*names) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/trip_it/util/param_util.rb', line 101

def datetime_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}=" do |val|
      if val.is_a?(DateTime)
        instance_variable_set("@#{name}",val)
      else
        raise ArgumentError, "#{name} must be a DateTime"
      end
    end
  end
end

#exceptions(*names) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/trip_it/util/param_util.rb', line 213

def exceptions(*names)
  names.each do |n|
    # Clean up the name
    n = camelize(n.to_s)

    class_eval %{
      # Define a common Error class if it's
      # not yet defined
      unless const_defined?("Error")
        const_set("Error", Class.new(StandardError) )
      end

      # Define the exception class
      class #{n} < Error; end
    }
  end
end

#float_param(*names) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/trip_it/util/param_util.rb', line 37

def float_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}=" do |val|
      if val.is_a?(Float)
        instance_variable_set("@#{name}",val)
      else
        raise ArgumentError, "#{name} must be a Float"
      end
    end
  end      
end

#integer_param(*names) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/trip_it/util/param_util.rb', line 21

def integer_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}=" do |val|
      if val.is_a?(Integer)
        instance_variable_set("@#{name}",val)
      else
        raise ArgumentError, "#{name} must be an Integer"
      end
    end
  end
end

#string_param(*names) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/trip_it/util/param_util.rb', line 5

def string_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}=" do |val|
      if val.is_a?(String)
        instance_variable_set("@#{name}",val)
      else
        raise ArgumentError, "#{name} must be a String"
      end
    end
  end
end

#time_param(*names) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/trip_it/util/param_util.rb', line 133

def time_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}=" do |val|
      if val.is_a?(Time)
        instance_variable_set("@#{name}",val)
      else
        raise ArgumentError, "#{name} must be a Time"
      end
    end
  end      
end

#traveler_array_param(*names) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/trip_it/util/param_util.rb', line 197

def traveler_array_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}=" do |val|
      if val.is_a?(Array) && val.all? { |e| Traveler === e }
        instance_variable_set("@#{name}",val)
      else
        raise ArgumentError, "#{name} must be an Array of Travelers"
      end
    end
  end      
end

#traveler_param(*names) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/trip_it/util/param_util.rb', line 181

def traveler_param(*names)
  names.each do |name|
    define_method "#{name}" do
      instance_variable_get("@#{name}")
    end
  
    define_method "#{name}=" do |val|
      if val.is_a?(Traveler)
        instance_variable_set("@#{name}",val)
      else
        raise ArgumentError, "#{name} must be a Traveler"
      end
    end
  end      
end