class Passwordstate::ResourceList

Attributes

client[R]
options[R]
resource[R]

Public Class Methods

new(client, resource, options = {}) click to toggle source
# File lib/passwordstate/resource_list.rb, line 31
def initialize(client, resource, options = {})
  @client = client
  @resource = resource
  @loaded = false
  @options = options

  options[:only] = [options[:only]].flatten if options.key? :only
  options[:except] = [options[:except]].flatten if options.key? :except
end

Public Instance Methods

all(query = {}) click to toggle source
# File lib/passwordstate/resource_list.rb, line 86
def all(query = {})
  raise 'Operation not supported' unless operation_supported?(:all)

  api_path = options.fetch(:all_path, resource.api_path)
  query = options.fetch(:all_query, {}).merge(query)

  load resource.all(client, query.merge(_api_path: api_path))
end
clear() click to toggle source
Calls superclass method
# File lib/passwordstate/resource_list.rb, line 41
def clear
  @loaded = super
end
create(data) click to toggle source
# File lib/passwordstate/resource_list.rb, line 69
def create(data)
  raise 'Operation not supported' unless operation_supported?(:post)

  obj = resource.new options.fetch(:object_data, {}).merge(data).merge(_client: client)
  obj.post
  obj
end
delete(id, query = {}) click to toggle source
# File lib/passwordstate/resource_list.rb, line 122
def delete(id, query = {})
  raise 'Operation not supported' unless operation_supported?(:delete)

  api_path = options.fetch(:delete_path, resource.api_path)
  query = options.fetch(:delete_query, {}).merge(query)

  resource.delete(client, id, query.merge(_api_path: api_path))
end
get(id, query = {}) click to toggle source
# File lib/passwordstate/resource_list.rb, line 95
def get(id, query = {})
  raise 'Operation not supported' unless operation_supported?(:get)

  api_path = options.fetch(:get_path, resource.api_path)
  query = options.fetch(:get_query, {}).merge(query)

  resource.get(client, id, query.merge(_api_path: api_path))
end
inspect() click to toggle source
Calls superclass method
# File lib/passwordstate/resource_list.rb, line 24
def inspect
  lazy_load unless @loaded
  super
end
load(entries) click to toggle source
# 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 options.fetch(:sort, true)
  end.each { |obj| self << obj }
  self
end
new(data) click to toggle source
# File lib/passwordstate/resource_list.rb, line 65
def new(data)
  resource.new options.fetch(:object_data, {}).merge(data).merge(_client: client)
end
operation_supported?(operation) click to toggle source
# 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 options.key?(:only) && !options[:only].include?(operation)
  return false if options.key?(:except) && options[:except].include?(operation)

  !options.fetch("#{operation}_path".to_sym, '').nil?
end
post(data, query = {}) click to toggle source
# File lib/passwordstate/resource_list.rb, line 104
def post(data, query = {})
  raise 'Operation not supported' unless operation_supported?(:post)

  api_path = options.fetch(:post_path, resource.api_path)
  query = options.fetch(:post_query, {}).merge(query)

  resource.post(client, data, query.merge(_api_path: api_path))
end
put(data, query = {}) click to toggle source
# File lib/passwordstate/resource_list.rb, line 113
def put(data, query = {})
  raise 'Operation not supported' unless operation_supported?(:put)

  api_path = options.fetch(:put_path, resource.api_path)
  query = options.fetch(:put_query, {}).merge(query)

  resource.put(client, data, query.merge(_api_path: api_path))
end
reload() click to toggle source
# File lib/passwordstate/resource_list.rb, line 45
def reload
  clear && lazy_load
  self
end

Private Instance Methods

lazy_load() click to toggle source
# File lib/passwordstate/resource_list.rb, line 133
def lazy_load
  all
end