silueta 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +21 -8
- data/lib/silueta/version.rb +1 -1
- data/lib/silueta.rb +50 -2
- data/test/silueta_test.rb +32 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 861f624f086c94b01e49297657e796b2915d9bc3
|
4
|
+
data.tar.gz: f507a9d070094296b797ba3da57470e4db9049b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80870d7eb943d9217eed3c5b1662abcbfdf229be155b8eb6137573b33c0d83e44462a7a780689e776b8d3ca4c218d14bec0e9520b4aaa501ae0344f4215765e6
|
7
|
+
data.tar.gz: ea7079586d196fd058e7c881912a7f1c6d8e2efc2751a4438e38df2f06752bb31e463c467c45ebcaea1daf4776737be2f12340d915304a54c417a86a83f77701
|
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Initialize an object with a hash of attributes.
|
4
4
|
|
5
|
+
Inspired by [Ohm's](https://github.com/soveran/ohm) attribute system.
|
6
|
+
|
5
7
|
## Usage
|
6
8
|
|
7
9
|
```ruby
|
@@ -10,20 +12,31 @@ require "silueta"
|
|
10
12
|
class User
|
11
13
|
include Silueta
|
12
14
|
|
13
|
-
|
15
|
+
attribute :first_name
|
16
|
+
attribute :last_name
|
17
|
+
attribute :email
|
18
|
+
attribute :age, cast: ->(x) { x && x.to_i }
|
14
19
|
end
|
15
20
|
|
16
21
|
user = User.new(
|
17
|
-
first_name: "
|
18
|
-
last_name: "
|
19
|
-
email: "
|
22
|
+
first_name: "Jane",
|
23
|
+
last_name: "Doe",
|
24
|
+
email: "jane@mail.com"
|
20
25
|
)
|
21
26
|
|
22
|
-
|
23
|
-
|
24
|
-
|
27
|
+
user.first_name # => "Jane"
|
28
|
+
user.last_name # => "Doe"
|
29
|
+
user.email # => "jane@mail.com"
|
30
|
+
|
31
|
+
user.age = "25"
|
32
|
+
user.age # => 25
|
33
|
+
|
34
|
+
user.attributes
|
35
|
+
# => {:first_name=>"Jane", :last_name=>"Doe", :email=>"jane@mail.com", :age=>25 }
|
25
36
|
```
|
26
37
|
|
27
38
|
## Installation
|
28
39
|
|
29
|
-
|
40
|
+
```
|
41
|
+
$ gem install silueta
|
42
|
+
```
|
data/lib/silueta/version.rb
CHANGED
data/lib/silueta.rb
CHANGED
@@ -1,9 +1,57 @@
|
|
1
1
|
require_relative "silueta/version"
|
2
2
|
|
3
3
|
module Silueta
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def inherited(subclass)
|
10
|
+
subclass.attributes.replace(attributes)
|
11
|
+
end
|
12
|
+
|
13
|
+
def attribute(attr, cast: nil)
|
14
|
+
attributes << attr
|
15
|
+
|
16
|
+
if cast
|
17
|
+
define_method(attr) do
|
18
|
+
return cast.call(@attributes[attr])
|
19
|
+
end
|
20
|
+
else
|
21
|
+
define_method(attr) do
|
22
|
+
return @attributes[attr]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
define_method(:"#{attr}=") do |value|
|
27
|
+
@attributes[attr] = value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def attributes
|
32
|
+
return @attributes ||= []
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
4
36
|
def initialize(attrs = {})
|
5
|
-
|
6
|
-
|
37
|
+
@attributes = {}
|
38
|
+
|
39
|
+
update(attrs)
|
40
|
+
end
|
41
|
+
|
42
|
+
def update(attrs)
|
43
|
+
attrs.each do |attr, value|
|
44
|
+
public_send(:"#{ attr }=", value)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def attributes
|
49
|
+
return slice(*self.class.attributes)
|
50
|
+
end
|
51
|
+
|
52
|
+
def slice(*attrs)
|
53
|
+
return attrs.each_with_object({}) do |att, hash|
|
54
|
+
hash[att] = send(att)
|
7
55
|
end
|
8
56
|
end
|
9
57
|
end
|
data/test/silueta_test.rb
CHANGED
@@ -5,14 +5,40 @@ require_relative "../lib/silueta"
|
|
5
5
|
class User
|
6
6
|
include Silueta
|
7
7
|
|
8
|
-
|
8
|
+
attribute :name
|
9
|
+
attribute :email
|
10
|
+
attribute :age, cast: ->(x) { x && x.to_i }
|
9
11
|
end
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
user = User.new(name: "Mayn", email: "mayn@mail.com")
|
13
|
+
test "initialize" do
|
14
|
+
user = User.new(name: "Jane", email: "jane@mail.com")
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
assert_equal("Jane", user.name)
|
17
|
+
assert_equal("jane@mail.com", user.email)
|
18
|
+
end
|
19
|
+
|
20
|
+
test "attributes" do
|
21
|
+
attributes = { name: "Jane", email: "jane@mail.com", age: nil }
|
22
|
+
user = User.new(attributes)
|
23
|
+
|
24
|
+
assert_equal(attributes, user.attributes)
|
25
|
+
end
|
26
|
+
|
27
|
+
test "class attributes" do
|
28
|
+
assert_equal(%i(name email age), User.attributes)
|
29
|
+
end
|
30
|
+
|
31
|
+
test "casting" do
|
32
|
+
user = User.new(name: "Jane", email: "jane@mail.com", age: "25")
|
33
|
+
|
34
|
+
assert user.age.kind_of?(Integer)
|
35
|
+
assert_equal(25, user.age)
|
36
|
+
end
|
37
|
+
|
38
|
+
test "inheritance" do
|
39
|
+
class Admin < User
|
40
|
+
attribute :superpowers
|
17
41
|
end
|
42
|
+
|
43
|
+
assert_equal(User.attributes + [:superpowers], Admin.attributes)
|
18
44
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: silueta
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mayn Ektvedt Kjær
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|