Module: ActiveRemote::Search::ClassMethods

Defined in:
lib/active_remote/search.rb

Instance Method Summary collapse

Instance Method Details

#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'))


27
28
29
30
31
32
# File 'lib/active_remote/search.rb', line 27

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

  remote
end

#find_by(args) ⇒ Object

Tries to load the first record; if it fails, returns nil.

Examples

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

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

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


47
48
49
# File 'lib/active_remote/search.rb', line 47

def find_by(args)
  search(args).first
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'))


62
63
64
65
66
# File 'lib/active_remote/search.rb', line 62

def first_or_create(attributes)
  remote = search(attributes).first
  remote ||= 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.



71
72
73
74
75
# File 'lib/active_remote/search.rb', line 71

def first_or_create!(attributes)
  remote = search(attributes).first
  remote ||= 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'))


88
89
90
91
92
# File 'lib/active_remote/search.rb', line 88

def first_or_initialize(attributes)
  remote = search(attributes).first
  remote ||= 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'))


105
106
107
108
109
110
111
112
113
114
115
# File 'lib/active_remote/search.rb', line 105

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

  response = remote_call(:search, args)

  if response.respond_to?(:records)
    serialize_records(response.records)
  else
    response
  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



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/active_remote/search.rb', line 120

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