Class: Passwordstate::ResourceList
- Inherits:
-
Array
- Object
- Array
- Passwordstate::ResourceList
- Defined in:
- lib/passwordstate/resource_list.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
- #all(query = {}) ⇒ Object
- #clear ⇒ Object
- #create(data) ⇒ Object
- #delete(id, query = {}) ⇒ Object
- #get(id, query = {}) ⇒ Object
-
#initialize(client, resource, options = {}) ⇒ ResourceList
constructor
A new instance of ResourceList.
- #inspect ⇒ Object
- #load(entries) ⇒ Object
- #new(data) ⇒ Object
- #operation_supported?(operation) ⇒ Boolean
- #post(data, query = {}) ⇒ Object
- #put(data, query = {}) ⇒ Object
- #reload ⇒ Object
- #search(query = {}) ⇒ Object
Constructor Details
#initialize(client, resource, options = {}) ⇒ ResourceList
31 32 33 34 35 36 37 38 39 |
# File 'lib/passwordstate/resource_list.rb', line 31 def initialize(client, resource, = {}) @client = client @resource = resource @loaded = false = [:only] = [[:only]].flatten if .key? :only [:except] = [[:except]].flatten if .key? :except end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
29 30 31 |
# File 'lib/passwordstate/resource_list.rb', line 29 def client @client end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
29 30 31 |
# File 'lib/passwordstate/resource_list.rb', line 29 def end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
29 30 31 |
# File 'lib/passwordstate/resource_list.rb', line 29 def resource @resource end |
Instance Method Details
#all(query = {}) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/passwordstate/resource_list.rb', line 86 def all(query = {}) raise 'Operation not supported' unless operation_supported?(:all) api_path = .fetch(:all_path, resource.api_path) query = .fetch(:all_query, {}).merge(query) load resource.all(client, query.merge(_api_path: api_path)) end |
#clear ⇒ Object
41 42 43 |
# File 'lib/passwordstate/resource_list.rb', line 41 def clear @loaded = super end |
#create(data) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/passwordstate/resource_list.rb', line 69 def create(data) raise 'Operation not supported' unless operation_supported?(:post) obj = resource.new .fetch(:object_data, {}).merge(data).merge(_client: client) obj.post obj end |
#delete(id, query = {}) ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/passwordstate/resource_list.rb', line 122 def delete(id, query = {}) raise 'Operation not supported' unless operation_supported?(:delete) api_path = .fetch(:delete_path, resource.api_path) query = .fetch(:delete_query, {}).merge(query) resource.delete(client, id, query.merge(_api_path: api_path)) end |
#get(id, query = {}) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/passwordstate/resource_list.rb', line 95 def get(id, query = {}) raise 'Operation not supported' unless operation_supported?(:get) api_path = .fetch(:get_path, resource.api_path) query = .fetch(:get_query, {}).merge(query) resource.get(client, id, query.merge(_api_path: api_path)) end |
#inspect ⇒ Object
24 25 26 27 |
# File 'lib/passwordstate/resource_list.rb', line 24 def inspect lazy_load unless @loaded super end |
#load(entries) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/passwordstate/resource_list.rb', line 50 def load(entries) clear && entries.tap do |loaded| loaded.sort! { |a, b| a.send(a.class.index_field) <=> b.send(b.class.index_field) } if .fetch(:sort, true) end.each { |obj| self << obj } self end |
#new(data) ⇒ Object
65 66 67 |
# File 'lib/passwordstate/resource_list.rb', line 65 def new(data) resource.new .fetch(:object_data, {}).merge(data).merge(_client: client) end |
#operation_supported?(operation) ⇒ Boolean
57 58 59 60 61 62 63 |
# File 'lib/passwordstate/resource_list.rb', line 57 def operation_supported?(operation) return nil unless i[search all get post put delete].include?(operation) return false if .key?(:only) && ![:only].include?(operation) return false if .key?(:except) && [:except].include?(operation) !.fetch("#{operation}_path".to_sym, '').nil? end |
#post(data, query = {}) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/passwordstate/resource_list.rb', line 104 def post(data, query = {}) raise 'Operation not supported' unless operation_supported?(:post) api_path = .fetch(:post_path, resource.api_path) query = .fetch(:post_query, {}).merge(query) resource.post(client, data, query.merge(_api_path: api_path)) end |
#put(data, query = {}) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/passwordstate/resource_list.rb', line 113 def put(data, query = {}) raise 'Operation not supported' unless operation_supported?(:put) api_path = .fetch(:put_path, resource.api_path) query = .fetch(:put_query, {}).merge(query) resource.put(client, data, query.merge(_api_path: api_path)) end |
#reload ⇒ Object
45 46 47 48 |
# File 'lib/passwordstate/resource_list.rb', line 45 def reload clear && lazy_load self end |
#search(query = {}) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/passwordstate/resource_list.rb', line 77 def search(query = {}) raise 'Operation not supported' unless operation_supported?(:search) api_path = .fetch(:search_path, resource.api_path) query = .fetch(:search_query, {}).merge(query) resource.search(client, query.merge(_api_path: api_path)) end |