Class: WP::API::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/wp/api/resource.rb

Direct Known Subclasses

Author, Category, Page, Post, Tag

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, headers = {}) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
11
# File 'lib/wp/api/resource.rb', line 7

def initialize(attributes, headers = {})
  raise ResourceNotFoundError.new(self.class.name) if attributes.nil?
  @attributes = attributes
  @headers = _downcase_keys(headers).slice("link") if headers
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, new_value = nil) ⇒ Object (protected)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wp/api/resource.rb', line 24

def method_missing(key, new_value = nil)
  key = key.to_s
  determinant = key[-1]
  case determinant
  when '?' # example: post.sticky?
    attributes[key.chomp(determinant)] == true
  when '!' # unsupported
    fail NoMethodError.new(key)
  when '=' # example: post.title = 'my new title'
    attributes[key.chomp(determinant)] = new_value
  else
    # All other values. Hashes are converted to objects
    # if a resource for them exists (e.g. Authors)
    object key, attributes[key]
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/wp/api/resource.rb', line 5

def attributes
  @attributes
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/wp/api/resource.rb', line 5

def headers
  @headers
end

Instance Method Details

#idObject



13
14
15
# File 'lib/wp/api/resource.rb', line 13

def id
  attributes['ID']
end

#meta(client = nil) ⇒ Object



17
18
19
20
# File 'lib/wp/api/resource.rb', line 17

def meta(client = nil)
  return super unless client
  client.(id)
end