structure 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +9 -0
- data/README.md +55 -48
- data/lib/structure.rb +2 -2
- data/lib/structure/version.rb +1 -1
- data/spec/models/person.rb +1 -1
- data/spec/structure_spec.rb +1 -1
- data/structure.gemspec +2 -3
- metadata +5 -7
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,9 @@
|
|
1
1
|
Structure
|
2
2
|
=========
|
3
3
|
|
4
|
-
Structure is a
|
5
|
-
Ruby.
|
4
|
+
Structure is a key/value container for modeling ephemeral data in Ruby.
|
6
5
|
|
7
|
-
Structure typecasts,
|
8
|
-
JSON seamlessly, even when nested in structures or other structure-like
|
9
|
-
objects.
|
6
|
+
Structure typecasts, nests other structures, and talks flawless JSON.
|
10
7
|
|
11
8
|
#_ d
|
12
9
|
##_ d#
|
@@ -34,73 +31,83 @@ objects.
|
|
34
31
|
Usage
|
35
32
|
-----
|
36
33
|
|
37
|
-
Require:
|
38
|
-
|
39
|
-
require 'structure'
|
40
|
-
|
41
34
|
Define a model:
|
42
35
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
36
|
+
```ruby
|
37
|
+
require 'structure'
|
38
|
+
|
39
|
+
class Person < Structure
|
40
|
+
key :name
|
41
|
+
key :age, :type => Integer
|
42
|
+
has_many :friends
|
43
|
+
has_one :partner
|
44
|
+
end
|
45
|
+
```
|
49
46
|
|
50
47
|
Conjure an object:
|
51
48
|
|
52
|
-
|
49
|
+
```ruby
|
50
|
+
p1 = Person.new :name => 'Gilles'
|
51
|
+
```
|
53
52
|
|
54
53
|
Typecast:
|
55
54
|
|
56
|
-
|
57
|
-
|
58
|
-
|
55
|
+
```ruby
|
56
|
+
p1.age = '28'
|
57
|
+
p1.age
|
58
|
+
=> 28
|
59
|
+
```
|
59
60
|
|
60
61
|
Check for presence:
|
61
|
-
p1.age?
|
62
|
-
=> true
|
63
62
|
|
64
|
-
|
63
|
+
```ruby
|
64
|
+
p1.age?
|
65
|
+
=> true
|
66
|
+
```
|
65
67
|
|
66
|
-
p2 = Person.new
|
67
|
-
p1.friends << p2
|
68
68
|
|
69
|
-
|
69
|
+
Embed other structures:
|
70
70
|
|
71
|
-
|
71
|
+
```ruby
|
72
|
+
p2 = Person.new
|
73
|
+
p1.friends << p2
|
74
|
+
```
|
72
75
|
|
73
|
-
|
74
|
-
=> {"json_class":"Person","name":"John","age":28,"friends":[{"json_class":"Person","name":null,"age":null,"friends":[]}],"partner":null}
|
76
|
+
Talk JSON:
|
75
77
|
|
76
|
-
|
78
|
+
```ruby
|
79
|
+
require 'structure/json'
|
77
80
|
|
78
|
-
|
79
|
-
|
80
|
-
=> #<Person:0x0000010107d030 @attributes={:name=>nil, :age=>nil, :friends=>[], :partner=>nil}>
|
81
|
+
json = p1.to_json
|
82
|
+
=> {"json_class":"Person","name":"John","age":28,"friends":[{"json_class":"Person","name":null,"age":null,"friends":[]}],"partner":null}
|
81
83
|
|
82
|
-
|
84
|
+
person = JSON.parse(json)
|
85
|
+
person.friends.first
|
86
|
+
=> #<Person:0x0000010107d030 @attributes={:name=>nil, :age=>nil, :friends=>[], :partner=>nil}>
|
87
|
+
```
|
83
88
|
|
84
|
-
|
89
|
+
Quack Active Model:
|
85
90
|
|
86
|
-
|
87
|
-
|
91
|
+
```ruby
|
92
|
+
require 'active_model'
|
88
93
|
|
89
|
-
|
94
|
+
class Book < Structure
|
95
|
+
include ActiveModel::Validations
|
90
96
|
|
91
|
-
|
92
|
-
end
|
97
|
+
key :title
|
93
98
|
|
94
|
-
|
99
|
+
validates_presence_of :title
|
100
|
+
end
|
95
101
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
102
|
+
book = Book.new
|
103
|
+
book.valid?
|
104
|
+
=> false
|
105
|
+
book.errors
|
106
|
+
=> {:title=>["can't be blank"]}
|
107
|
+
book.title = "Society of the Spectacle"
|
108
|
+
book.valid?
|
109
|
+
=> true
|
110
|
+
```
|
104
111
|
|
105
112
|
Types
|
106
113
|
-----
|
data/lib/structure.rb
CHANGED
@@ -15,12 +15,12 @@ class Structure
|
|
15
15
|
|
16
16
|
# A shortcut to define an attribute that represents an array of other
|
17
17
|
# objects, possibly structures.
|
18
|
-
def
|
18
|
+
def embeds_many(name)
|
19
19
|
key name, :type => Array, :default => []
|
20
20
|
end
|
21
21
|
|
22
22
|
# A shortcut to define an attribute that represents another structure.
|
23
|
-
def
|
23
|
+
def embeds_one(name)
|
24
24
|
key name, :type => Structure
|
25
25
|
end
|
26
26
|
|
data/lib/structure/version.rb
CHANGED
data/spec/models/person.rb
CHANGED
data/spec/structure_spec.rb
CHANGED
data/structure.gemspec
CHANGED
@@ -9,10 +9,9 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.authors = ["Paper Cavalier"]
|
10
10
|
s.email = ["code@papercavalier.com"]
|
11
11
|
s.homepage = "http://rubygems.com/gems/structure"
|
12
|
-
s.summary = "
|
12
|
+
s.summary = "A key/value container for modeling ephemeral data"
|
13
13
|
s.description = <<-END_OF_DESCRIPTION.strip
|
14
|
-
Structure is a
|
15
|
-
in Ruby.
|
14
|
+
Structure is a key/value container for modeling ephemeral data.
|
16
15
|
END_OF_DESCRIPTION
|
17
16
|
|
18
17
|
s.rubyforge_project = "structure"
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 10
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.10.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Paper Cavalier
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-07-05 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -60,9 +60,7 @@ dependencies:
|
|
60
60
|
version: 0.11.6
|
61
61
|
type: :development
|
62
62
|
version_requirements: *id003
|
63
|
-
description:
|
64
|
-
Structure is a Struct-like key/value container for modeling ephemeral data
|
65
|
-
in Ruby.
|
63
|
+
description: Structure is a key/value container for modeling ephemeral data.
|
66
64
|
email:
|
67
65
|
- code@papercavalier.com
|
68
66
|
executables: []
|
@@ -120,7 +118,7 @@ rubyforge_project: structure
|
|
120
118
|
rubygems_version: 1.3.7
|
121
119
|
signing_key:
|
122
120
|
specification_version: 3
|
123
|
-
summary:
|
121
|
+
summary: A key/value container for modeling ephemeral data
|
124
122
|
test_files:
|
125
123
|
- spec/models/book.rb
|
126
124
|
- spec/models/person.rb
|