Module: ActiveRemote::Search::ClassMethods

Defined in:
lib/active_remote/search.rb

Instance Method Summary collapse

Instance Method Details

#_active_remote_search_args(args) ⇒ Object

:noapi:



18
19
20
21
22
# File 'lib/active_remote/search.rb', line 18

def _active_remote_search_args(args)
  warn "DEPRECATED Model._active_remote_search_args is depracted and will be remoted in Active Remote 3.0."

  validate_search_args!(args)
end

#find(args) ⇒ Object

Tries to load the first record; if it fails, an exception is raised.

Examples

# A single hash
Tag.find(:guid => 'foo')

# Active remote object
Tag.find(Tag.new(:guid => 'foo'))

# Protobuf object
Tag.find(Generic::Remote::TagRequest.new(:guid => 'foo'))


37
38
39
40
41
42
# File 'lib/active_remote/search.rb', line 37

def find(args)
  remote = self.search(args).first
  raise RemoteRecordNotFound.new(self) if remote.nil?

  return remote
end

#first_or_create(attributes) ⇒ Object

Tries to load the first record; if it fails, then create is called with the same arguments.

Examples

# A single hash
Tag.first_or_create(:name => 'foo')

# Protobuf object
Tag.first_or_create(Generic::Remote::TagRequest.new(:name => 'foo'))


55
56
57
58
59
# File 'lib/active_remote/search.rb', line 55

def first_or_create(attributes)
  remote = self.search(attributes).first
  remote ||= self.create(attributes)
  remote
end

#first_or_create!(attributes) ⇒ Object

Tries to load the first record; if it fails, then create! is called with the same arguments.



64
65
66
67
68
# File 'lib/active_remote/search.rb', line 64

def first_or_create!(attributes)
  remote = self.search(attributes).first
  remote ||= self.create!(attributes)
  remote
end

#first_or_initialize(attributes) ⇒ Object

Tries to load the first record; if it fails, then a new record is initialized with the same arguments.

Examples

# A single hash
Tag.first_or_initialize(:name => 'foo')

# Protobuf object
Tag.first_or_initialize(Generic::Remote::TagRequest.new(:name => 'foo'))


81
82
83
84
85
# File 'lib/active_remote/search.rb', line 81

def first_or_initialize(attributes)
  remote = self.search(attributes).first
  remote ||= self.new(attributes)
  remote
end

#search(args) ⇒ Object

Searches for records with the given arguments. Returns a collection of Active Remote objects.

Examples

# A single hash
Tag.search(:name => 'foo')

# Protobuf object
Tag.search(Generic::Remote::TagRequest.new(:name => 'foo'))


98
99
100
101
102
103
104
105
106
107
# File 'lib/active_remote/search.rb', line 98

def search(args)
  args = validate_search_args!(args)

  response = rpc.execute(:search, args)

  if response.respond_to?(:records)
    records = serialize_records(response.records)
    records.each { |record| record.run_callbacks :search }
  end
end

#validate_search_args!(args) ⇒ Object

Validates the given args to ensure they are compatible Search args must be a hash or respond to to_hash



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/active_remote/search.rb', line 112

def validate_search_args!(args)
  unless args.is_a?(Hash)
    if args.respond_to?(:to_hash)
      args = args.to_hash
    else
      raise "Invalid parameter: #{args}. Search args must respond to :to_hash."
    end
  end

  args
end