mor 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +17 -1
- data/lib/mor.rb +21 -4
- data/lib/mor/version.rb +1 -1
- data/spec/model_spec.rb +2 -4
- data/spec/mor_spec.rb +0 -1
- data/spec/support/minitest_helper.rb +9 -1
- metadata +2 -6
- data/config/cache.yml +0 -20
- data/config/env.rb +0 -13
- data/lib/mor.old.rb +0 -147
- data/lib/mor/client.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ddaa2cec471c5f0c67af4bfabc0ef9753c5f8e36
|
4
|
+
data.tar.gz: a2fb5a1f529128cefa2890385608faf55154dc5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74b5fa096acbe7deee0c2a598f62f2dc3337f34a110579a4b869548eafd98cc255cf239e08812085a7eb50d24ff7db4a7e62c361ed8b5623c4a4527e63dd8051
|
7
|
+
data.tar.gz: 4f0f4675842c82bef9112978d546ca15fefdac8090fc1490f5cd15b96cffa89463e61e43baaed5a5187ecdf4384ebb53b512eab8e84cde2bc3d6b8253880ce26
|
data/README.md
CHANGED
@@ -18,7 +18,23 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
require 'mor'
|
22
|
+
|
23
|
+
Mor.config do |dalli|
|
24
|
+
dalli['servers'] = "localhost:11211"
|
25
|
+
end
|
26
|
+
|
27
|
+
class Person
|
28
|
+
include Mor::Model
|
29
|
+
attr_accessor :id, :name
|
30
|
+
end
|
31
|
+
|
32
|
+
frank = Person.new(id: 1, name: "Frank")
|
33
|
+
frank.persisted? # false
|
34
|
+
frank.valid? # true
|
35
|
+
frank.save # true
|
36
|
+
|
37
|
+
Person.find(1) # <Person:0x007fc510892b00 @name="Frank">
|
22
38
|
|
23
39
|
## Contributing
|
24
40
|
|
data/lib/mor.rb
CHANGED
@@ -1,12 +1,29 @@
|
|
1
1
|
require 'mor/version'
|
2
|
-
require '
|
2
|
+
require 'dalli'
|
3
3
|
|
4
4
|
module Mor
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
@@dalli = {
|
7
|
+
compress: true,
|
8
|
+
threadsafe: true,
|
9
|
+
servers: ENV['MEMCACHIER_SERVERS'],
|
10
|
+
password: ENV['MEMCACHIER_PASSWORD'],
|
11
|
+
username: ENV['MEMCACHIER_USERNAME'],
|
12
|
+
async: true
|
13
|
+
}
|
14
|
+
|
15
|
+
def self.dalli
|
16
|
+
@@dalli
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.config
|
20
|
+
yield self
|
8
21
|
end
|
9
22
|
|
10
|
-
|
23
|
+
def self.cache
|
24
|
+
@@cache ||= begin
|
25
|
+
::Dalli::Client.new self.dalli["servers"].split(","), self.dalli.reject{|k,v| k == "servers"}
|
26
|
+
end
|
27
|
+
end
|
11
28
|
|
12
29
|
end
|
data/lib/mor/version.rb
CHANGED
data/spec/model_spec.rb
CHANGED
@@ -1,15 +1,13 @@
|
|
1
1
|
require 'minitest_helper'
|
2
|
-
require 'mor'
|
3
2
|
require 'mor/model'
|
4
3
|
|
5
4
|
class TestModel
|
6
5
|
include Mor::Model
|
7
|
-
attr_accessor :id
|
8
|
-
|
6
|
+
attr_accessor :id, :title, :body
|
9
7
|
end
|
10
8
|
|
11
9
|
describe Mor::Model do
|
12
|
-
|
10
|
+
|
13
11
|
it "acts as active_model/model" do
|
14
12
|
instance = TestModel.new(title: "title", body: "test body")
|
15
13
|
instance.title.must_equal "title"
|
data/spec/mor_spec.rb
CHANGED
@@ -4,7 +4,15 @@ require 'minitest/pride'
|
|
4
4
|
require 'minitest/autorun'
|
5
5
|
require 'awesome_print'
|
6
6
|
|
7
|
-
|
7
|
+
require 'mor'
|
8
|
+
|
9
|
+
require 'i18n'
|
10
|
+
I18n.enforce_available_locales = false
|
11
|
+
|
12
|
+
Mor.config do |mor|
|
13
|
+
mor.dalli['namespace'] = "mor:test"
|
14
|
+
mor.dalli['servers'] = "localhost:11211"
|
15
|
+
end
|
8
16
|
|
9
17
|
class MiniTest::Spec
|
10
18
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Onur Uyar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -106,11 +106,7 @@ files:
|
|
106
106
|
- LICENSE.txt
|
107
107
|
- README.md
|
108
108
|
- Rakefile
|
109
|
-
- config/cache.yml
|
110
|
-
- config/env.rb
|
111
|
-
- lib/mor.old.rb
|
112
109
|
- lib/mor.rb
|
113
|
-
- lib/mor/client.rb
|
114
110
|
- lib/mor/model.rb
|
115
111
|
- lib/mor/version.rb
|
116
112
|
- mor.gemspec
|
data/config/cache.yml
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
development:
|
2
|
-
compress: true
|
3
|
-
threadsafe: true
|
4
|
-
namespace: mor:development
|
5
|
-
servers: localhost:11211
|
6
|
-
async: true
|
7
|
-
test:
|
8
|
-
compress: true
|
9
|
-
threadsafe: true
|
10
|
-
namespace: mor:test
|
11
|
-
servers: localhost:11211
|
12
|
-
async: true
|
13
|
-
production:
|
14
|
-
compress: true
|
15
|
-
threadsafe: true
|
16
|
-
namespace: mor
|
17
|
-
servers: <%= ENV['MEMCACHIER_SERVERS'] %>
|
18
|
-
password: <%= ENV['MEMCACHIER_PASSWORD'] %>
|
19
|
-
username: <%= ENV['MEMCACHIER_USERNAME'] %>
|
20
|
-
async: true
|
data/config/env.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
# real time stdout
|
2
|
-
$stdout.sync = true
|
3
|
-
|
4
|
-
%w(. lib).each do |dir|
|
5
|
-
path = File.expand_path( "../../#{dir}", __FILE__)
|
6
|
-
$LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
|
7
|
-
end
|
8
|
-
|
9
|
-
require 'bundler/setup'
|
10
|
-
Bundler.setup
|
11
|
-
|
12
|
-
require 'i18n'
|
13
|
-
I18n.enforce_available_locales = false
|
data/lib/mor.old.rb
DELETED
@@ -1,147 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
require 'erb'
|
3
|
-
require 'dalli'
|
4
|
-
require 'active_model'
|
5
|
-
require 'active_model/callbacks'
|
6
|
-
require 'active_support/hash_with_indifferent_access'
|
7
|
-
require 'active_support/concern'
|
8
|
-
module Mor
|
9
|
-
|
10
|
-
def self.env
|
11
|
-
ENV['RACK_ENV'] ||= "development"
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.cache=cache
|
15
|
-
@@cache=cache
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.cache
|
19
|
-
@@cache ||= begin
|
20
|
-
config = YAML::load(ERB.new(File.read("config/cache.yml")).result)[Mor.env]
|
21
|
-
@instance = Dalli::Client.new config.delete("servers").split(","), config
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
extend ActiveSupport::Concern
|
26
|
-
extend ActiveModel::Callbacks
|
27
|
-
|
28
|
-
module ClassMethods
|
29
|
-
|
30
|
-
def attr_accessor *attrs
|
31
|
-
super(*attrs)
|
32
|
-
attrs.delete_if{ |a| a == :attributes }.each { |attr|
|
33
|
-
define_method(:"#{attr}="){|val| self.attributes[attr]=val}
|
34
|
-
define_method(attr){ self.attributes[attr] }
|
35
|
-
}
|
36
|
-
end
|
37
|
-
|
38
|
-
def key id = primary_key
|
39
|
-
:"#{self.name.downcase}:#{id}"
|
40
|
-
end
|
41
|
-
|
42
|
-
def primary_key
|
43
|
-
:id
|
44
|
-
end
|
45
|
-
|
46
|
-
def create attributes = {}
|
47
|
-
instance = self.new(attributes)
|
48
|
-
instance.save
|
49
|
-
instance
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
included do
|
55
|
-
include ActiveModel::Model
|
56
|
-
extend ActiveModel::Callbacks
|
57
|
-
include ActiveModel::Validations::Callbacks
|
58
|
-
define_model_callbacks :create, :update, :destroy
|
59
|
-
after_create :add_to_index
|
60
|
-
after_destroy :remove_from_index
|
61
|
-
validates_presence_of :id
|
62
|
-
validate :validate_uniqueness_of_id
|
63
|
-
end
|
64
|
-
|
65
|
-
def attributes
|
66
|
-
@attributes ||= ActiveSupport::HashWithIndifferentAccess.new
|
67
|
-
end
|
68
|
-
|
69
|
-
def initialize attributes = {}
|
70
|
-
self.attributes ||= ActiveSupport::HashWithIndifferentAccess.new(attributes)
|
71
|
-
end
|
72
|
-
|
73
|
-
def update attributes={}
|
74
|
-
self.attributes.update attributes
|
75
|
-
save
|
76
|
-
end
|
77
|
-
|
78
|
-
def save
|
79
|
-
if self.valid?
|
80
|
-
if self.persisted?
|
81
|
-
run_callbacks :update do
|
82
|
-
cache.set(self.key,self)
|
83
|
-
end
|
84
|
-
else
|
85
|
-
run_callbacks :create do
|
86
|
-
cache.set(self.key,self)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def destroy
|
93
|
-
run_callbacks :destroy do
|
94
|
-
cache.delete key
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def key
|
99
|
-
self.class.key self.id
|
100
|
-
end
|
101
|
-
|
102
|
-
def persisted?
|
103
|
-
self.indexed?
|
104
|
-
end
|
105
|
-
|
106
|
-
def id
|
107
|
-
self.attributes[self.class.primary_key]
|
108
|
-
end
|
109
|
-
|
110
|
-
# index
|
111
|
-
|
112
|
-
def indexed?
|
113
|
-
@indexed ||= index.include?(self.id)
|
114
|
-
end
|
115
|
-
|
116
|
-
def index
|
117
|
-
@index ||= begin
|
118
|
-
cache.get(self.class.key) || (self.index=[])
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
def index=index
|
123
|
-
@index = begin
|
124
|
-
cache.set(self.class.key,index) ? index : []
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
private
|
129
|
-
|
130
|
-
def add_to_index
|
131
|
-
self.index = index.push(self.id)
|
132
|
-
end
|
133
|
-
|
134
|
-
def remove_from_index
|
135
|
-
self.index = index.tap{ |index| index.delete(self.id) }
|
136
|
-
end
|
137
|
-
|
138
|
-
def validate_uniqueness_of_id
|
139
|
-
self.errors.add(:id, :taken) if indexed?
|
140
|
-
end
|
141
|
-
|
142
|
-
def cache
|
143
|
-
Mor.cache
|
144
|
-
end
|
145
|
-
|
146
|
-
|
147
|
-
end
|
data/lib/mor/client.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
require 'erb'
|
3
|
-
require 'dalli'
|
4
|
-
|
5
|
-
module Mor
|
6
|
-
module Client
|
7
|
-
|
8
|
-
def cache
|
9
|
-
@@cache ||= begin
|
10
|
-
config = YAML::load(ERB.new(File.read("config/cache.yml")).result)[Mor.env]
|
11
|
-
@instance = Dalli::Client.new config.delete("servers").split(","), config
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
end
|