Class: Passwordstate::ResourceList
- Inherits:
-
Object
- Object
- Passwordstate::ResourceList
- Includes:
- Enumerable
- 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
- #[](index) ⇒ Object
- #all(**query) ⇒ Object
- #clear ⇒ Object
- #create(data) ⇒ Object
- #delete(id, **query) ⇒ Object
- #each(&block) ⇒ Object
- #get(id, **query) ⇒ Object
-
#initialize(resource, client:, **options) ⇒ ResourceList
constructor
A new instance of ResourceList.
- #load(entries) ⇒ Object
- #new(data) ⇒ Object
- #operation_supported?(operation) ⇒ Boolean
- #post(data, **query) ⇒ Object
- #pretty_print(pp) ⇒ Object
- #pretty_print_instance_variables ⇒ Object
- #put(data, **query) ⇒ Object
- #reload ⇒ Object
- #search(**query) ⇒ Object
Constructor Details
#initialize(resource, client:, **options) ⇒ ResourceList
Returns a new instance of ResourceList.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/passwordstate/resource_list.rb', line 9 def initialize(resource, client:, **) @client = client @resource = resource @loaded = false @options = @data = [] [: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.
7 8 9 |
# File 'lib/passwordstate/resource_list.rb', line 7 def client @client end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/passwordstate/resource_list.rb', line 7 def @options end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
7 8 9 |
# File 'lib/passwordstate/resource_list.rb', line 7 def resource @resource end |
Instance Method Details
#[](index) ⇒ Object
40 41 42 |
# File 'lib/passwordstate/resource_list.rb', line 40 def [](index) @data[index] end |
#all(**query) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/passwordstate/resource_list.rb', line 93 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
44 45 46 47 |
# File 'lib/passwordstate/resource_list.rb', line 44 def clear @data = [] @loaded = false end |
#create(data) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/passwordstate/resource_list.rb', line 76 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
136 137 138 139 140 141 142 143 |
# File 'lib/passwordstate/resource_list.rb', line 136 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 |
#each(&block) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/passwordstate/resource_list.rb', line 32 def each(&block) lazy_load unless @loaded return to_enum(__method__) { @data.size } unless block_given? @data.each(&block) end |
#get(id, **query) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/passwordstate/resource_list.rb', line 102 def get(id, **query) raise 'Operation not supported' unless operation_supported?(:get) if query.empty? && !@data.empty? existing = @data.find do |entry| entry.send(entry.class.index_field) == id end return existing if existing end 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 |
#load(entries) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/passwordstate/resource_list.rb', line 54 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 entries.each { |obj| @data << obj } @loaded = true self end |
#new(data) ⇒ Object
72 73 74 |
# File 'lib/passwordstate/resource_list.rb', line 72 def new(data) resource.new .fetch(:object_data, {}).merge(data).merge(_client: client) end |
#operation_supported?(operation) ⇒ Boolean
64 65 66 67 68 69 70 |
# File 'lib/passwordstate/resource_list.rb', line 64 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
118 119 120 121 122 123 124 125 |
# File 'lib/passwordstate/resource_list.rb', line 118 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 |
#pretty_print(pp) ⇒ Object
24 25 26 27 28 |
# File 'lib/passwordstate/resource_list.rb', line 24 def pretty_print(pp) return pp.pp self if respond_to? :mocha_inspect pp.pp_object(self) end |
#pretty_print_instance_variables ⇒ Object
20 21 22 |
# File 'lib/passwordstate/resource_list.rb', line 20 def pretty_print_instance_variables instance_variables.reject { |k| %i[@client @data].include? k }.sort end |
#put(data, **query) ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/passwordstate/resource_list.rb', line 127 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
49 50 51 52 |
# File 'lib/passwordstate/resource_list.rb', line 49 def reload clear && lazy_load self end |
#search(**query) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/passwordstate/resource_list.rb', line 84 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 |