Class: Vk::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vk/base.rb

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

#attributesObject (readonly)

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

Parameters:

  • ids (self, <self>)


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.extract_options!
  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>

Returns:

  • (<Symbol>)


22
# File 'lib/vk/base.rb', line 22

class_attribute :fields

#idString

Returns:

  • (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}

Returns:

  • ({Integer => self})


13
# File 'lib/vk/base.rb', line 13

class_attribute :identity_map

#inspectObject



119
120
121
# File 'lib/vk/base.rb', line 119

def inspect
  "#<#{self.class.name}:#{id} @attributes=#{@attributes.inspect}>"
end

#key_fieldSymbol

Returns:

  • (Symbol)


19
# File 'lib/vk/base.rb', line 19

class_attribute :key_field

#loaderVk::Client

Returns:



16
# File 'lib/vk/base.rb', line 16

class_attribute :loader

#loggerObject



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

Returns:

  • (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_hashObject



115
116
117
# File 'lib/vk/base.rb', line 115

def to_hash
  attributes.to_hash
end