Class: Me2API::Model
- Inherits:
-
Hash
- Object
- Hash
- Me2API::Model
show all
- Defined in:
- lib/me2api/model.rb
Constant Summary
collapse
- FROM_OR_TO_REGEX =
/(\d{4}\-\d{1,2}-\d{1,2}[T|\s]\d{1,2}\:\d{1,2}\:\d{1,2})([\+|\s])(\d{2}\:{0,}\d{2})/
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash) ⇒ Model
Returns a new instance of Model.
3
4
5
|
# File 'lib/me2api/model.rb', line 3
def initialize(hash)
hash.keys.each { |key| self[key] = hash[key] }
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
30
31
32
|
# File 'lib/me2api/model.rb', line 30
def method_missing(name, *args, &block)
self[name.to_sym]
end
|
Class Method Details
.wrap(value) ⇒ Object
7
8
9
10
11
12
13
14
15
|
# File 'lib/me2api/model.rb', line 7
def self.wrap(value)
if value.is_a?(Hash) && !value.is_a?(Me2API::Model)
Model.new(value)
elsif value.is_a?(Array)
value.map { |v| wrap(v) }
else
value
end
end
|
Instance Method Details
#[]=(key, value) ⇒ Object
17
18
19
20
|
# File 'lib/me2api/model.rb', line 17
def []=(key, value)
key = underscore(key.to_s).to_sym
super(key, Model.wrap(value))
end
|
#id ⇒ Object
22
23
24
|
# File 'lib/me2api/model.rb', line 22
def id
method_missing(:id)
end
|
#iso8601_time(tstr) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/me2api/model.rb', line 35
def iso8601_time(tstr)
if tstr =~ FROM_OR_TO_REGEX && tstr !~ /\+/
tstr = tstr.gsub(FROM_OR_TO_REGEX) do |match|
if $2 == " "
match = $1 + "+" + $3
end
match
end
end
Time.parse(tstr).utc
end
|
#type ⇒ Object
26
27
28
|
# File 'lib/me2api/model.rb', line 26
def type
method_missing(:type)
end
|
#underscore(str) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/me2api/model.rb', line 48
def underscore(str)
str.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
|