Class: Vk::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/vk/base.rb
Direct Known Subclasses
Album, Audio, Audio::Album, Audio::Lyrics, City, Country, DSL::Newsfeed::SearchItem, Group, Photo, Post, Region, Stats::Age, Stats::City, Stats::Country, Stats::Period, Stats::Sex, Stats::SexAge, Street, User
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(id, options = {}) ⇒ Base
Returns a new instance of Base.
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/vk/base.rb', line 59
def initialize(id, options = {})
@attributes = ActiveSupport::HashWithIndifferentAccess.new
if id.is_a?(Hash)
options[:data] = id
id = options[:data][key_field.to_s]
end
self.id = id
self.class.identity_map[id] = self
if options.key? :data
@attributes.merge!(options[:data].with_indifferent_access)
else
load_data(options)
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/vk/base.rb', line 95
def method_missing(method, *args)
if method =~ /\?\Z/
respond_to_missing?(method) && @attributes[method]
elsif @attributes.key?(method)
@attributes[method]
elsif self.class.fields.include?(method.to_sym)
read_attribute(method.to_s)
else
super
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
77
78
79
|
# File 'lib/vk/base.rb', line 77
def attributes
@attributes
end
|
Class Method Details
.find(*ids) ⇒ Object
Find object in identity map or initialize object
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/vk/base.rb', line 26
def self.find(*ids)
options = ids.
if ids.count == 1
id = ids.first.to_i
identity_map[id] ||= new(id, options)
elsif respond_to?(:find_all)
find_all(ids, options)
else
ids.map do |id|
find(id, options)
end
end
end
|
.inherited(subclass) ⇒ Object
50
51
52
53
54
55
56
57
|
# File 'lib/vk/base.rb', line 50
def self.inherited(subclass)
super(subclass)
subclass.identity_map = {}
subclass.fields = []
subclass.key_field = :id
subclass.fields = []
subclass.loader = Vk.client
end
|
.method_missing(method, *args) ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/vk/base.rb', line 42
def self.method_missing(method, *args)
if identity_map.respond_to?(method)
identity_map.send(method, *args)
else
super
end
end
|
Instance Method Details
#fields ⇒ <Symbol>
22
|
# File 'lib/vk/base.rb', line 22
class_attribute :fields
|
#id ⇒ String
84
85
86
|
# File 'lib/vk/base.rb', line 84
def id
@attributes[key_field].to_s
end
|
#id=(id) ⇒ Object
79
80
81
|
# File 'lib/vk/base.rb', line 79
def id=(id)
@attributes[key_field] = id
end
|
#identity_map ⇒ {Integer => self}
13
|
# File 'lib/vk/base.rb', line 13
class_attribute :identity_map
|
#inspect ⇒ Object
119
120
121
|
# File 'lib/vk/base.rb', line 119
def inspect
"#<#{self.class.name}:#{id} @attributes=#{@attributes.inspect}>"
end
|
#key_field ⇒ Symbol
19
|
# File 'lib/vk/base.rb', line 19
class_attribute :key_field
|
16
|
# File 'lib/vk/base.rb', line 16
class_attribute :loader
|
#logger ⇒ Object
111
112
113
|
# File 'lib/vk/base.rb', line 111
def logger
Vk.logger
end
|
#read_attribute(name) ⇒ Object
88
89
90
91
92
93
|
# File 'lib/vk/base.rb', line 88
def read_attribute(name)
if !@attributes.key?(name) && self.class.fields.include?(name.to_sym)
load_data(fields: name)
end
@attributes[name]
end
|
#respond_to_missing?(method, include_all = false) ⇒ Boolean
107
108
109
|
# File 'lib/vk/base.rb', line 107
def respond_to_missing?(method, include_all = false)
@attributes.key?(method) || self.class.fields.include?(method.to_sym) || super(method, include_all)
end
|
#to_hash ⇒ Object
115
116
117
|
# File 'lib/vk/base.rb', line 115
def to_hash
attributes.to_hash
end
|