cudify 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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWRmMTlhZGUzMDQ1MGRjNDRkYWM3YmUyOTllYWYzMDU3MjkyZjk5MA==
5
+ data.tar.gz: !binary |-
6
+ N2FiZjU0NDViN2ZlODBhOWVjMjU3YmE0NzFmYzg2ODg3ZGNlNjMxMQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YjAxZDViNTZkYTFlZjg3MzIyYzQ4MWRlZDBiYmViMGY0NWExZDUyZGEyNGUx
10
+ YjZmYjg1NmYzMzYzM2M3YzE0OWNhYjU4ZTg4MjE3MWY3MzlhY2VkMDEyNDll
11
+ YTk1Y2U3Nzg5OTRjMjI4NWFmMDQ4ZjBjODkzMDYwYmM3NWQ1ZGM=
12
+ data.tar.gz: !binary |-
13
+ MDhjMDk2YTVkYTFlNGFlYTJlMjg4ZDZkOTNmZTNhZTllMWYzMDJjZGVkOWE5
14
+ ZWQwZmRmYTdiZDc1ZjA5NGRmYTZkYmI1ZDQ0YzYwMmExNGE2ZjQxMGJkZDg2
15
+ YjQ2OTI0MGVlY2ZhN2JjOTI0NjA1ZDc5MzM1OGY1ZWIyZTcwOTU=
data/.gitignore ADDED
@@ -0,0 +1,20 @@
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
+
19
+ *.swp
20
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cudify.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TsaiKoga
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,63 @@
1
+ # Cudify
2
+
3
+ It's convinient for you.
4
+
5
+ This gem can help you quickly create, update or delete the records.
6
+
7
+ And it also supports accept_nested_attributes, for examples.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'cudify'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cudify
22
+
23
+ ## Usage
24
+ Firstly, it must add require cudify to your enviroment(in config/environment.rb):
25
+
26
+ require "cudify"
27
+
28
+ This gem can help you quickly create, update or delete the records(you can try it by console), if you follow the rule below:
29
+
30
+ ###CREATE RECORDS:
31
+ You just use the ActiveRecord method cudify or cudify!, the parameters is like the original records
32
+ example:
33
+
34
+ User.cudify!(name: "TsaiKoga", sex: "man", age: 23)
35
+
36
+ ###DESTROY RECORDS:
37
+ If you want to destroy records, the first thing is you need to know their ids, and set hash like them below:
38
+ example:
39
+
40
+ User.cudify!(id:1, name:"TsaiKoga", _destroy: 1)
41
+ User.cudify!([{id:1, name:"TsaiKoga", _destroy: 1}, {id:2, _destroy: 1}])
42
+
43
+ Remember: the symbol _destroy is important!
44
+
45
+ ###UPDATE RECORDS:
46
+ If you want to update records, you must know their ids and put them into hash.
47
+ example:
48
+
49
+ User.cudify!(name: "CKJ", sex: "man")
50
+
51
+ It also supports accept_nested_attributes, for examples:( user has many appliances )
52
+
53
+ ###CREATE RECORDS WITH ACCEPT NESTED ATTRIBUTES:
54
+
55
+ User.cudify!({name: "TsaiKoga", sex: "man", age: 23, appliances_attributes: [{name: "labtop", price: 4300.0}, {name: "pen", price: 5}]})
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/cudify.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cudify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cudify"
8
+ spec.version = Cudify::VERSION
9
+ spec.authors = ["TsaiKoga"]
10
+ spec.email = ["969024089@qq.com"]
11
+ spec.description = %q{
12
+ It's convinient for you to quickly create, update or delete the records.
13
+ And it also supports accept_nested_attributes.
14
+ }
15
+ spec.summary = %q{This gem is used to create, delete or update records.}
16
+ spec.homepage = "http://github.com/tsaikoga/cudify"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files`.split($/)
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
data/lib/cudify.rb ADDED
@@ -0,0 +1,50 @@
1
+ require "cudify/version"
2
+
3
+ # User: TsaiKoga
4
+ # Params: rec
5
+ # For example:
6
+ # { :id => 1, ..., :_destroy => 1 } this record will be delete
7
+ # { :id => 2, ..., :_destroy => 0 } || { :id => 2, ... } these records will be updated
8
+ # { :id => nil, ... } || { name: "TsaiKoga", sex: "man", ... } these records will be created
9
+ module Cudify
10
+
11
+ def cudify! rec
12
+ define_cudify :cudify!, rec
13
+ end
14
+
15
+ def cudify rec
16
+ define_cudify :cudify, rec
17
+ end
18
+
19
+ def define_cudify(name, rec)
20
+ # At first, parameters hash become array
21
+ array = (rec.is_a?(Array) ? rec : [rec])
22
+ records = []
23
+
24
+ # Secondly, records are created, updated or deleted
25
+ transaction do
26
+ array.each do |hash|
27
+ if hash[:id] == nil then
28
+ make = ( name == :cudify ? :create : :create! )
29
+ hash[:_destroy].to_i == 1 ? next : (record = send(make,hash))
30
+ records.push(record)
31
+ else
32
+ if hash[:_destroy].to_i == 1 then
33
+ del = ( name == :cudify ? :delete : :destroy )
34
+ find(hash[:id]).nil? ? raise : find(hash[:id]).send(:destroy)
35
+ else
36
+ hash = hash.slice!(:_destroy)
37
+ find(hash[:id]).nil? ? raise : find(hash[:id]).update_attributes!(hash)
38
+ records.push(find(hash[:id]))
39
+ end
40
+ end
41
+ end
42
+ end
43
+ # At last, return the records what are created or updated
44
+ return records.compact
45
+ end
46
+ end
47
+
48
+ class ActiveRecord::Base
49
+ extend Cudify
50
+ end
@@ -0,0 +1,3 @@
1
+ module Cudify
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cudify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - TsaiKoga
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-26 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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
+ description: ! "\n\t\tIt's convinient for you to quickly create, update or delete
42
+ the records.\n\t\tAnd it also supports accept_nested_attributes.\n\t"
43
+ email:
44
+ - 969024089@qq.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - cudify.gemspec
55
+ - lib/cudify.rb
56
+ - lib/cudify/version.rb
57
+ homepage: http://github.com/tsaikoga/cudify
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.0.5
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: This gem is used to create, delete or update records.
81
+ test_files: []