Class: Passwordstate::ResourceList

Inherits:
Array
  • Object
show all
Defined in:
lib/passwordstate/resource_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, resource, options = {}) ⇒ ResourceList

Returns a new instance of ResourceList.



30
31
32
33
34
35
36
37
38
# File 'lib/passwordstate/resource_list.rb', line 30

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



28
29
30
# File 'lib/passwordstate/resource_list.rb', line 28

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/passwordstate/resource_list.rb', line 28

def options
  @options
end

#resourceObject (readonly)

Returns the value of attribute resource.



28
29
30
# File 'lib/passwordstate/resource_list.rb', line 28

def resource
  @resource
end

Instance Method Details

#all(query = {}) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/passwordstate/resource_list.rb', line 80

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

#clearObject



40
41
42
# File 'lib/passwordstate/resource_list.rb', line 40

def clear
  @loaded = super
end

#create(data) ⇒ Object



65
66
67
68
69
70
# File 'lib/passwordstate/resource_list.rb', line 65

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 = {}) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/passwordstate/resource_list.rb', line 112

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 = {}) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/passwordstate/resource_list.rb', line 88

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

#inspectObject



23
24
25
26
# File 'lib/passwordstate/resource_list.rb', line 23

def inspect
  lazy_load unless @loaded
  super
end

#load(entries) ⇒ Object



49
50
51
52
# File 'lib/passwordstate/resource_list.rb', line 49

def load(entries)
  clear && entries.each { |obj| self << obj }
  true
end

#new(data) ⇒ Object



61
62
63
# File 'lib/passwordstate/resource_list.rb', line 61

def new(data)
  resource.new options.fetch(:object_data, {}).merge(data).merge(_client: client)
end

#operation_supported?(operation) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
# File 'lib/passwordstate/resource_list.rb', line 54

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 = {}) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/passwordstate/resource_list.rb', line 96

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 = {}) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/passwordstate/resource_list.rb', line 104

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

#reloadObject



44
45
46
47
# File 'lib/passwordstate/resource_list.rb', line 44

def reload
  clear && lazy_load
  self
end

#search(query = {}) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/passwordstate/resource_list.rb', line 72

def search(query = {})
  raise 'Operation not supported' unless operation_supported?(:search)
  api_path = options.fetch(:search_path, resource.api_path)
  query = options.fetch(:search_query, {}).merge(query)

  resource.search(client, query.merge(_api_path: api_path))
end