super_accessors 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +88 -0
- data/Rakefile +1 -0
- data/lib/super_accessors/datetime.rb +79 -0
- data/lib/super_accessors/store.rb +89 -0
- data/lib/super_accessors/version.rb +3 -0
- data/lib/super_accessors.rb +6 -0
- data/super_accessors.gemspec +23 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2318f0d762516481afbe336d0e7129f9d378c4a9
|
4
|
+
data.tar.gz: b043ba8aa066f31c3741072134dad08c4245767e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b06d5d59767553be77523ab87d54fd08871fbca7c3e88169be1205fa567fa6fa781306599cd9704de1e9fb52b620dbfe0fed25eda3d1ad1cf82a5b079055acad
|
7
|
+
data.tar.gz: edb611c4d3a73cc0b1dbac535b7f668efbc9c6b53ee8f0c5043d87ad5dedbc1541cc8b0b5265b5f53adf7373bb9f1d5e98d6e2942bb05c21158856d3689d2a4e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 eddie
|
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,88 @@
|
|
1
|
+
# SuperAccessors
|
2
|
+
|
3
|
+
Supported split datetime attributes, store hash key datatypes (Integer, String, Boolean)
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'super_accessors'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install super_accessors
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
### Split Date / Time
|
23
|
+
|
24
|
+
```
|
25
|
+
# app/models/user.rb
|
26
|
+
class Ticket < ActiveRecord::Base
|
27
|
+
split_date_hour_min :depart_datetime, default: lambda { Time.now + 1.day }
|
28
|
+
ticket.depart_time_start_date = '2013/10/20'
|
29
|
+
ticket.depart_time_start_time = '20:10'
|
30
|
+
# '2013/10/20 20:10'
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
### Split Date / Hour / Min
|
35
|
+
|
36
|
+
```
|
37
|
+
class Ticket < ActiveRecord::Base
|
38
|
+
split_date_time :depart_time_start, default: lambda { Time.now + 1.day }
|
39
|
+
end
|
40
|
+
|
41
|
+
ticket = Ticket.new
|
42
|
+
ticket.depart_time_start_date = '2013/10/20'
|
43
|
+
ticket.depart_time_start_hr = '10'
|
44
|
+
ticket.depart_time_start_min = '20'
|
45
|
+
# '2013/10/20 10:20'
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
### Store
|
50
|
+
|
51
|
+
```
|
52
|
+
# app/models/user.rb
|
53
|
+
class User < ActiveRecord::Base
|
54
|
+
store_define :contact_info, accessors: {name: :s, zipcode: :i, mobile: :s, available: :b}
|
55
|
+
end
|
56
|
+
|
57
|
+
@user.name = 123
|
58
|
+
"123"
|
59
|
+
@user.zipcode = "123"
|
60
|
+
123
|
61
|
+
@user.available = 1
|
62
|
+
true
|
63
|
+
```
|
64
|
+
|
65
|
+
### Checkboxes with Store
|
66
|
+
```
|
67
|
+
# app/models/user.rb
|
68
|
+
class User < ActiveRecord::Base
|
69
|
+
store_define :role, accessors: {admin: :b, manager: :b, user: :b}, checkboxes: true
|
70
|
+
end
|
71
|
+
|
72
|
+
# show all true option keys
|
73
|
+
@user.role_keys
|
74
|
+
[:admin, :manager]
|
75
|
+
|
76
|
+
# show all true option with i18n
|
77
|
+
@user.role_options
|
78
|
+
['管理員', '使用者']
|
79
|
+
```
|
80
|
+
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
1. Fork it
|
85
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
86
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
87
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
88
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module SuperAccessors
|
2
|
+
module Datetime
|
3
|
+
def split_date_time(*attrs)
|
4
|
+
opts = { format: "%F", default: lambda { Time.now.change(min: 0) } }
|
5
|
+
if attrs.last.class == Hash
|
6
|
+
custom = attrs.delete_at(-1)
|
7
|
+
opts = opts.merge(custom)
|
8
|
+
end
|
9
|
+
|
10
|
+
attrs.each do |attr|
|
11
|
+
define_method(attr) do
|
12
|
+
super() || opts[:default].call
|
13
|
+
end
|
14
|
+
|
15
|
+
define_method("#{attr}_date=") do |date|
|
16
|
+
return unless date.present?
|
17
|
+
date = Date.parse(date.to_s)
|
18
|
+
self.send("#{attr}=", self.send(attr).change(year: date.year, month: date.month, day: date.day))
|
19
|
+
end
|
20
|
+
|
21
|
+
define_method("#{attr}_time=") do |time|
|
22
|
+
return unless time.present
|
23
|
+
time = Date.parse(time.to_s)
|
24
|
+
self.send("#{attr}=", self.send(attr).change(hour: time.hour, min: time.min))
|
25
|
+
end
|
26
|
+
|
27
|
+
define_method("#{attr}_date") do
|
28
|
+
self.send(attr).strftime(opts[:format])
|
29
|
+
end
|
30
|
+
|
31
|
+
define_method("#{attr}_time") do
|
32
|
+
[self.send(attr).hour, self.send(attr).min].join(':')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def split_date_hour_min(*attrs)
|
38
|
+
opts = { format: "%F", default: lambda { Time.now.change(min: 0) } }
|
39
|
+
if attrs.last.class == Hash
|
40
|
+
custom = attrs.delete_at(-1)
|
41
|
+
opts = opts.merge(custom)
|
42
|
+
end
|
43
|
+
|
44
|
+
attrs.each do |attr|
|
45
|
+
define_method(attr) do
|
46
|
+
super() || opts[:default].call
|
47
|
+
end
|
48
|
+
|
49
|
+
define_method("#{attr}_date=") do |date|
|
50
|
+
return unless date.present?
|
51
|
+
date = Date.parse(date.to_s)
|
52
|
+
self.send("#{attr}=", self.send(attr).change(year: date.year, month: date.month, day: date.day))
|
53
|
+
end
|
54
|
+
|
55
|
+
define_method("#{attr}_hr=") do |hour|
|
56
|
+
return unless hour.present?
|
57
|
+
self.send("#{attr}=", self.send(attr).change(hour: hour, min: self.send(attr).min))
|
58
|
+
end
|
59
|
+
|
60
|
+
define_method("#{attr}_min=") do |min|
|
61
|
+
return unless min.present?
|
62
|
+
self.send("#{attr}=", self.send(attr).change(min: min))
|
63
|
+
end
|
64
|
+
|
65
|
+
define_method("#{attr}_date") do
|
66
|
+
self.send(attr).strftime(opts[:format])
|
67
|
+
end
|
68
|
+
|
69
|
+
define_method("#{attr}_hr") do
|
70
|
+
self.send(attr).hour
|
71
|
+
end
|
72
|
+
|
73
|
+
define_method("#{attr}_min") do
|
74
|
+
self.send(attr).min
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module SuperAccessors
|
2
|
+
module Store
|
3
|
+
def self.extended(base)
|
4
|
+
base.class_eval do
|
5
|
+
include Converter
|
6
|
+
end
|
7
|
+
end
|
8
|
+
module Converter
|
9
|
+
# for integer converter
|
10
|
+
def any_to_i(any)
|
11
|
+
any.to_i
|
12
|
+
end
|
13
|
+
# for string converter
|
14
|
+
def any_to_s(any)
|
15
|
+
any.to_s
|
16
|
+
end
|
17
|
+
# for boolean convert
|
18
|
+
def any_to_b(any)
|
19
|
+
str = any.to_s
|
20
|
+
return false if str.blank? || str =~ (/(false|f|no|n|0)$/i)
|
21
|
+
return true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
# This function will instead of store function, now support string, boolean, integer
|
27
|
+
#
|
28
|
+
# Example 1:
|
29
|
+
# store_define :contact_info, {name: :s, tel: :s, mobile: :s, available: :b}
|
30
|
+
#
|
31
|
+
# Example 2 (checkboxes)
|
32
|
+
# store_define :role, accessors: {admin: :b, manager: :b, user: :b}, checkboxes: true
|
33
|
+
#
|
34
|
+
# you have to setup boolean type of all attribute if you are using checkboxes
|
35
|
+
# it provide the function
|
36
|
+
# 1. show all option of true item
|
37
|
+
# 2. show all option of true item with i18n translation
|
38
|
+
def store_define(store_name, option)
|
39
|
+
column_with_datatype = option[:accessors]
|
40
|
+
option[:accessors] = option[:accessors].keys
|
41
|
+
# setting the activerecord store
|
42
|
+
store store_name, option
|
43
|
+
# define the virtual attrs
|
44
|
+
column_with_datatype.each do |attr, type|
|
45
|
+
self.send("store_key_type_#{type.to_s}", store_name, attr)
|
46
|
+
end
|
47
|
+
# setting the checkboxs
|
48
|
+
if option[:checkboxes]
|
49
|
+
store_checkboxes_setup store_name, option[:accessors]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# get the checkboxes true item list & i18n translation
|
54
|
+
def store_checkboxes_setup(store_name, attrs)
|
55
|
+
define_method("#{store_name}_keys") do
|
56
|
+
list = [];
|
57
|
+
attrs.each do |attr|
|
58
|
+
if self.send(attr) == true
|
59
|
+
list.push(attr)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
list
|
63
|
+
end
|
64
|
+
define_method("#{store_name}_options") do
|
65
|
+
@i18n_base_scope = [:activerecord, :options, self.class.name.underscore, store_name]
|
66
|
+
list = [];
|
67
|
+
attrs.each do |attr|
|
68
|
+
if self.send(attr) == true
|
69
|
+
list.push(I18n.t("#{attr}", scope: @i18n_base_scope, :default => attr.to_s.humanize))
|
70
|
+
end
|
71
|
+
end
|
72
|
+
list
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
['i', 's', 'b'].each do |datatype|
|
77
|
+
define_method("store_key_type_#{datatype}") do |store_name, attr|
|
78
|
+
define_method(attr) do
|
79
|
+
value = read_store_attribute(store_name, attr)
|
80
|
+
self.send("any_to_#{datatype}", value)
|
81
|
+
end
|
82
|
+
define_method("#{attr}=") do |value|
|
83
|
+
value = self.send("any_to_#{datatype}", value)
|
84
|
+
write_store_attribute(store_name, attr, value)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
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 'super_accessors/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "super_accessors"
|
8
|
+
spec.version = SuperAccessors::VERSION
|
9
|
+
spec.authors = ["eddie"]
|
10
|
+
spec.email = ["eddie@visionbundles.com"]
|
11
|
+
spec.description = %q{Make activerecord support split datetime attributes, store specific datatypes (Integer, String, Boolean)}
|
12
|
+
spec.summary = %q{activerecord datetime split, store specific datatype (Integer, String, Boolean) }
|
13
|
+
spec.homepage = "https://github.com/afunction/super_accessors"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: super_accessors
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- eddie
|
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: Make activerecord support split datetime attributes, store specific datatypes
|
42
|
+
(Integer, String, Boolean)
|
43
|
+
email:
|
44
|
+
- eddie@visionbundles.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/super_accessors.rb
|
55
|
+
- lib/super_accessors/datetime.rb
|
56
|
+
- lib/super_accessors/store.rb
|
57
|
+
- lib/super_accessors/version.rb
|
58
|
+
- super_accessors.gemspec
|
59
|
+
homepage: https://github.com/afunction/super_accessors
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.6
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: activerecord datetime split, store specific datatype (Integer, String, Boolean)
|
83
|
+
test_files: []
|