ottoman_orm 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 56a607951929d96a8f091da04d65e4be2993faaa
4
+ data.tar.gz: abf2d5e30659b81d608d36ad641befcceb8c9732
5
+ SHA512:
6
+ metadata.gz: 18f22ac4a42bc0d59affb17b95ab8895b370a52a251f92e0b65a8c2baa72e234ad452e75ec562f8c02bdd8463be762005d9903faf52604f619c2f74e25456482
7
+ data.tar.gz: e60c627b4a9638b5d2cba6aefc6ee28b6c9a1ec63a258fdd9305d8c7ec24bc03230fe24b033cc67fa91c40f4a665727bcad8af7b662f3def95c052fd4c566752
@@ -0,0 +1,6 @@
1
+ class Object
2
+
3
+ def blank?
4
+ respond_to?(:empty?) ? empty? : !self
5
+ end
6
+ end
@@ -0,0 +1 @@
1
+ require 'ottoman_orm/core_ext/object/blank'
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/core_ext/*.rb"].each do |path|
2
+ require path
3
+ end
@@ -0,0 +1,44 @@
1
+ module OttomanORM
2
+ class Datastore
3
+
4
+ attr_accessor :client
5
+
6
+ def initialize parameters
7
+ self.connect parameters
8
+ end
9
+
10
+ def create name, data
11
+ execute_sproc(name + '_create', data)
12
+ end
13
+
14
+ def get name, id
15
+ execute_sproc(name + '_get', id).values()
16
+ end
17
+
18
+ def get_by_field name, field, value
19
+ execute_sproc(name + '_get_by_' + field, value).values()
20
+ end
21
+
22
+ protected
23
+
24
+ def connect parameters
25
+ config = YAML.load_file(File.join('config', 'ottoman_orm.yml'))[parameters[:mode]]
26
+
27
+ @client = PG::Connection.new(
28
+ :host => config['host'],
29
+ :port => config['port'],
30
+ :dbname => config['dbname'],
31
+ :user => config['user'],
32
+ :password => config['password']
33
+ )
34
+ end; alias :reconnect! :connect
35
+
36
+ def execute_sproc(sproc, args)
37
+ @client.exec(build_query_string(sproc, args))
38
+ end
39
+
40
+ def build_query_string(sproc, args)
41
+ query_string = 'select ' + sproc + '(\'' + args + '\')'
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ module OttomanORM
2
+ module Error
3
+
4
+ end
5
+ end
@@ -0,0 +1,129 @@
1
+ module OttomanORM
2
+ class Model
3
+ # representation methods
4
+ include OttomanORM::Representation
5
+
6
+ # internal references
7
+ attr_accessor :_id
8
+
9
+ # attributes list
10
+ @@_attributes = []
11
+
12
+ # Initializes a new model with the given +params+.
13
+ #
14
+ # class Person
15
+ # include OttomanORM::Model
16
+ # attr_accessor :name, :age
17
+ # end
18
+ #
19
+ # person = Person.new(name: 'bob', age: '18')
20
+ # person.name # => "bob"
21
+ # person.age # => 18
22
+ def initialize(params={})
23
+ params.each do |attr, value|
24
+ self.public_send("#{attr}=", value)
25
+ end if params
26
+
27
+ super()
28
+ end
29
+
30
+ # attributes
31
+ def self.attribute *names
32
+ names.each { |name| @@_attributes << name }
33
+ class_eval { attr_accessor *names }
34
+ end
35
+ class << self; alias :attributes :attribute; end
36
+
37
+ def attributes
38
+ @@_attributes
39
+ end
40
+
41
+ # object creation
42
+ def self.create data
43
+ self.new(data).save
44
+ end
45
+
46
+ # Indicates if the model is persisted. Default is +false+.
47
+ #
48
+ # class Person
49
+ # include OttomanORM::Model
50
+ # attr_accessor :id, :name
51
+ # end
52
+ #
53
+ # person = Person.new(id: 1, name: 'bob')
54
+ # person.persisted? # => false
55
+ def persisted?
56
+ !new_record?
57
+ end
58
+
59
+ def new_record?
60
+ @_id.blank?
61
+ end
62
+
63
+ def id
64
+ @_id
65
+ end
66
+
67
+ # validations
68
+ def run_validations
69
+ true
70
+ end
71
+
72
+ # save record
73
+ def save
74
+ return false unless run_validations
75
+ create_or_update
76
+ end
77
+
78
+ def create_or_update
79
+ new_record? ? create_record : update_record
80
+ end
81
+
82
+ def create_record
83
+ @_id = OttomanORM.client.create self.class.name, self.to_json
84
+ self
85
+ end
86
+
87
+ def update_record
88
+ OttomanORM.client.update @_id, self.to_hash
89
+ self
90
+ end
91
+
92
+ def self.fetch name, id
93
+ # r = []
94
+ # OttomanORM.client.get(id).each_pair do |key, value|
95
+ # if v[0].is_a?(Hash)
96
+ # instance = new(value[0].extract!(*@@_attributes))
97
+ # instance._id = key
98
+ # r << instance
99
+ # end
100
+ # end
101
+ # r.empty? ? nil : r.length == 1 ? r[0] : r
102
+ OttomanORM.client.get name, id
103
+ end
104
+
105
+ def self.fetch_by_field name, field, value
106
+ OttomanORM.client.get_by_field name, field, value
107
+ end
108
+
109
+ # update record
110
+ def update_attribute attribute, value
111
+ public_send("#{attribute}=", value)
112
+ save
113
+ end
114
+
115
+ def update_attributes attributes
116
+ return if attributes.blank?
117
+ attributes.stringify_keys.each_pair do |attribute, value|
118
+ public_send("#{attribute}=", value)
119
+ end
120
+ save
121
+ end
122
+
123
+ # delete record
124
+ def delete
125
+ OttomanORM.client.delete(@_id) unless new_record?
126
+ freeze
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,15 @@
1
+ module OttomanORM
2
+ module Representation
3
+ def to_hash
4
+ pairs = {}
5
+ attributes.each do |attribute|
6
+ pairs[attribute] = send(attribute)
7
+ end
8
+ pairs
9
+ end
10
+
11
+ def to_json
12
+ self.to_hash.to_json
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module OttomanORM
2
+ module Version
3
+ VERSION = '0.0.2'
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ require 'pg'
2
+ require 'yaml'
3
+ require 'json'
4
+
5
+ require 'ottoman_orm/datastore'
6
+ require 'ottoman_orm/error'
7
+ require 'ottoman_orm/representation'
8
+ require 'ottoman_orm/model'
9
+ require 'ottoman_orm/version'
10
+ require 'ottoman_orm/core_ext'
11
+
12
+ module OttomanORM
13
+ @@client = nil
14
+
15
+ def self.connect parameters = {}
16
+ @@client = Datastore.new(parameters)
17
+ end
18
+
19
+ def self.client
20
+ @@client
21
+ end
22
+
23
+ def self.hello
24
+ 'hello'
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ottoman_orm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Craig McCown
8
+ - Spencer Applegate
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A lightweight ORM for Sinatra with PostgreSQL -- longer description
15
+ email: spencer@thotpod.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/ottoman_orm/core_ext/object/blank.rb
21
+ - lib/ottoman_orm/core_ext/object.rb
22
+ - lib/ottoman_orm/core_ext.rb
23
+ - lib/ottoman_orm/datastore.rb
24
+ - lib/ottoman_orm/error.rb
25
+ - lib/ottoman_orm/model.rb
26
+ - lib/ottoman_orm/representation.rb
27
+ - lib/ottoman_orm/version.rb
28
+ - lib/ottoman_orm.rb
29
+ homepage: https://github.com/Thotpod/ottoman-orm
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.1.5
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: A lightweight ORM for Sinatra with PostgreSQL
53
+ test_files: []