structure 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -0
- data/CHANGELOG.md +4 -0
- data/README.md +2 -2
- data/lib/structure.rb +2 -4
- data/lib/structure/json.rb +25 -0
- data/lib/structure/version.rb +1 -1
- data/spec/models/person.rb +0 -1
- data/spec/structure/json_spec.rb +50 -14
- data/spec/structure_spec.rb +0 -1
- data/structure.gemspec +3 -2
- metadata +25 -10
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use 1.9.2@structure
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -5,7 +5,8 @@ Structure is a Struct-like key/value container for modeling ephemeral data in
|
|
5
5
|
Ruby.
|
6
6
|
|
7
7
|
Structure typecasts, uses basic association idioms, and converts to and from
|
8
|
-
JSON seamlessly
|
8
|
+
JSON seamlessly, even when nested in structures or other structure-like
|
9
|
+
objects.
|
9
10
|
|
10
11
|
#_ d
|
11
12
|
##_ d#
|
@@ -109,4 +110,3 @@ Structure supports the following types:
|
|
109
110
|
* Integer
|
110
111
|
* String
|
111
112
|
* Structure
|
112
|
-
* URI
|
data/lib/structure.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'uri'
|
2
|
-
|
3
1
|
# Ruby doesn't have a Boolean class, so let's feign one.
|
4
2
|
unless Object.const_defined?(:Boolean)
|
5
3
|
module Boolean; end
|
@@ -11,7 +9,7 @@ end
|
|
11
9
|
class Structure
|
12
10
|
include Enumerable
|
13
11
|
|
14
|
-
TYPES = [Array, Boolean, Float, Hash, Integer, String, Structure
|
12
|
+
TYPES = [Array, Boolean, Float, Hash, Integer, String, Structure]
|
15
13
|
|
16
14
|
class << self
|
17
15
|
|
@@ -31,7 +29,7 @@ class Structure
|
|
31
29
|
# Takes a name and an optional hash of options. Available options are:
|
32
30
|
#
|
33
31
|
# * :type, which can be Array, Boolean, Float, Hash, Integer, String,
|
34
|
-
#
|
32
|
+
# or Structure. If not specified, type defaults to String.
|
35
33
|
# * :default, which sets the default value for the attribute.
|
36
34
|
#
|
37
35
|
# class Book
|
data/lib/structure/json.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'structure'
|
2
|
+
|
1
3
|
unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
|
2
4
|
::JSON::JSON_LOADED
|
3
5
|
require 'json'
|
@@ -16,3 +18,26 @@ class Structure
|
|
16
18
|
to_json(*args)
|
17
19
|
end
|
18
20
|
end
|
21
|
+
|
22
|
+
if defined? ActiveSupport
|
23
|
+
class Structure
|
24
|
+
def as_json(options = nil)
|
25
|
+
# create a subset of the attributes by applying :only or :except
|
26
|
+
subset = if options
|
27
|
+
if attrs = options[:only]
|
28
|
+
@attributes.slice(*Array.wrap(attrs))
|
29
|
+
elsif attrs = options[:except]
|
30
|
+
@attributes.except(*Array.wrap(attrs))
|
31
|
+
else
|
32
|
+
@attributes.dup
|
33
|
+
end
|
34
|
+
else
|
35
|
+
@attributes.dup
|
36
|
+
end
|
37
|
+
|
38
|
+
klass = self.class.name
|
39
|
+
{ JSON.create_id => klass }.
|
40
|
+
merge(subset)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/structure/version.rb
CHANGED
data/spec/models/person.rb
CHANGED
data/spec/structure/json_spec.rb
CHANGED
@@ -1,30 +1,66 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
shared_examples_for "a JSON interface" do
|
4
|
+
it "dumps to JSON" do
|
5
|
+
person.to_json.should eql json
|
6
|
+
end
|
7
7
|
|
8
|
+
it "loads from JSON" do
|
9
|
+
JSON.parse(json).should == person
|
10
|
+
end
|
11
|
+
|
12
|
+
context "when nesting other structures" do
|
8
13
|
before do
|
14
|
+
person.friends = [Person.new(:name => 'Jane')]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "loads them into their corresponding structures" do
|
18
|
+
json = person.to_json
|
19
|
+
JSON.parse(json).friends.first.should be_a Person
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe Structure do
|
25
|
+
let(:person) { Person.new(:name => 'Joe') }
|
26
|
+
let(:json) { '{"json_class":"Person","name":"Joe","age":null,"friends":[]}' }
|
27
|
+
|
28
|
+
context "without Active Support" do
|
29
|
+
before(:all) do
|
9
30
|
require 'structure/json'
|
10
31
|
end
|
11
32
|
|
12
|
-
|
13
|
-
|
33
|
+
it_behaves_like "a JSON interface"
|
34
|
+
end
|
35
|
+
|
36
|
+
context "with Active Support" do
|
37
|
+
before(:all) do
|
38
|
+
require 'active_support/ordered_hash'
|
39
|
+
require 'active_support/json'
|
40
|
+
load 'structure/json.rb'
|
14
41
|
end
|
15
42
|
|
16
|
-
|
17
|
-
|
43
|
+
after(:all) do
|
44
|
+
Object.send(:remove_const, :ActiveSupport)
|
18
45
|
end
|
19
46
|
|
20
|
-
|
21
|
-
|
22
|
-
|
47
|
+
it_behaves_like "a JSON interface"
|
48
|
+
|
49
|
+
describe "#as_json" do
|
50
|
+
it "returns a hash" do
|
51
|
+
person.as_json.should be_a Hash
|
52
|
+
end
|
53
|
+
|
54
|
+
it "selects a subset of attributes" do
|
55
|
+
as_json = person.as_json(:only => :name)
|
56
|
+
as_json.should have_key :name
|
57
|
+
as_json.should_not have_key :age
|
23
58
|
end
|
24
59
|
|
25
|
-
it "
|
26
|
-
|
27
|
-
|
60
|
+
it "rejects a subset of attributes" do
|
61
|
+
as_json = person.as_json(:except => :name)
|
62
|
+
as_json.should_not have_key :name
|
63
|
+
as_json.should have_key :age
|
28
64
|
end
|
29
65
|
end
|
30
66
|
end
|
data/spec/structure_spec.rb
CHANGED
@@ -41,7 +41,6 @@ describe Structure do
|
|
41
41
|
it "returns the default attributes for the structure" do
|
42
42
|
Person.send(:default_attributes).should == { :name => nil,
|
43
43
|
:age => nil,
|
44
|
-
:website => nil,
|
45
44
|
:friends => [] }
|
46
45
|
Book.send(:default_attributes).should == { :title => nil,
|
47
46
|
:authors => nil }
|
data/structure.gemspec
CHANGED
@@ -18,8 +18,9 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.rubyforge_project = "structure"
|
19
19
|
|
20
20
|
{
|
21
|
-
'
|
22
|
-
'
|
21
|
+
'activesupport' => '>= 3.0',
|
22
|
+
'rspec' => '~> 2.6',
|
23
|
+
'ruby-debug19' => '~> 0.11.6'
|
23
24
|
}.each do |lib, version|
|
24
25
|
s.add_development_dependency lib, version
|
25
26
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 8
|
8
|
+
- 0
|
9
|
+
version: 0.8.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Paper Cavalier
|
@@ -14,13 +14,27 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-06-24 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
21
|
+
name: activesupport
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
version: "3.0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rspec
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
24
38
|
none: false
|
25
39
|
requirements:
|
26
40
|
- - ~>
|
@@ -28,14 +42,13 @@ dependencies:
|
|
28
42
|
segments:
|
29
43
|
- 2
|
30
44
|
- 6
|
31
|
-
|
32
|
-
version: 2.6.0
|
45
|
+
version: "2.6"
|
33
46
|
type: :development
|
34
|
-
version_requirements: *
|
47
|
+
version_requirements: *id002
|
35
48
|
- !ruby/object:Gem::Dependency
|
36
49
|
name: ruby-debug19
|
37
50
|
prerelease: false
|
38
|
-
requirement: &
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
39
52
|
none: false
|
40
53
|
requirements:
|
41
54
|
- - ~>
|
@@ -46,7 +59,7 @@ dependencies:
|
|
46
59
|
- 6
|
47
60
|
version: 0.11.6
|
48
61
|
type: :development
|
49
|
-
version_requirements: *
|
62
|
+
version_requirements: *id003
|
50
63
|
description: |-
|
51
64
|
Structure is a Struct-like key/value container for modeling ephemeral data
|
52
65
|
in Ruby.
|
@@ -60,6 +73,8 @@ extra_rdoc_files: []
|
|
60
73
|
|
61
74
|
files:
|
62
75
|
- .gitignore
|
76
|
+
- .rvmrc
|
77
|
+
- CHANGELOG.md
|
63
78
|
- Gemfile
|
64
79
|
- LICENSE
|
65
80
|
- README.md
|