Module: SimpleRecord

Defined in:
lib/simple_record/password.rb,
lib/simple_record.rb,
lib/simple_record/json.rb,
lib/simple_record/stats.rb,
lib/simple_record/errors.rb,
lib/simple_record/logging.rb,
lib/simple_record/sharding.rb,
lib/simple_record/callbacks.rb,
lib/simple_record/encryptor.rb,
lib/simple_record/active_sdb.rb,
lib/simple_record/attributes.rb,
lib/simple_record/validations.rb,
lib/simple_record/translations.rb,
lib/simple_record/results_array.rb

Overview

This module defines all the methods that perform data translations for storage and retrieval.

Defined Under Namespace

Modules: Attributes, Callbacks, Callbacks3, Encryptor, Json, Logging, Password, Sharding, Translations, Validations Classes: ActiveSdb, Activerecordtosdb_subrecord_array, Base, Error, PasswordHashed, RecordNotFound, RecordNotSaved, RemoteNil, ResultsArray, SimpleRecordError, SimpleRecord_errors, Stats

Constant Summary collapse

@@options =
{}
@@stats =
SimpleRecord::Stats.new
@@logging =
false
@@s3 =
nil
@@auto_close_s3 =
false
@@logger =
Logger.new(STDOUT)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.aws_access_keyObject

Returns the value of attribute aws_access_key.



68
69
70
# File 'lib/simple_record.rb', line 68

def aws_access_key
  @aws_access_key
end

.aws_secret_keyObject

Returns the value of attribute aws_secret_key.



68
69
70
# File 'lib/simple_record.rb', line 68

def aws_secret_key
  @aws_secret_key
end

Class Method Details

.close_connectionObject

Call this to close the connection to SimpleDB. If you’re using this in Rails with per_thread connection mode, you should do this in an after_filter for each request.



161
162
163
164
# File 'lib/simple_record.rb', line 161

def close_connection()
  SimpleRecord::ActiveSdb.close_connection
  @@s3.close_connection if @@auto_close_s3
end

.close_usage_log(type) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/simple_record.rb', line 103

def close_usage_log(type)
  return unless @usage_logging_options[type]
  @usage_logging_options[type][:file].close if @usage_logging_options[type][:file]
  # unless we remove it, it will keep trying to log these events
  # and will fail because the file is closed.
  @usage_logging_options.delete(type)
end

.disable_loggingObject

Deprecated



77
78
79
# File 'lib/simple_record.rb', line 77

def disable_logging
  @@logging = false
end

.enable_loggingObject

Deprecated



71
72
73
74
# File 'lib/simple_record.rb', line 71

def enable_logging
  @@logging = true
  @@logger.level = Logger::DEBUG
end

.establish_connection(aws_access_key = nil, aws_secret_key = nil, options = {}) ⇒ Object

Create a new handle to an Sdb account. All handles share the same per process or per thread HTTP connection to Amazon Sdb. Each handle is for a specific account. The params are passed through as-is to Aws::SdbInterface.new Params:

{ :server       => 'sdb.amazonaws.com'  # Amazon service host: 'sdb.amazonaws.com'(default)
  :port         => 443                  # Amazon service port: 80(default) or 443
  :protocol     => 'https'              # Amazon service protocol: 'http'(default) or 'https'
  :signature_version => '0'             # The signature version : '0' or '1'(default)
  :connection_mode  => :default         # options are
                                              :default (will use best known safe (as in won't need explicit close) option, may change in the future)
                                              :per_request (opens and closes a connection on every request to SDB)
                                              :single (one thread across entire app)
                                              :per_thread (one connection per thread)
                                              :pool (uses a connection pool with a maximum number of connections - NOT IMPLEMENTED YET)
  :logger       => Logger Object        # Logger instance: logs to STDOUT if omitted


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/simple_record.rb', line 135

def establish_connection(aws_access_key=nil, aws_secret_key=nil, options={})
  @aws_access_key = aws_access_key
  @aws_secret_key = aws_secret_key
  @@options.merge!(options)
    #puts 'SimpleRecord.establish_connection with options: ' + @@options.inspect
  SimpleRecord::ActiveSdb.establish_connection(aws_access_key, aws_secret_key, @@options)
  if options[:connection_mode] == :per_thread
    @@auto_close_s3 = true
    # todo: should we init this only when needed?
  end
  s3_ops = {:connection_mode=>options[:connection_mode] || :default}
  @@s3 = Aws::S3.new(SimpleRecord.aws_access_key, SimpleRecord.aws_secret_key, s3_ops)

  if options[:created_col]
    SimpleRecord::Base.has_dates options[:created_col]
  end
  if options[:updated_col]
    SimpleRecord::Base.has_dates options[:updated_col]
  end


end

.log_usage(types = {}) ⇒ Object

This can be used to log queries and what not to a file. Params: :select=>:format=>“csv”



93
94
95
96
97
98
99
100
101
# File 'lib/simple_record.rb', line 93

def log_usage(types={})
  @usage_logging_options = {} unless @usage_logging_options
  return if types.nil?
  types.each_pair do |type, options|
    options[:lines_between_flushes] = 100 unless options[:lines_between_flushes]
    @usage_logging_options[type] = options
  end
  #puts 'SimpleRecord.usage_logging_options=' + SimpleRecord.usage_logging_options.inspect
end

.loggerObject



86
87
88
# File 'lib/simple_record.rb', line 86

def logger
  @@logger
end

.logging?Boolean

Deprecated

Returns:

  • (Boolean)


82
83
84
# File 'lib/simple_record.rb', line 82

def logging?
  @@logging
end

.optionsObject



177
178
179
# File 'lib/simple_record.rb', line 177

def options
  @@options
end

.s3Object



173
174
175
# File 'lib/simple_record.rb', line 173

def s3
  @@s3
end

.s3=(s3) ⇒ Object

If you’d like to specify the s3 connection to use for LOBs, you can pass it in here. We recommend that this connection matches the type of connection you’re using for SimpleDB, at least if you’re using per_thread connection mode.



169
170
171
# File 'lib/simple_record.rb', line 169

def s3=(s3)
  @@s3 = s3
end

.statsObject



115
116
117
# File 'lib/simple_record.rb', line 115

def stats
  @@stats
end

.usage_logging_optionsObject



111
112
113
# File 'lib/simple_record.rb', line 111

def usage_logging_options
  @usage_logging_options
end