Module: DynamicMethods
- Included in:
- User
- Defined in:
- lib/dynamic_methods.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/dynamic_methods.rb', line 9
def method_missing(method_id, *args)
key = method_id.id2name
return super if key =~ /^dynamic_fields(=)?$/
key.slice!(/^([A-Z]+_)*/)
if key.ends_with?('?')
key.slice!(-1,1)
if self.dynamic_hash.has_key?(key)
return self.dynamic_hash[key].to_i == 1
end
else
return self.dynamic_hash[key] if self.dynamic_hash.has_key?(key)
end
begin
return super
rescue NoMethodError
@required_fields ||= []
@ccnumber_fields ||= []
@email_fields ||= []
key = method_id.id2name
if key[-1, 1] == '='
key.slice!(-1, 1)
if (prefix = key.slice!(/^([A-Z]+_)*/))
@required_fields << key if prefix[/REQUIRED_/]
@ccnumber_fields << key if prefix[/CCNUMBER_/]
@email_fields << key if prefix[/EMAIL_/]
end
val = args.shift
@dynamic_fields_data[key] = val
logger.debug "DYNAMIC_METHOD: #{key} = #{val}"
self.dynamic_fields = YAML::dump(self.dynamic_hash)
return val
end
end
nil
end
|
Instance Method Details
#dynamic_hash ⇒ Object
4
5
6
7
|
# File 'lib/dynamic_methods.rb', line 4
def dynamic_hash
@dynamic_fields_data ||= YAML::load(self.dynamic_fields || '') || {}
@dynamic_fields_data
end
|
#save(*args) ⇒ Object
104
105
106
107
|
# File 'lib/dynamic_methods.rb', line 104
def save(*args)
self.dynamic_fields = YAML::dump(self.dynamic_hash)
super
end
|
#save!(*args) ⇒ Object
109
110
111
112
|
# File 'lib/dynamic_methods.rb', line 109
def save!(*args)
self.dynamic_fields = YAML::dump(self.dynamic_hash)
super
end
|
#validate ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/dynamic_methods.rb', line 59
def validate
super
for f in @required_fields || []
errors.add(f, "is required") if self.send(f).blank?
end
for f in @ccnumber_fields || []
next if (self.send(f) || '') == ''
value = self.send(f).gsub(/[-\s]/, '')
next if value == 'test' && RAILS_ENV == 'development'
errors.add(f, "is invalid") and next if value !~ /^\d{15,16}$/
errors.add(f, "is invalid") and next if value !~ /^(4)|(3[47])|(5[1-5])|(6011)/
sum = 0
for k in 1..value.length
temp = value.slice(-k, 1).to_i
if k.odd?
sum += temp.to_i
elsif k.even?
(temp * 2).to_s.split('').each { |d| sum += d.to_i }
end
end
errors.add(f, "is invalid") if sum % 10 != 0
end
for f in @email_fields || []
errors.add(f, "is invalid") if self.send(f) !~ /^[\w\d]+([\w\d\!\#\$\%\&\*\+\-\/\=\?\^\`\{\|\}\~\.]*[\w\d]+)*@([\w\d]+\.)+[\w]{2,}$/
end
end
|