resource_manager 0.0.1

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: bbce9166d689ff918807792caae7eacb6c0df155
4
+ data.tar.gz: c67cdeee9ff59d77eac3040cb8944a64327113f4
5
+ SHA512:
6
+ metadata.gz: a5accd7d89bd4506714589ce773c3c2d1dab450f523d1b1175527b81cd43816f89bbe023c3848235c448ad32bc5abb0393034b16a07e58436e219ee1d2d6aede
7
+ data.tar.gz: 1facfcbcd414c1370222f99d1ea4e8c053fa5ad279a530f215b1e24e079884548fb7704747861c641ea3a3f4b8a85411c713af9fa0d55274d82b92a2a9553b37
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in resource_manager.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Gleb Sinyavsky
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Gleb Sinyavsky
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,31 @@
1
+ # ResourceManager
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'resource_manager'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install resource_manager
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/resource_manager/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,7 @@
1
+ module ResourceManager
2
+ module Transactionable
3
+ def transaction(&block)
4
+ ActiveRecord::Base.transaction &block
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module ResourceManager
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,103 @@
1
+ require 'resource_manager/version'
2
+ require 'resource_manager/transactionable'
3
+
4
+ module ResourceManager
5
+ class Base
6
+ include ResourceManager::Transactionable
7
+
8
+ class_attribute :model
9
+ class_attribute :before_create_callback, :after_create_callback, :before_update_callback, :after_update_callback, :before_destroy_callback, :after_destroy_callback
10
+ class_attribute :create_action, :update_action, :destroy_action
11
+
12
+ class << self
13
+ def model(class_or_proc, &block)
14
+ self.model = class_or_proc
15
+ end
16
+
17
+ [:before_create, :after_create, :before_update, :after_update, :before_destroy, :after_destroy].each do |method|
18
+ define_method(method) do |&block|
19
+ self.send("#{method}_callback=", block)
20
+ end
21
+ end
22
+
23
+ [:create, :update, :destroy].each do |method|
24
+ define_method(method) do |&block|
25
+ self.send("#{method}_action=", block)
26
+ end
27
+ end
28
+ end
29
+
30
+ def model
31
+ if self.class.model.kind_of?(Proc)
32
+ instance_exec(&self.class.model)
33
+ else
34
+ self.class.model
35
+ end
36
+ end
37
+
38
+ def create(params)
39
+ transaction do
40
+ run_callback(:before_create, params)
41
+
42
+ result = if self.create_action.present?
43
+ instance_exec(params, &self.create_action)
44
+ else
45
+ item = model.new(params)
46
+
47
+ yield(item, item.save) if block_given?
48
+ end
49
+
50
+ run_callback(:after_create, item) if result
51
+ result
52
+ end
53
+ end
54
+
55
+ def update(item_or_id, params)
56
+ transaction do
57
+ item = item(item_or_id)
58
+ run_callback(:before_update, item, params)
59
+
60
+ result = if self.update_action.present?
61
+ instance_exec(item, params, &self.update_action)
62
+ else
63
+ item.assign_attributes(params)
64
+ changed = item.changed
65
+ yield(item, item.save) if block_given?
66
+ end
67
+
68
+ run_callback(:after_update, item, params, changed) if result
69
+ result
70
+ end
71
+ end
72
+
73
+ def destroy(item_or_id)
74
+ transaction do
75
+ item = item(item_or_id)
76
+ run_callback(:before_destroy, item)
77
+
78
+ if self.destroy_action.present?
79
+ instance_exec(item, &self.destroy_action)
80
+ else
81
+ item.destroy
82
+ end
83
+
84
+ run_callback(:after_destroy, item)
85
+ nil
86
+ end
87
+ end
88
+
89
+ private
90
+
91
+ def run_callback(name, *args)
92
+ self.instance_exec(*args, &self.send("#{name}_callback")) if self.send("#{name}_callback").present?
93
+ end
94
+
95
+ def item(item_or_id)
96
+ if item_or_id.is_a?(model)
97
+ item_or_id
98
+ else
99
+ model.find(item_or_id)
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'resource_manager/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'resource_manager'
8
+ spec.version = ResourceManager::VERSION
9
+ spec.authors = ['Gleb Sinyavsky']
10
+ spec.email = ['zhulik.gleb@gmail.com']
11
+ spec.summary = %q{Base for service objects that encapsulates model actions and callbacks}
12
+ spec.description = %q{Base for service objects that encapsulates model actions and callbacks}
13
+ spec.homepage = 'https://github.com/zhulik/resource_manager'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resource_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gleb Sinyavsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-28 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: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Base for service objects that encapsulates model actions and callbacks
42
+ email:
43
+ - zhulik.gleb@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/resource_manager.rb
55
+ - lib/resource_manager/transactionable.rb
56
+ - lib/resource_manager/version.rb
57
+ - resource_manager.gemspec
58
+ homepage: https://github.com/zhulik/resource_manager
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Base for service objects that encapsulates model actions and callbacks
82
+ test_files: []