cero 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/cero.gemspec +24 -0
- data/lib/cero/adapter.rb +55 -0
- data/lib/cero/adapters/abstract_adapter.rb +57 -0
- data/lib/cero/adapters/memory_adapter.rb +67 -0
- data/lib/cero/adapters/sql_adapter.rb +13 -0
- data/lib/cero/deps.rb +4 -0
- data/lib/cero/entity.rb +32 -0
- data/lib/cero/errors.rb +6 -0
- data/lib/cero/repo/config.rb +8 -0
- data/lib/cero/repo.rb +89 -0
- data/lib/cero/version.rb +13 -0
- data/lib/cero.rb +16 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bf7b0ab42440cec12df54c647510bdfe07934a81
|
4
|
+
data.tar.gz: 694de73764dba04bd1c15ef606e6a69d27130de9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bb52c2f45c1bb29a0d9e5df1feb783d4df50e818b0f94ec2b795fe87e22f9e50542714485feb407f23af50efea488e3dfed4c80da7329b995053e1a19065124b
|
7
|
+
data.tar.gz: 162eb4ae53464c4bd247beef4a744c76856d115da73095b137e7585278d7995ba9756ac6558bf7cb8ebe1f3f69007abd2654652f158e56065e10bd4e442d91fe
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
*.sw[op]
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 John Faucett
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Cero
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'cero'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install cero
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/cero/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/cero.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
$:.unshift File.expand_path('../lib', __FILE__)
|
3
|
+
|
4
|
+
require 'cero/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cero"
|
8
|
+
spec.version = Cero::VERSION::STRING
|
9
|
+
spec.authors = ['John Faucett']
|
10
|
+
spec.email = ['jwaterfaucett@gmail.com']
|
11
|
+
spec.summary = 'Application Building Blocks'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
20
|
+
spec.add_development_dependency "rake"
|
21
|
+
|
22
|
+
spec.add_dependency 'activesupport', '~> 4.1.4'
|
23
|
+
spec.add_dependency 'virtus', '~> 1.0.3'
|
24
|
+
end
|
data/lib/cero/adapter.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module Cero
|
2
|
+
class Adapter
|
3
|
+
DEFAULT_OPTIONS = {
|
4
|
+
type: :memory
|
5
|
+
}
|
6
|
+
|
7
|
+
cattr_reader :types
|
8
|
+
@@types = [:memory, :sql, :key_value, :document].freeze
|
9
|
+
|
10
|
+
cattr_reader :backends
|
11
|
+
@@backends = {
|
12
|
+
memory: [],
|
13
|
+
sql: [:sequel, :active_record],
|
14
|
+
key_value: [],
|
15
|
+
document: []
|
16
|
+
}.freeze
|
17
|
+
|
18
|
+
attr_reader :type, :backend, :proxy
|
19
|
+
|
20
|
+
def initialize(options = {})
|
21
|
+
options = DEFAULT_OPTIONS.merge(options)
|
22
|
+
configure_options(options)
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def resolve_for(collection_name)
|
27
|
+
proxy_klass = get_backend_adapter
|
28
|
+
@proxy = Cero::Adapters.qualified_const_get(proxy_klass).new(collection_name)
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def configure_options(options = {})
|
34
|
+
t, b = options[:type], options[:backend]
|
35
|
+
raise ArgumentError.new("adapter #{t} is not available") unless self.class.types.include?(t)
|
36
|
+
available = self.class.backends[t]
|
37
|
+
if available.empty?
|
38
|
+
b = :none
|
39
|
+
else
|
40
|
+
b = b.nil? ? available.first : b
|
41
|
+
raise ArgumentError.new("backend #{b} is not available") unless available.include?(b)
|
42
|
+
end
|
43
|
+
@type, @backend = t, b
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_backend_adapter
|
47
|
+
if backend.eql?(:none)
|
48
|
+
"#{type}_adapter".classify
|
49
|
+
else
|
50
|
+
"#{type}_adapter/#{backend}".classify
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Cero
|
2
|
+
module Adapters
|
3
|
+
|
4
|
+
|
5
|
+
class AbstractAdapter
|
6
|
+
|
7
|
+
def initialize(collection = nil)
|
8
|
+
@collection = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def create(collection, entity)
|
12
|
+
raise Errors::AbstractMethodError
|
13
|
+
end
|
14
|
+
|
15
|
+
def find(collection, id)
|
16
|
+
raise Errors::AbstractMethodError
|
17
|
+
end
|
18
|
+
|
19
|
+
def update(collection, entity)
|
20
|
+
raise Errors::AbstractMethodError
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete(collection, entity)
|
24
|
+
raise Errors::AbstractMethodError
|
25
|
+
end
|
26
|
+
|
27
|
+
def save(collection, entity)
|
28
|
+
raise Errors::AbstractMethodError
|
29
|
+
end
|
30
|
+
|
31
|
+
def all(collection)
|
32
|
+
raise Errors::AbstractMethodError
|
33
|
+
end
|
34
|
+
|
35
|
+
def first(collection)
|
36
|
+
raise Errors::AbstractMethodError
|
37
|
+
end
|
38
|
+
|
39
|
+
def last(collection)
|
40
|
+
raise Errors::AbstractMethodError
|
41
|
+
end
|
42
|
+
|
43
|
+
def count(collection)
|
44
|
+
raise Errors::AbstractMethodError
|
45
|
+
end
|
46
|
+
|
47
|
+
def query(collection, &block)
|
48
|
+
raise Errors::AbstractMethodError
|
49
|
+
end
|
50
|
+
|
51
|
+
def clear(collection)
|
52
|
+
raise Errors::AbstractMethodError
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Cero
|
2
|
+
module Adapters
|
3
|
+
class MemoryAdapter < AbstractAdapter
|
4
|
+
|
5
|
+
def initialize(collection = nil)
|
6
|
+
super
|
7
|
+
@lock = Monitor.new
|
8
|
+
@collection[collection] = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def create(collection, entity)
|
12
|
+
next_id = next_id_for(@collection[collection])
|
13
|
+
entity.id = next_id
|
14
|
+
@collection[collection][next_id] = entity
|
15
|
+
end
|
16
|
+
|
17
|
+
def find(collection, id)
|
18
|
+
@collection[collection].fetch(id) { nil }
|
19
|
+
end
|
20
|
+
|
21
|
+
def update(collection, entity)
|
22
|
+
@collection[collection][entity.id] = entity
|
23
|
+
end
|
24
|
+
|
25
|
+
def delete(collection, entity)
|
26
|
+
@collection[collection].delete(entity.id)
|
27
|
+
end
|
28
|
+
|
29
|
+
def save(collection, entity)
|
30
|
+
if entity.id
|
31
|
+
update(collection, entity)
|
32
|
+
else
|
33
|
+
create(collection, entity)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def all(collection)
|
38
|
+
@collection[collection].values
|
39
|
+
end
|
40
|
+
|
41
|
+
def first(collection)
|
42
|
+
@collection[collection].values.first
|
43
|
+
end
|
44
|
+
|
45
|
+
def last(collection)
|
46
|
+
@collection[collection].values.last
|
47
|
+
end
|
48
|
+
|
49
|
+
def count(collection)
|
50
|
+
@collection[collection].size
|
51
|
+
end
|
52
|
+
|
53
|
+
def query(collection, &block)
|
54
|
+
yield @collection[collection].values
|
55
|
+
end
|
56
|
+
|
57
|
+
def clear(collection)
|
58
|
+
@collection[collection] = {}
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def next_id_for(hash)
|
63
|
+
hash.empty? ? 1 : hash.keys.count + 1
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/cero/deps.rb
ADDED
data/lib/cero/entity.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
|
3
|
+
module Cero
|
4
|
+
module Entity
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
base.__send__(:attr_accessor, :id)
|
9
|
+
end
|
10
|
+
|
11
|
+
def ==(other)
|
12
|
+
self.class == other.class && self.id == other.id && !self.id.nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(attributes = {})
|
16
|
+
attributes.each do |k, v|
|
17
|
+
public_send("#{k}=", v)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
|
23
|
+
def attributes(*attr_list)
|
24
|
+
return @attributes if attr_list.empty?
|
25
|
+
|
26
|
+
@attributes = attr_list.reject { |k| k == :id }
|
27
|
+
attr_accessor *@attributes
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/cero/errors.rb
ADDED
data/lib/cero/repo.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
module Cero
|
2
|
+
|
3
|
+
#
|
4
|
+
# examples:
|
5
|
+
#
|
6
|
+
# Cero::Repo.configure do |config|
|
7
|
+
# config.adapter
|
8
|
+
# config.adapter = :active_record # => [:sequel]
|
9
|
+
# config.connection_uri = 'sqlite:/db/development.sqlite3'
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# class UserRepo
|
13
|
+
# include Cero::Repo
|
14
|
+
#
|
15
|
+
# adapter type: :sql, backend: :active_record
|
16
|
+
# entity_class = 'User'
|
17
|
+
# collection = :users
|
18
|
+
#
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# UserRepo.find(1) # => <User @id=45 @name="John">
|
22
|
+
#
|
23
|
+
module Repo
|
24
|
+
|
25
|
+
require 'cero/repo/config'
|
26
|
+
|
27
|
+
def self.included(base)
|
28
|
+
base.extend(ClassMethods)
|
29
|
+
end
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
|
33
|
+
def adapter(options = {})
|
34
|
+
@adapter = Adapter.new(options)
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_adapter
|
38
|
+
@adapter ||= Repo.adapter
|
39
|
+
end
|
40
|
+
|
41
|
+
def collection=(collection_name)
|
42
|
+
@collection = collection_name.to_s
|
43
|
+
end
|
44
|
+
|
45
|
+
def collection
|
46
|
+
@collection ||= begin
|
47
|
+
klass_name = extract_default_class_name
|
48
|
+
collection_name = if klass_name.index('::')
|
49
|
+
parts = klass_name.split('::')
|
50
|
+
parts.join.underscore.pluralize
|
51
|
+
else
|
52
|
+
klass_name.downcase.pluralize
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def entity_class=(klass_name)
|
58
|
+
@entity_class = klass_name.to_s.classify.constantize
|
59
|
+
end
|
60
|
+
|
61
|
+
def entity_class
|
62
|
+
@entity_class ||= begin
|
63
|
+
klass_name = extract_default_class_name.constantize
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def method_missing(name, *args, &block)
|
68
|
+
if target.respond_to?(name)
|
69
|
+
args = args.unshift(collection)
|
70
|
+
target.__send__(name, *args, &block)
|
71
|
+
else
|
72
|
+
super(name, *args, &block)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def target
|
77
|
+
@target ||= begin
|
78
|
+
t = get_adapter.resolve_for(collection)
|
79
|
+
t.proxy
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
def extract_default_class_name
|
85
|
+
klass_name = self.name.gsub(/Repo$/, '')
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/lib/cero/version.rb
ADDED
data/lib/cero.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'cero/deps'
|
2
|
+
|
3
|
+
module Cero
|
4
|
+
|
5
|
+
autoload :Errors, 'cero/errors'
|
6
|
+
autoload :Entity, 'cero/entity'
|
7
|
+
autoload :Repo, 'cero/repo'
|
8
|
+
autoload :Adapter, 'cero/adapter'
|
9
|
+
|
10
|
+
module Adapters
|
11
|
+
autoload :AbstractAdapter, 'cero/adapters/abstract_adapter'
|
12
|
+
autoload :MemoryAdapter, 'cero/adapters/memory_adapter'
|
13
|
+
autoload :SqlAdapter, 'cero/adapters/sql_adapter'
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cero
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Faucett
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.1.4
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 4.1.4
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: virtus
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.0.3
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.0.3
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- jwaterfaucett@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- cero.gemspec
|
82
|
+
- lib/cero.rb
|
83
|
+
- lib/cero/adapter.rb
|
84
|
+
- lib/cero/adapters/abstract_adapter.rb
|
85
|
+
- lib/cero/adapters/memory_adapter.rb
|
86
|
+
- lib/cero/adapters/sql_adapter.rb
|
87
|
+
- lib/cero/deps.rb
|
88
|
+
- lib/cero/entity.rb
|
89
|
+
- lib/cero/errors.rb
|
90
|
+
- lib/cero/repo.rb
|
91
|
+
- lib/cero/repo/config.rb
|
92
|
+
- lib/cero/version.rb
|
93
|
+
homepage:
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.4.1
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Application Building Blocks
|
117
|
+
test_files: []
|
118
|
+
has_rdoc:
|