Class: Believer::Environment::BaseEnv
- Inherits:
-
Object
- Object
- Believer::Environment::BaseEnv
- Defined in:
- lib/believer/environment/base_env.rb
Constant Summary collapse
- DEFAULT_CONFIG =
{ :pool => { :size => 1, :timeout => 10 }, :believer => { :logger => { :use_environment => true, :level => ::Logger::DEBUG } } }
Instance Method Summary collapse
-
#believer_configuration ⇒ Object
The connection_pool configuration, which should be a :pool node in the configuration.
-
#configuration ⇒ Object
Returns the configuration.
-
#configuration=(config) ⇒ Object
Sets the configuration.
- #connection_configuration ⇒ Object
-
#connection_pool_configuration ⇒ Object
The connection_pool configuration, which should be a :pool node in the configuration.
-
#create_connection(options = {}) ⇒ Object
Creates a new connection.
- #create_keyspace(properties = {}, connection = nil) ⇒ Object
- #drop_keyspace ⇒ Object
-
#initialize(config = nil) ⇒ BaseEnv
constructor
Creates a new environment using the provided configuration.
- #logger ⇒ Object
Constructor Details
#initialize(config = nil) ⇒ BaseEnv
Creates a new environment using the provided configuration
19 20 21 |
# File 'lib/believer/environment/base_env.rb', line 19 def initialize(config = nil) @configuration = config.dup unless config.nil? end |
Instance Method Details
#believer_configuration ⇒ Object
The connection_pool configuration, which should be a :pool node in the configuration.
64 65 66 |
# File 'lib/believer/environment/base_env.rb', line 64 def believer_configuration configuration[:believer] end |
#configuration ⇒ Object
Returns the configuration. This configuration hash should contain the cql-rb client connection parameters. Optionally the connection_pool configuraton can be included in a :pool node.
38 39 40 41 42 43 44 45 |
# File 'lib/believer/environment/base_env.rb', line 38 def configuration unless @configuration loaded = load_configuration config = HashWithIndifferentAccess.new(DEFAULT_CONFIG.merge(loaded)) @configuration = config end @configuration end |
#configuration=(config) ⇒ Object
Sets the configuration
48 49 50 |
# File 'lib/believer/environment/base_env.rb', line 48 def configuration=(config) @configuration = config end |
#connection_configuration ⇒ Object
52 53 54 |
# File 'lib/believer/environment/base_env.rb', line 52 def connection_configuration configuration.reject { |k, v| k == :pool || k == :believer } end |
#connection_pool_configuration ⇒ Object
The connection_pool configuration, which should be a :pool node in the configuration.
57 58 59 60 61 |
# File 'lib/believer/environment/base_env.rb', line 57 def connection_pool_configuration pc = configuration[:pool] return DEFAULT_CONFIG[:pool] unless pc pc end |
#create_connection(options = {}) ⇒ Object
Creates a new connection
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/believer/environment/base_env.rb', line 69 def create_connection( = {}) cc = connection_configuration if [:connect_to_keyspace] && cc[:keyspace] connection = Cql::Client.connect(cc) connection.use(cc[:keyspace]) else connection = Cql::Client.connect(cc.delete_if { |k, v| k == :keyspace }) end connection end |
#create_keyspace(properties = {}, connection = nil) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/believer/environment/base_env.rb', line 85 def create_keyspace(properties = {}, connection = nil) conn = connection || create_connection(:connect_to_keyspace => false) default_properties = {:replication => {:class => 'SimpleStrategy', :replication_factor => 1}} ks_props = default_properties.merge(properties) ks_props_s = ks_props.keys.map { |k| v = ks_props[k] v_s = nil if v.is_a?(Hash) v_s = v.to_json.gsub(/\"/) { |m| "'" } elsif v.is_a?(String) v_s = "'#{v}'" else v_s = v.to_s end "#{k} = #{v_s}" }.join("\nAND ") ks_def = " CREATE KEYSPACE \#{connection_configuration[:keyspace]}\n WITH \#{ks_props_s}\n KS_DEF\n\n conn.execute(ks_def)\nend\n" |
#drop_keyspace ⇒ Object
80 81 82 83 |
# File 'lib/believer/environment/base_env.rb', line 80 def drop_keyspace conn = create_connection(:connect_to_keyspace => false) conn.execute("DROP KEYSPACE #{connection_configuration[:keyspace]}") end |
#logger ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/believer/environment/base_env.rb', line 23 def logger return nil unless believer_configuration[:logger] return environment_logger if believer_configuration[:logger][:use_environment] && respond_to?(:environment_logger) unless @std_logger @std_logger = ::Logger.new(STDOUT) if believer_configuration[:logger][:level] && believer_configuration[:logger][:level].is_a?(Numeric) @std_logger.level = believer_configuration[:logger][:level].to_i end end @std_logger end |