believer 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,2 +1,132 @@
1
+ # CQL3 Object Relational Mapping
2
+ Believer is an Object Relational Mapping library for CQL3
3
+
4
+ ## Installation
5
+ gem install believer
6
+
7
+ ## Inspiration
8
+ The Believer library is heavily inspired by ActiveRecord. Most patterns used in this library should be pretty familiar for ActiveRecord users.
9
+
10
+ ## Usage
11
+
12
+ ### Define your class
13
+ An example:
14
+
15
+ require 'believer'
16
+
17
+ class Artist < Believer::Base
18
+ column :name
19
+ column :label
20
+
21
+ primary_key :name
22
+ end
23
+
24
+ class Album < Believer::Base
25
+ column :artist
26
+ column :name
27
+ column :release_date, :type => :timestamp
28
+
29
+ primary_key :artist, :name
30
+ end
31
+
32
+ class Song < Believer::Base
33
+ column :artist
34
+ column :album
35
+ column :name
36
+ column :track_number, :type => :integer
37
+
38
+ primary_key :artist, :album, :name
39
+ end
40
+
41
+ #### The Believer::Base class
42
+ This is the class you should extend from.
43
+
44
+ #### The column class method
45
+ Defines the mapping between a Ruby object attribute and a Cassandra column. Also defines a getter and setter attribute with the same name.
46
+ The second argument is a Hash, which support the following keys:
47
+ * type: the data type. Supported are: :string, :integer, :float, :timestamp
48
+
49
+
50
+ #### The primary_key class method
51
+ Sets the primary key columns of the class.
52
+ In a situation where you're only querying data, you don't need to set this.
53
+ However, if you rely on object equality somewhere in your application, it is advisable to set the primary key, as the primary key values are used in the Believer::Base.eql? method.
54
+
55
+ ### Query your class
56
+ The following methods can be used to query class instances.
57
+ * where: specify query filters
58
+ * select: specify which fields to load. Using none defaults to all fields being loaded.
59
+ * limit: limit the number of results to a maximum
60
+ * order: specify the order of the results
61
+
62
+ All methods are chainable, meaning you can
63
+
64
+ #### The where method
65
+ Use the where method to specify filters on the result set. These filters correspond to the expressions in the WHERE clause of a Cassandra query.
66
+
67
+ # Using a hash
68
+ Artist.where(:name => 'James Brown')
69
+
70
+ # Using a hash mapping key to an array of possible values. Maps to the CQL IN operator
71
+ Artist.where(:name => ['Coldplay', 'Depeche Mode', 'Eurythmics'])
72
+
73
+ # Using string with interpolation
74
+ Artist.where('name = ?', 'Foreigner')
75
+
76
+ #### The select method
77
+ Using the select method you can define the columns loaded in a query. These fields correspond to the expressions in the SELECT clause of a Cassandra query.
78
+ This might be handy in the case you have a table with a lot of columns, but only need a few.
79
+
80
+ # Select a single field
81
+ Artist.select(:name)
82
+
83
+ # Select a multiple fields
84
+ Artist.select(:name, :label)
85
+
86
+ #### The limit method
87
+ Limits the amount of records returned to the specified maximum
88
+
89
+ # Yield at most 20 class instances
90
+ Artist.limit(20)
91
+
92
+ #### The order method
93
+ Order the results in using the specified column
94
+
95
+ # Order ascending by name
96
+ Album.order(:name)
97
+ Album.order(:name, :asc)
98
+
99
+ # Order descending by name
100
+ Album.order(:name, :desc)
101
+
102
+ #### Method chaining
103
+ All query methods can be chained.
104
+ This is done by creating and returning a clone of the receiver. The clone is the receiver of the query method.
105
+
106
+ # 'Echoes'....
107
+ Song.where(:artist => 'Pink Floyd').where(:album => 'Meddle').order(:track_number, :desc).limit(1)
108
+
109
+ ### Configuration
110
+ If using Rails, place a believer.yml file in the configuration directory of your application.
111
+ The file structure starts with the the environment name, followed by the connection configuration.
112
+ This is the client connection configuration passed to the cql-rb gem.
113
+
114
+ development:
115
+ host: 127.0.0.1
116
+ port: 9042
117
+ keyspace: my_keyspace
118
+
119
+ staging:
120
+ host: 'staging.mynetwork.local'
121
+ port: 9042
122
+ keyspace: my_keyspace
123
+ credentials:
124
+ username: john
125
+ password: $FDFD%@#&*
126
+
127
+
128
+ In other cases, you will have to programatically set the environment:
129
+
130
+ Believer::Base.environment = Believer::Environment::BaseEnv.new(:host => '127.0.0.1', :keyspace => 'mykeyspace')
1
131
 
2
132
 
@@ -20,6 +20,28 @@ module Believer
20
20
  end
21
21
 
22
22
  class Column
