Class: MagLoft::RemoteCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/magloft/remote_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource_class, filter = {}) ⇒ RemoteCollection

Returns a new instance of RemoteCollection.



3
4
5
6
# File 'lib/magloft/remote_collection.rb', line 3

def initialize(resource_class, filter = {})
  @resource_class = resource_class
  @filter = filter
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/magloft/remote_collection.rb', line 28

def method_missing(name, *args, &block)
  if name[0..7] == "find_by_" and args.length == 1
    attribute = name[8..-1].to_sym
    if @resource_class.remote_attributes.include?(attribute)
      params = {}
      params[attribute] = args.first
      return self.find_one(params.merge(@filter))
    end
  end
  super
end

Instance Method Details

#allObject



24
25
26
# File 'lib/magloft/remote_collection.rb', line 24

def all
  @resource_class.where(@filter)
end

#create(attributes = {}) ⇒ Object



44
45
46
47
48
# File 'lib/magloft/remote_collection.rb', line 44

def create(attributes = {})
  entity = @resource_class.new(attributes.merge(@filter))
  entity.save
  entity
end

#find(id) ⇒ Object



12
13
14
# File 'lib/magloft/remote_collection.rb', line 12

def find(id)
  @resource_class.where(@filter.merge(id: id)).first
end

#find_one(params) ⇒ Object



20
21
22
# File 'lib/magloft/remote_collection.rb', line 20

def find_one(params)
  @resource_class.find_one(params.merge(@filter))
end

#new(attributes) ⇒ Object



8
9
10
# File 'lib/magloft/remote_collection.rb', line 8

def new(attributes)
  @resource_class.new(attributes.merge(@filter))
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/magloft/remote_collection.rb', line 40

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.start_with?('find_by_') || super
end

#where(params) ⇒ Object



16
17
18
# File 'lib/magloft/remote_collection.rb', line 16

def where(params)
  @resource_class.where(params.merge(@filter))
end