shoden-contrib 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/HUGS +15 -0
- data/LICENSE +20 -0
- data/README.md +43 -0
- data/Rakefile +8 -0
- data/lib/shoden/contrib.rb +8 -0
- data/lib/shoden/datatypes.rb +45 -0
- data/lib/shoden/default.rb +25 -0
- data/lib/shoden/timestamps.rb +17 -0
- data/shoden-contrib.gemspec +15 -0
- data/test/datatypes_test.rb +80 -0
- data/test/default_test.rb +13 -0
- data/test/helper.rb +7 -0
- data/test/timestamps_test.rb +12 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9e37b49868c536a8d8503d2a5683954c7516484a
|
4
|
+
data.tar.gz: ef5ee94200206d675fd9e5811e5fa8e7def1ba1b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c3f007ac63cb9f092a340151f21c6909168fb5682e26a30f098cf4e64e0ebcf62fda4f6049f3a6b3df1c0d136c8ec62d5110aad74b840584398b039a7c309807
|
7
|
+
data.tar.gz: 63e5c473c65b9cff8abda6d92955ad51bb3c539990d8ca22cb3d910dd6db6e151648f1f32d7541799a9a038a8dc48e2ee20422bc31b7d909fadbd48cd1edaf83
|
data/HUGS
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
THE HUGWARE LICENSE
|
2
|
+
|
3
|
+
LICENSE
|
4
|
+
|
5
|
+
If there is no other license you can do whatever you want with this while you
|
6
|
+
retain the attribution to the author.
|
7
|
+
|
8
|
+
HUGS
|
9
|
+
|
10
|
+
The author spent time to make this software so please show some gratitude,
|
11
|
+
in any
|
12
|
+
form. A hug, a tweet, a beer on a conference or just a plain old email. Your
|
13
|
+
choice.
|
14
|
+
|
15
|
+
Less hate, more hugs.
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Bruno Aguirre
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Shoden Contrib
|
2
|
+
|
3
|
+
![](http://www.lashwallpapers.com/wp-content/uploads/2014/05/elephant-wallpaper-in-hd-free-download.jpg)
|
4
|
+
|
5
|
+
shoden-contrib adds some sugar to play with Shoden
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
```bash
|
10
|
+
gem install shoden-contrib
|
11
|
+
```
|
12
|
+
|
13
|
+
## Data Types
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
class Person < Shoden::Model
|
17
|
+
include Shoden::DataTypes
|
18
|
+
|
19
|
+
attribute :age, Type::Integer
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
## Default
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Person < Shoden::Model
|
27
|
+
include Shoden::Default
|
28
|
+
|
29
|
+
attribute :name
|
30
|
+
default :name, 'John Doe'
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
## Timestamps
|
35
|
+
```ruby
|
36
|
+
class Person < Shoden::Model
|
37
|
+
include Shoden::Timestamps
|
38
|
+
end
|
39
|
+
|
40
|
+
person = Person.create
|
41
|
+
person.created_at
|
42
|
+
person.updated_at
|
43
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Shoden
|
5
|
+
module DataTypes
|
6
|
+
module Type
|
7
|
+
# Copy and paste from cyx/ohm-contrib <3 <3 <3
|
8
|
+
#
|
9
|
+
Integer = ->(x) { x.to_i }
|
10
|
+
Decimal = ->(x) { BigDecimal(x.to_s) }
|
11
|
+
Float = ->(x) { x.to_f }
|
12
|
+
Symbol = ->(x) { x && x.to_sym }
|
13
|
+
Time = ->(t) { t && (t.kind_of?(::Time) ? t : ::Time.parse(t)) }
|
14
|
+
Date = ->(d) { d && (d.kind_of?(::Date) ? d : ::Date.parse(d)) }
|
15
|
+
Timestamp = ->(t) { t && UnixTime.at(t.to_i) }
|
16
|
+
Hash = ->(h) { h && SerializedHash[h.kind_of?(::Hash) ? h : JSON(h)] }
|
17
|
+
Array = ->(a) { a && SerializedArray.new(a.kind_of?(::Array) ? a : JSON(a)) }
|
18
|
+
Set = ->(s) { s && SerializedSet.new(s.kind_of?(::Set) ? s : JSON(s)) }
|
19
|
+
end
|
20
|
+
|
21
|
+
class UnixTime < Time
|
22
|
+
def to_s
|
23
|
+
to_i.to_s
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class SerializedHash < Hash
|
28
|
+
def to_s
|
29
|
+
JSON.dump(self)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class SerializedArray < Array
|
34
|
+
def to_s
|
35
|
+
JSON.dump(self)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class SerializedSet < ::Set
|
40
|
+
def to_s
|
41
|
+
JSON.dump(to_a.sort)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Shoden
|
2
|
+
module Default
|
3
|
+
def self.included(model)
|
4
|
+
model.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def default_attributes
|
9
|
+
@default_attributes ||= {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def default(name, value)
|
13
|
+
default_attributes[name] = value if attributes.include?(name)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(*)
|
18
|
+
super
|
19
|
+
|
20
|
+
self.class.default_attributes.each do |k,v|
|
21
|
+
self.send("#{k}=", v) if !self.send(k)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative 'datatypes'
|
2
|
+
|
3
|
+
module Shoden
|
4
|
+
module Timestamps
|
5
|
+
def self.included(model)
|
6
|
+
model.attribute :created_at, DataTypes::Type::Timestamp
|
7
|
+
model.attribute :updated_at, DataTypes::Type::Timestamp
|
8
|
+
end
|
9
|
+
|
10
|
+
def save
|
11
|
+
self.created_at = Time.now.utc.to_i if @_id.nil?
|
12
|
+
self.updated_at = Time.now.utc.to_i
|
13
|
+
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "shoden-contrib"
|
3
|
+
s.version = "0.1.0"
|
4
|
+
s.summary = "Contrib helpers for shoden"
|
5
|
+
s.description = "Makes your life easier"
|
6
|
+
s.authors = ["elcuervo"]
|
7
|
+
s.licenses = ["MIT", "HUGWARE"]
|
8
|
+
s.email = ["yo@brunoaguirre.com"]
|
9
|
+
s.homepage = "http://github.com/elcuervo/shoden-contrib"
|
10
|
+
s.files = `git ls-files`.split("\n")
|
11
|
+
s.test_files = `git ls-files test`.split("\n")
|
12
|
+
|
13
|
+
s.add_dependency("shoden", "~> 0.2.0")
|
14
|
+
s.add_development_dependency("cutest", "~> 1.2.1")
|
15
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class Omni < Shoden::Model
|
4
|
+
include Shoden::DataTypes
|
5
|
+
|
6
|
+
attribute :integer, Type::Integer
|
7
|
+
attribute :decimal, Type::Decimal
|
8
|
+
attribute :float, Type::Float
|
9
|
+
attribute :symbol, Type::Symbol
|
10
|
+
attribute :time, Type::Time
|
11
|
+
attribute :date, Type::Date
|
12
|
+
attribute :timestamp, Type::Timestamp
|
13
|
+
attribute :dict, Type::Hash
|
14
|
+
attribute :array, Type::Array
|
15
|
+
attribute :set, Type::Set
|
16
|
+
end
|
17
|
+
|
18
|
+
test 'Type::Integer' do
|
19
|
+
id = Omni.create(integer: 1).id
|
20
|
+
assert_equal Omni[id].integer, 1
|
21
|
+
end
|
22
|
+
|
23
|
+
test 'Type::Float' do
|
24
|
+
id = Omni.create(float: 1.0).id
|
25
|
+
assert_equal Omni[id].float, 1.0
|
26
|
+
end
|
27
|
+
|
28
|
+
test 'Type::Hash' do
|
29
|
+
hash = { a: 1, b: 2, c: 3 }
|
30
|
+
omni = Omni.create(dict: hash)
|
31
|
+
|
32
|
+
assert omni.dict.kind_of?(Hash)
|
33
|
+
|
34
|
+
assert Omni[omni.id].dict.kind_of?(Hash)
|
35
|
+
assert hash == omni.dict
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'Type::Symbol' do
|
39
|
+
omni = Omni.create(symbol: :hi)
|
40
|
+
|
41
|
+
assert omni.symbol.kind_of?(Symbol)
|
42
|
+
|
43
|
+
assert_equal Omni[omni.id].symbol, :hi
|
44
|
+
end
|
45
|
+
|
46
|
+
test 'Type::Time' do
|
47
|
+
time = Time.utc(2011, 11, 22, 22, 10, 23)
|
48
|
+
omni = Omni.create(time: time)
|
49
|
+
|
50
|
+
assert omni.time.kind_of?(Time)
|
51
|
+
|
52
|
+
assert_equal Omni[omni.id].time, time
|
53
|
+
end
|
54
|
+
|
55
|
+
test 'Type::Date' do
|
56
|
+
date = Date.today
|
57
|
+
omni = Omni.create(date: date)
|
58
|
+
|
59
|
+
assert omni.date.kind_of?(Date)
|
60
|
+
|
61
|
+
assert_equal Omni[omni.id].date, date
|
62
|
+
end
|
63
|
+
|
64
|
+
test 'Type::Array' do
|
65
|
+
array = [1, 2, 3]
|
66
|
+
omni = Omni.create(array: array)
|
67
|
+
|
68
|
+
assert omni.array.kind_of?(Array)
|
69
|
+
|
70
|
+
assert_equal Omni[omni.id].array, array
|
71
|
+
end
|
72
|
+
|
73
|
+
test 'Type::Set' do
|
74
|
+
set = Set.new([1,2,3])
|
75
|
+
omni = Omni.create(set: set)
|
76
|
+
|
77
|
+
assert omni.set.kind_of?(Set)
|
78
|
+
|
79
|
+
assert_equal Omni[omni.id].set, set
|
80
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shoden-contrib
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- elcuervo
|
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: shoden
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cutest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.1
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.2.1
|
41
|
+
description: Makes your life easier
|
42
|
+
email:
|
43
|
+
- yo@brunoaguirre.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- HUGS
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- lib/shoden/contrib.rb
|
53
|
+
- lib/shoden/datatypes.rb
|
54
|
+
- lib/shoden/default.rb
|
55
|
+
- lib/shoden/timestamps.rb
|
56
|
+
- shoden-contrib.gemspec
|
57
|
+
- test/datatypes_test.rb
|
58
|
+
- test/default_test.rb
|
59
|
+
- test/helper.rb
|
60
|
+
- test/timestamps_test.rb
|
61
|
+
homepage: http://github.com/elcuervo/shoden-contrib
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
- HUGWARE
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.0.14
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Contrib helpers for shoden
|
86
|
+
test_files:
|
87
|
+
- test/datatypes_test.rb
|
88
|
+
- test/default_test.rb
|
89
|
+
- test/helper.rb
|
90
|
+
- test/timestamps_test.rb
|
91
|
+
has_rdoc:
|