23
+ #TYPES = {
24
+ # :ascii strings US-ASCII character string
25
+ #bigint integers 64-bit signed long
26
+ #blob blobs Arbitrary bytes (no validation), expressed as hexadecimal
27
+ #boolean booleans true or false
28
+ #counter integers Distributed counter value (64-bit long)
29
+ #decimal integers, floats Variable-precision decimal
30
+ #double integers 64-bit IEEE-754 floating point
31
+ #float integers, floats 32-bit IEEE-754 floating point
32
+ #inet strings IP address string in IPv4 or IPv6 format*
33
+ # int integers 32-bit signed integer
34
+ #list n/a A collection of one or more ordered elements
35
+ #map n/a A JSON-style array of literals: { literal : literal, literal : literal ... }
36
+ #set n/a A collection of one or more elements
37
+ #text strings UTF-8 encoded string
38
+ #timestamp integers, strings Date plus time, encoded as 8 bytes since epoch
39
+ #uuid uuids A UUID in standard UUID format
40
+ #timeuuid uuids Type 1 UUID only (CQL 3)
41
+ #varchar strings UTF-8 encoded string
42
+ #varint integers Arbitrary-precision integer
43
+ #}
44
+ #
23
45
  CQL_COL_TYPES = {
24
46
  :integer => 'INT',
25
47
  :string => 'VARCHAR',
@@ -7,8 +7,11 @@ module Believer
7
7
  module ClassMethods
8
8
 
9
9
  def environment
10
+
10
11
  if @environment.nil?
11
- if defined?(::Rails)
12
+ if self.superclass.respond_to?(:environment)
13
+ @environment = self.superclass.environment
14
+ elsif defined?(::Rails)
12
15
  @environment = ::Believer::Environment::RailsEnv.new
13
16
  end
14
17
  end
@@ -26,6 +29,22 @@ module Believer
26
29
  # {:host => '127.0.0.1'}
27
30
  #end
28
31
 
32
+ def initialize(config = nil)
33
+ @configuration = config.dup unless config.nil?
34
+ end
35
+
36
+ def configuration
37
+ @configuration ||= load_configuration
38
+ end
39
+
40
+ def configuration=(config)
41
+ @configuration = config
42
+ end
43
+
44
+ def connection_configuration
45
+ @connection_configuration ||= configuration.reject_if {|k, v| k == :pool}
46
+ end
47
+
29
48
  def create_connection(options = {})
30
49
  cc = connection_configuration
31
50
 
@@ -33,20 +52,20 @@ module Believer
33
52
  connection = Cql::Client.connect(cc)
34
53
  connection.use(cc[:keyspace])
35
54
  else
36
- connection = Cql::Client.connect(cc.delete_if {|k,v|k==:keyspace})
55
+ connection = Cql::Client.connect(cc.delete_if {|k,v|k == :keyspace})
37
56
  end
38
57
  connection
39
58
  end
40
59
 
41
60
  def create_keyspace(connection = nil)
42
61
  conn = connection || create_connection
43
- ks_def = <<-KSDEF
62
+ ks_def = <<-KS_DEF
44
63
  CREATE KEYSPACE #{connection_configuration[:keyspace]}
45
64
  WITH replication = {
46
65
  'class': 'SimpleStrategy',
47
66
  'replication_factor': 1
48
67
  }
49
- KSDEF
68
+ KS_DEF
50
69
 
51
70
  conn.execute(ks_def)
52
71
  end
@@ -3,16 +3,13 @@ module Believer
3
3
  module Environment
4
4
  class RailsEnv < Believer::Environment::BaseEnv
5
5
 
6
- def connection_configuration
7
- unless @connection_configuration
8
- config_file = Rails.root + "config/cql.yml"
9
- #puts "Using CQL connection config file: #{config_file}"
10
- config = YAML::load(File.open(config_file.to_s))
11
- env_config = config[Rails.env]
12
- @connection_configuration = env_config.symbolize_keys
13
- @connection_configuration[:logger] = Rails.logger
14
- end
15
- @connection_configuration
6
+ def load_configuration
7
+ config_file = Rails.root + "config/believer.yml"
8
+ config = YAML::load(File.open(config_file.to_s))
9
+ env_config = config[Rails.env]
10
+ env_config = {}.merge(env_config).symbolize_keys
11
+ env_config[:logger] = Rails.logger
12
+ env_config
16
13
  end
17
14
  end
18
15
  end
@@ -1,5 +1,5 @@
1
1
  module Believer
2
2
  module Version
3
- VERSION = '0.1.2'
3
+ VERSION = '0.1.3'
4
4
  end
5
5
  end
@@ -51,8 +51,9 @@ module Test
51
51
  @env ||= Environment.new
52
52
  end
53
53
 
54
+ Believer::Base.environment = test_environment
54
55
  CLASSES = [Processor, Computer, Event, Person]
55
- CLASSES.each {|cl| cl.environment = test_environment}
56
+ #CLASSES.each {|cl| cl.environment = test_environment}
56
57
 
57
58
  def self.classes
58
59
  CLASSES
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: believer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: