Class: WP::API::Resource
- Inherits:
-
Object
- Object
- WP::API::Resource
- Defined in:
- lib/wp/api/resource.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
- #id ⇒ Object
-
#initialize(attributes, headers = {}) ⇒ Resource
constructor
A new instance of Resource.
- #meta(client = nil) ⇒ Object
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/wp/api/resource.rb', line 5 def attributes @attributes end |
#headers ⇒ Object (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
#id ⇒ Object
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 (client = nil) return super unless client client.(id) end |