betty_resource 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/CHANGELOG.rdoc +8 -0
- data/Gemfile +1 -1
- data/lib/betty_resource/api.rb +3 -1
- data/lib/betty_resource/model.rb +5 -0
- data/lib/betty_resource/model/property.rb +8 -1
- data/lib/betty_resource/model/record.rb +24 -17
- data/lib/betty_resource/version.rb +1 -1
- data/test/unit/model/test_record.rb +21 -2
- data/test/unit/test_model.rb +1 -1
- metadata +15 -15
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
YmIxNGMxNGUxNWQ1OGNlMjhiMDdmZjlmZTU2ZTkzZThhMjBkNzU3ODIzYjdh
|
10
|
-
NGI3ZjBmNWQyYzBiZjc1ZDNmZmM3ODMyYWQ4NmYwMDNjMmQ2ZDRjZDMxMDc0
|
11
|
-
NjEzOGFkOTU5NzVhMTI4MDg4YTM3NGJjOGMxYzg2MTYwYTU3ZjM=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
OWE1MjE5NDI4ODRiY2VhMmU4ZjBlOGE5YTg0M2Q2MmI5ODBkNjU2NGYzZDVi
|
14
|
-
YWEyYTdmNmMzMWI3NWNkY2FhZjhkMjM3NTRmNDExNTMxOTUwYTJiOGVlMGM0
|
15
|
-
ZTFhYzIyNDYxNDc1YWMwYzczMzU2NGI1YTM1NGVkNGQxODQ1NTI=
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cfa495aa7363146a42eed8058b57cb3c6ce730b4
|
4
|
+
data.tar.gz: ad3b006f15cf5825b317afe092a435913a3f0807
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cf411c5cfe9e29b168c0043c774f353fe91a7cb75faafb3ae16935a4384593468f1bded2a8c2a274a898335840509c67fa46f8ad9a2bcdd81fd430b47dc921ce
|
7
|
+
data.tar.gz: f5bb929a910371dab0c0e54e0d99cb0bd5f8236358a3c3076c21adfa567f3ebff0ea4329cc9a68a6812674acd85d220b3a46950d9705e99bd3b882e7a31fc384
|
data/CHANGELOG.rdoc
CHANGED
data/Gemfile
CHANGED
data/lib/betty_resource/api.rb
CHANGED
data/lib/betty_resource/model.rb
CHANGED
@@ -13,7 +13,14 @@ module BettyResource
|
|
13
13
|
@id, @name, @kind, @options = id, name, kind, options
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
def typecast(value)
|
17
|
+
if kind == "belongs_to" && id = value["id"]
|
18
|
+
model.get(id)
|
19
|
+
else
|
20
|
+
value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
17
24
|
def model
|
18
25
|
BettyResource.meta_data.models.values.detect{|x| x.id == options["model"]} if kind == "belongs_to"
|
19
26
|
end
|
@@ -1,3 +1,26 @@
|
|
1
|
+
class DirtyHashy < HashWithIndifferentAccess
|
2
|
+
|
3
|
+
alias_method :regular_reader, :[]
|
4
|
+
def [](key, mapped = false)
|
5
|
+
typecasted(key) || set_typecasted(key, regular_reader(key, mapped))
|
6
|
+
end
|
7
|
+
|
8
|
+
protected
|
9
|
+
|
10
|
+
def typecasted(key)
|
11
|
+
instance_variable_get("@typecasted_#{key}")
|
12
|
+
end
|
13
|
+
|
14
|
+
def set_typecasted(key, value)
|
15
|
+
if @model
|
16
|
+
@model.typecast(key, value)
|
17
|
+
else
|
18
|
+
value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
1
24
|
module BettyResource
|
2
25
|
class Model
|
3
26
|
class Record
|
@@ -17,6 +40,7 @@ module BettyResource
|
|
17
40
|
@errors = {}
|
18
41
|
super()
|
19
42
|
self.attributes = Hash[model.attributes.collect{|x| [x, nil]}].merge attributes
|
43
|
+
self.attributes.instance_variable_set(:@model, model)
|
20
44
|
end
|
21
45
|
|
22
46
|
def new_record?
|
@@ -67,23 +91,6 @@ module BettyResource
|
|
67
91
|
|
68
92
|
private
|
69
93
|
|
70
|
-
# TODO: Clean this mess up as this is a dirty quick fix for loading belongs_to properties at the moment
|
71
|
-
def method_missing(method, *args)
|
72
|
-
if method.to_s.match(/^(\w+).id=$/)
|
73
|
-
if model.attributes.include?($1)
|
74
|
-
instance = attributes[$1] ||= begin
|
75
|
-
if property = model.properties.detect{|x| x.name == $1}
|
76
|
-
property.model.new
|
77
|
-
end
|
78
|
-
end
|
79
|
-
if instance
|
80
|
-
return instance.instance_variable_set :@id, args.first
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
super
|
85
|
-
end
|
86
|
-
|
87
94
|
def to_params
|
88
95
|
{:body => {:record => attributes_as_json}}
|
89
96
|
end
|
@@ -12,7 +12,7 @@ module Unit
|
|
12
12
|
|
13
13
|
it "should return its attributes" do
|
14
14
|
relation = BettyResource::Relation.new
|
15
|
-
assert_equal %w(first_name id last_name), relation.attributes.keys.sort
|
15
|
+
assert_equal %w(first_name group id last_name), relation.attributes.keys.sort
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should create a method for writing each attribute" do
|
@@ -58,7 +58,8 @@ module Unit
|
|
58
58
|
assert_equal({"id" => nil, "first_name" => "Paul", "last_name" => "Engel"}, relation.as_json)
|
59
59
|
|
60
60
|
relation = BettyResource::Relation.get(1)
|
61
|
-
assert_equal({"id" => 1, "first_name" => "Daniel", "last_name" => "Willemse"}, relation.as_json)
|
61
|
+
assert_equal({"id" => 1, "first_name" => "Daniel", "last_name" => "Willemse", "group"=>{"id"=>1}}, relation.as_json)
|
62
|
+
assert_equal({"id" => 1, "name" => "group one"}, relation.group.as_json)
|
62
63
|
end
|
63
64
|
|
64
65
|
it "should save itself" do
|
@@ -109,6 +110,24 @@ module Unit
|
|
109
110
|
relation = BettyResource::Relation.get(relation.id)
|
110
111
|
assert_equal "Stephan", relation.first_name
|
111
112
|
end
|
113
|
+
|
114
|
+
it "should be able to fetch a belongs-to value" do
|
115
|
+
relation = BettyResource::Relation.get(1)
|
116
|
+
assert_equal 1, relation.group.id
|
117
|
+
|
118
|
+
filter = {
|
119
|
+
"operator" => "and",
|
120
|
+
"conditions" => [
|
121
|
+
"path" => [BettyResource::Relation.property(:id).id],
|
122
|
+
"predicate" => "eq",
|
123
|
+
"criteria" => 1
|
124
|
+
]
|
125
|
+
}
|
126
|
+
|
127
|
+
relations = BettyResource::Relation.all(:filters => [filter])
|
128
|
+
assert_equal 1, relations.size
|
129
|
+
assert_equal 1, relations[0].group.id
|
130
|
+
end
|
112
131
|
end
|
113
132
|
|
114
133
|
end
|
data/test/unit/test_model.rb
CHANGED
@@ -11,7 +11,7 @@ module Unit
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should know its attributes" do
|
14
|
-
assert_equal %w(first_name id last_name), BettyResource::Relation.attributes.sort
|
14
|
+
assert_equal %w(first_name group id last_name), BettyResource::Relation.attributes.sort
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should return a new record instance" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: betty_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chiel Wester
|
@@ -11,48 +11,48 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-
|
14
|
+
date: 2013-05-14 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: httparty
|
18
18
|
requirement: !ruby/object:Gem::Requirement
|
19
19
|
requirements:
|
20
|
-
- -
|
20
|
+
- - '>='
|
21
21
|
- !ruby/object:Gem::Version
|
22
22
|
version: '0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: activesupport
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
33
33
|
requirements:
|
34
|
-
- -
|
34
|
+
- - '>='
|
35
35
|
- !ruby/object:Gem::Version
|
36
36
|
version: '0'
|
37
37
|
type: :runtime
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
|
-
- -
|
41
|
+
- - '>='
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
version: '0'
|
44
44
|
- !ruby/object:Gem::Dependency
|
45
45
|
name: crack
|
46
46
|
requirement: !ruby/object:Gem::Requirement
|
47
47
|
requirements:
|
48
|
-
- -
|
48
|
+
- - '>='
|
49
49
|
- !ruby/object:Gem::Version
|
50
50
|
version: '0'
|
51
51
|
type: :runtime
|
52
52
|
prerelease: false
|
53
53
|
version_requirements: !ruby/object:Gem::Requirement
|
54
54
|
requirements:
|
55
|
-
- -
|
55
|
+
- - '>='
|
56
56
|
- !ruby/object:Gem::Version
|
57
57
|
version: '0'
|
58
58
|
- !ruby/object:Gem::Dependency
|
@@ -73,28 +73,28 @@ dependencies:
|
|
73
73
|
name: multi_json
|
74
74
|
requirement: !ruby/object:Gem::Requirement
|
75
75
|
requirements:
|
76
|
-
- -
|
76
|
+
- - '>='
|
77
77
|
- !ruby/object:Gem::Version
|
78
78
|
version: '0'
|
79
79
|
type: :runtime
|
80
80
|
prerelease: false
|
81
81
|
version_requirements: !ruby/object:Gem::Requirement
|
82
82
|
requirements:
|
83
|
-
- -
|
83
|
+
- - '>='
|
84
84
|
- !ruby/object:Gem::Version
|
85
85
|
version: '0'
|
86
86
|
- !ruby/object:Gem::Dependency
|
87
87
|
name: minitest
|
88
88
|
requirement: !ruby/object:Gem::Requirement
|
89
89
|
requirements:
|
90
|
-
- -
|
90
|
+
- - '>='
|
91
91
|
- !ruby/object:Gem::Version
|
92
92
|
version: '0'
|
93
93
|
type: :development
|
94
94
|
prerelease: false
|
95
95
|
version_requirements: !ruby/object:Gem::Requirement
|
96
96
|
requirements:
|
97
|
-
- -
|
97
|
+
- - '>='
|
98
98
|
- !ruby/object:Gem::Version
|
99
99
|
version: '0'
|
100
100
|
description: Map Betty5 application resources to Ruby objects through the JSON API
|
@@ -136,17 +136,17 @@ require_paths:
|
|
136
136
|
- lib
|
137
137
|
required_ruby_version: !ruby/object:Gem::Requirement
|
138
138
|
requirements:
|
139
|
-
- -
|
139
|
+
- - '>='
|
140
140
|
- !ruby/object:Gem::Version
|
141
141
|
version: '0'
|
142
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
143
|
requirements:
|
144
|
-
- -
|
144
|
+
- - '>='
|
145
145
|
- !ruby/object:Gem::Version
|
146
146
|
version: '0'
|
147
147
|
requirements: []
|
148
148
|
rubyforge_project:
|
149
|
-
rubygems_version: 2.0.
|
149
|
+
rubygems_version: 2.0.0
|
150
150
|
signing_key:
|
151
151
|
specification_version: 4
|
152
152
|
summary: Map Betty5 application resources to Ruby objects
|