betty_resource 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/betty_resource.rb +12 -9
- data/lib/betty_resource/api.rb +2 -2
- data/lib/betty_resource/configuration.rb +4 -4
- data/lib/betty_resource/kind_extendable.rb +35 -0
- data/lib/betty_resource/meta_data.rb +2 -2
- data/lib/betty_resource/model.rb +11 -11
- data/lib/betty_resource/model/property.rb +22 -13
- data/lib/betty_resource/model/property/types/association.rb +21 -0
- data/lib/betty_resource/model/property/types/belongs_to.rb +18 -0
- data/lib/betty_resource/model/property/types/has_many.rb +29 -0
- data/lib/betty_resource/model/record.rb +8 -8
- data/lib/betty_resource/version.rb +2 -2
- data/script/console +3 -3
- data/test/test_helper.rb +11 -10
- data/test/unit/model/test_record.rb +60 -55
- data/test/unit/test_base.rb +8 -8
- data/test/unit/test_configuration.rb +7 -7
- data/test/unit/test_model.rb +20 -20
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8cfa318752fd70e7e5dca53b694b05ed3b0c69e3
|
4
|
+
data.tar.gz: f0a203d324f8d3b7900b7432c8d766446e84e4e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3cc1dd827a3f618b113cc77ad2740db50dc82474d7be02852507d41a414697fd02891ed329d631447e3ef33f929bf7916475da3cb633bfea04a2cc07d541ad11
|
7
|
+
data.tar.gz: 8cebce3fed61a1facf9b0fe3a70f5b59fd75606a0b05fd3b7de9f455a1078bc0db43b1240b3475389c2f24b84d6d5d35d5bad7fca9cc7cc324309dd1c908fc92
|
data/lib/betty_resource.rb
CHANGED
@@ -1,14 +1,17 @@
|
|
1
|
-
require
|
1
|
+
require 'active_support/core_ext/hash/indifferent_access' # See https://gist.github.com/1075643
|
2
|
+
require 'active_support/inflector'
|
2
3
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
4
|
+
require 'httparty'
|
5
|
+
require 'crack/json'
|
6
|
+
require 'dirty_hashy'
|
7
|
+
|
8
|
+
require 'betty_resource/kind_extendable'
|
6
9
|
|
7
10
|
module BettyResource
|
8
|
-
autoload :Api,
|
9
|
-
autoload :Configuration,
|
10
|
-
autoload :MetaData,
|
11
|
-
autoload :Model,
|
11
|
+
autoload :Api, 'betty_resource/api'
|
12
|
+
autoload :Configuration, 'betty_resource/configuration'
|
13
|
+
autoload :MetaData, 'betty_resource/meta_data'
|
14
|
+
autoload :Model, 'betty_resource/model'
|
12
15
|
|
13
16
|
def self.const_missing(name)
|
14
17
|
meta_data.model(name).tap do |model|
|
@@ -36,4 +39,4 @@ module BettyResource
|
|
36
39
|
end
|
37
40
|
end
|
38
41
|
|
39
|
-
end
|
42
|
+
end
|
data/lib/betty_resource/api.rb
CHANGED
@@ -5,8 +5,8 @@ module BettyResource
|
|
5
5
|
base_uri "#{BettyResource.config.host}/api"
|
6
6
|
format :json
|
7
7
|
basic_auth BettyResource.config.user, BettyResource.config.password
|
8
|
-
query_string_normalizer proc{|query|
|
8
|
+
query_string_normalizer proc { |query|
|
9
9
|
query.to_json
|
10
10
|
}
|
11
11
|
end
|
12
|
-
end
|
12
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
require
|
1
|
+
require 'singleton'
|
2
2
|
|
3
3
|
module BettyResource
|
4
4
|
class Configuration
|
5
5
|
def initialize(args = {})
|
6
6
|
args.each do |key, value|
|
7
|
-
|
7
|
+
send("#{key}=", value) if [:host, :user, :password].include?(key.to_sym)
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
@@ -13,8 +13,8 @@ module BettyResource
|
|
13
13
|
attr_accessor :host, :user, :password
|
14
14
|
|
15
15
|
def validate!
|
16
|
-
raise InvalidError if [:host, :user, :password].any?{|option| send(option).to_s.strip.empty?}
|
16
|
+
raise InvalidError if [:host, :user, :password].any? { |option| send(option).to_s.strip.empty? }
|
17
17
|
end
|
18
18
|
|
19
19
|
end
|
20
|
-
end
|
20
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module KindExtendable
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.send :include, InstanceMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module InstanceMethods
|
8
|
+
|
9
|
+
def extend_kind_methods
|
10
|
+
unless (m = kind_module).nil?
|
11
|
+
extend m
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def kind=(value)
|
16
|
+
if defined?(super)
|
17
|
+
super
|
18
|
+
else
|
19
|
+
@kind = value
|
20
|
+
end
|
21
|
+
@kind_module = nil
|
22
|
+
extend_kind_methods
|
23
|
+
end
|
24
|
+
|
25
|
+
def kind_module
|
26
|
+
@kind_module ||= begin
|
27
|
+
if kind.is_a?(String) && !kind.empty?
|
28
|
+
m = kind.gsub(/(?:^|_)(.)/) { Regexp.last_match[1].upcase }
|
29
|
+
ActiveSupport::Inflector.constantize("#{self.class}::Types::#{m}")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -2,7 +2,7 @@ module BettyResource
|
|
2
2
|
class MetaData
|
3
3
|
|
4
4
|
def models
|
5
|
-
@models ||= BettyResource::Model.parse(Api.get(
|
5
|
+
@models ||= BettyResource::Model.parse(Api.get('/models').parsed_response)
|
6
6
|
end
|
7
7
|
|
8
8
|
def model(name)
|
@@ -10,4 +10,4 @@ module BettyResource
|
|
10
10
|
end
|
11
11
|
|
12
12
|
end
|
13
|
-
end
|
13
|
+
end
|
data/lib/betty_resource/model.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
+
require 'betty_resource/model/record'
|
2
|
+
require 'betty_resource/model/property'
|
3
|
+
|
1
4
|
module BettyResource
|
2
5
|
class Model
|
3
|
-
autoload :Record, "betty_resource/model/record"
|
4
|
-
autoload :Property, "betty_resource/model/property"
|
5
|
-
|
6
6
|
attr_accessor :id, :name, :properties
|
7
7
|
|
8
8
|
def self.parse(input)
|
9
9
|
input.inject({}) do |hash, row|
|
10
|
-
hash.merge(row[
|
10
|
+
hash.merge(row['name'] => Model.new(row['id'], row['name'], Property.parse(row['id'], row['properties'])))
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
@@ -16,7 +16,7 @@ module BettyResource
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def property(name)
|
19
|
-
properties.
|
19
|
+
properties.find { |p|p.name == name.to_s }
|
20
20
|
end
|
21
21
|
|
22
22
|
def typecast(key, value)
|
@@ -25,14 +25,14 @@ module BettyResource
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def attributes
|
28
|
-
properties.
|
28
|
+
properties.map(&:name)
|
29
29
|
end
|
30
30
|
|
31
31
|
# TODO: Refactor this method in order to handle formatted view JSON correctly
|
32
32
|
def all(options = {})
|
33
33
|
begin
|
34
|
-
response = Api.get("/models/#{id}/records", :
|
35
|
-
((view_id = options.delete(:view_id) || options.delete(
|
34
|
+
response = Api.get("/models/#{id}/records", body: options).parsed_response
|
35
|
+
((view_id = options.delete(:view_id) || options.delete('view_id')).nil? ? response : response['records']).map do |data|
|
36
36
|
load data
|
37
37
|
end
|
38
38
|
rescue MultiJson::DecodeError
|
@@ -47,7 +47,7 @@ module BettyResource
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def first(options = {})
|
50
|
-
all(options.merge(:
|
50
|
+
all(options.merge(limit: 1)).first
|
51
51
|
end
|
52
52
|
|
53
53
|
def new(attributes = {})
|
@@ -68,7 +68,7 @@ module BettyResource
|
|
68
68
|
|
69
69
|
def load(data, record = nil)
|
70
70
|
if data
|
71
|
-
id = data.delete
|
71
|
+
id = data.delete 'id'
|
72
72
|
(record || BettyResource::Model::Record.new(self)).tap do |record|
|
73
73
|
record.instance_variable_set :@id, id
|
74
74
|
record.attributes = data
|
@@ -78,4 +78,4 @@ module BettyResource
|
|
78
78
|
end
|
79
79
|
|
80
80
|
end
|
81
|
-
end
|
81
|
+
end
|
@@ -1,30 +1,39 @@
|
|
1
|
+
require 'betty_resource/model/property/types/association'
|
2
|
+
require 'betty_resource/model/property/types/belongs_to'
|
3
|
+
require 'betty_resource/model/property/types/has_many'
|
4
|
+
|
1
5
|
module BettyResource
|
2
6
|
class Model
|
3
7
|
class Property
|
4
|
-
|
8
|
+
include KindExtendable
|
9
|
+
attr_reader :id, :name, :kind, :options
|
5
10
|
|
6
|
-
def self.parse(input)
|
7
|
-
input.
|
8
|
-
|
11
|
+
def self.parse(model_id, input)
|
12
|
+
input.map do |row|
|
13
|
+
options = row['options']
|
14
|
+
inverse_association = row["inverse_association"]
|
15
|
+
options.merge!("inverse_association" => inverse_association) if inverse_association
|
16
|
+
Property.new(row['id'], model_id, row['name'], row['kind'], options)
|
9
17
|
end
|
10
18
|
end
|
11
19
|
|
12
|
-
def initialize(id, name, kind, options)
|
13
|
-
@id, @name, @kind, @options = id, name, kind, options
|
20
|
+
def initialize(id, model_id, name, kind, options)
|
21
|
+
@id, @model_id, @name, @kind, @options = id, model_id, name, kind, options
|
22
|
+
extend_kind_methods if %(belongs_to has_many).include?(kind)
|
23
|
+
end
|
24
|
+
|
25
|
+
def collection?
|
26
|
+
false
|
14
27
|
end
|
15
28
|
|
16
29
|
def typecast(value)
|
17
|
-
|
18
|
-
model.get(id)
|
19
|
-
else
|
20
|
-
value
|
21
|
-
end
|
30
|
+
value
|
22
31
|
end
|
23
32
|
|
24
33
|
def model
|
25
|
-
BettyResource.meta_data.models.values.
|
34
|
+
@model ||= BettyResource.meta_data.models.values.find { |x| x.id == @model_id }
|
26
35
|
end
|
27
36
|
|
28
37
|
end
|
29
38
|
end
|
30
|
-
end
|
39
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module BettyResource
|
2
|
+
class Model
|
3
|
+
class Property
|
4
|
+
module Types
|
5
|
+
module Association
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def target_model
|
10
|
+
BettyResource.meta_data.models.values.find { |x| x.id == options['model'] }
|
11
|
+
end
|
12
|
+
|
13
|
+
def inverse_property
|
14
|
+
@inverse_property ||= target_model.properties.find { |x| x.id == options['inverse_association'] }
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module BettyResource
|
2
|
+
class Model
|
3
|
+
class Property
|
4
|
+
module Types
|
5
|
+
module HasMany
|
6
|
+
include Association
|
7
|
+
|
8
|
+
def collection?
|
9
|
+
true
|
10
|
+
end
|
11
|
+
|
12
|
+
def typecast(value)
|
13
|
+
filter = {
|
14
|
+
'operator' => 'and',
|
15
|
+
'conditions' => [
|
16
|
+
'path' => [inverse_property.id, model.property(:id).id],
|
17
|
+
'predicate' => 'eq',
|
18
|
+
'criteria' => 1
|
19
|
+
]
|
20
|
+
}
|
21
|
+
|
22
|
+
target_model.all :filters => [filter]
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -33,10 +33,10 @@ module BettyResource
|
|
33
33
|
|
34
34
|
def initialize(model, attributes = {})
|
35
35
|
@model = model
|
36
|
-
@id = attributes.delete(:id) || attributes.delete(
|
36
|
+
@id = attributes.delete(:id) || attributes.delete('id')
|
37
37
|
@errors = {}
|
38
38
|
super()
|
39
|
-
self.attributes = Hash[model.attributes.
|
39
|
+
self.attributes = Hash[model.attributes.map { |x| [x, nil] }].merge attributes
|
40
40
|
self.attributes.instance_variable_set(:@model, model)
|
41
41
|
end
|
42
42
|
|
@@ -70,31 +70,31 @@ module BettyResource
|
|
70
70
|
if success
|
71
71
|
model.send :load, result.parsed_response, self
|
72
72
|
else
|
73
|
-
@errors = result.parsed_response ? result.parsed_response[
|
73
|
+
@errors = result.parsed_response ? result.parsed_response['errors'] : { '' => ['Er is iets mis gegaan met het verwerken van het formulier. Probeer u het later nog eens. Onze excuses voor het ongemak'] }
|
74
74
|
end
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
78
|
def inspect
|
79
|
-
inspection = "id: #{id.inspect}, #{attributes.
|
79
|
+
inspection = "id: #{id.inspect}, #{attributes.map { |key, value| "#{key}: #{value.inspect}"}.join(", ")}"
|
80
80
|
"#<#{model.name} #{inspection}>"
|
81
81
|
end
|
82
82
|
alias :to_s :inspect
|
83
83
|
|
84
84
|
# TODO: Test this update
|
85
85
|
def as_json(options = {})
|
86
|
-
attributes_as_json(options).merge!
|
86
|
+
attributes_as_json(options).merge! 'id' => id
|
87
87
|
end
|
88
88
|
|
89
89
|
private
|
90
90
|
|
91
91
|
def to_params
|
92
|
-
{:
|
92
|
+
{ body: { record: attributes_as_json }, headers: { 'Content-Type' => 'application/json' } }
|
93
93
|
end
|
94
94
|
|
95
95
|
# TODO: Test this update
|
96
96
|
def attributes_as_json(options = {})
|
97
|
-
attributes.
|
97
|
+
attributes.reduce({}) do |h, (k, v)|
|
98
98
|
h.merge! k => (v.respond_to?(:as_json) ? v.as_json(options) : v) if v
|
99
99
|
h
|
100
100
|
end
|
@@ -102,4 +102,4 @@ module BettyResource
|
|
102
102
|
|
103
103
|
end
|
104
104
|
end
|
105
|
-
end
|
105
|
+
end
|
data/script/console
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
require
|
3
|
-
require
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler/setup'
|
4
4
|
|
5
5
|
Bundler.require :gem_default
|
6
6
|
|
@@ -8,4 +8,4 @@ BettyResource.configure(MultiJson.decode(File.read(File.expand_path('../../.cred
|
|
8
8
|
|
9
9
|
puts "Loading BettyResource development environment (#{BettyResource::VERSION})"
|
10
10
|
puts "Configured connection with #{BettyResource.config.host}".yellow
|
11
|
-
Pry.start
|
11
|
+
Pry.start
|
data/test/test_helper.rb
CHANGED
@@ -1,18 +1,19 @@
|
|
1
|
-
ENV[
|
2
|
-
ENV[
|
1
|
+
ENV['RACK_ENV'] = 'test'
|
2
|
+
ENV['TESTOPTS'] = '-v'
|
3
3
|
|
4
|
-
require
|
5
|
-
require
|
4
|
+
require 'rubygems'
|
5
|
+
require 'bundler/setup'
|
6
6
|
|
7
|
-
require
|
8
|
-
require
|
7
|
+
require 'minitest/unit'
|
8
|
+
require 'minitest/autorun'
|
9
|
+
require 'minitest/pride'
|
9
10
|
|
10
|
-
require
|
11
|
+
require 'simplecov'
|
11
12
|
|
12
13
|
SimpleCov.start do
|
13
|
-
coverage_dir
|
14
|
-
add_filter
|
14
|
+
coverage_dir 'coverage'
|
15
|
+
add_filter '/test/'
|
15
16
|
end
|
16
17
|
|
17
18
|
Bundler.require :gem_default, :gem_test
|
18
|
-
BettyResource.configure(MultiJson.decode(File.read(File.expand_path('../../.credentials', __FILE__))))
|
19
|
+
BettyResource.configure(MultiJson.decode(File.read(File.expand_path('../../.credentials', __FILE__))))
|
@@ -1,147 +1,152 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative '../../test_helper'
|
2
2
|
|
3
3
|
module Unit
|
4
4
|
module Record
|
5
5
|
class TestRecord < MiniTest::Unit::TestCase
|
6
6
|
|
7
7
|
describe BettyResource::Model::Record do
|
8
|
-
it
|
8
|
+
it 'should return its model' do
|
9
9
|
assert_equal BettyResource::Relation, BettyResource::Relation.new.model
|
10
10
|
assert_equal BettyResource::Relation, BettyResource::Relation.new.class
|
11
11
|
end
|
12
12
|
|
13
|
-
it
|
13
|
+
it 'should return its attributes' do
|
14
14
|
relation = BettyResource::Relation.new
|
15
15
|
assert_equal %w(first_name group id last_name), relation.attributes.keys.sort
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
18
|
+
it 'should create a method for writing each attribute' do
|
19
19
|
relation = BettyResource::Relation.new
|
20
|
-
assert relation.first_name =
|
21
|
-
assert relation.last_name =
|
20
|
+
assert relation.first_name = 'my_first_name'
|
21
|
+
assert relation.last_name = 'my_last_name'
|
22
22
|
end
|
23
23
|
|
24
|
-
it
|
24
|
+
it 'should create a method for reading each attribute' do
|
25
25
|
relation = BettyResource::Relation.new
|
26
|
-
relation.first_name =
|
27
|
-
assert_equal
|
26
|
+
relation.first_name = 'my_first_name'
|
27
|
+
assert_equal 'my_first_name', relation.first_name
|
28
28
|
end
|
29
29
|
|
30
|
-
it
|
30
|
+
it 'should store its values in the @attributes instance variable' do
|
31
31
|
relation = BettyResource::Relation.new
|
32
|
-
relation.first_name =
|
33
|
-
assert_equal
|
32
|
+
relation.first_name = 'my_first_name'
|
33
|
+
assert_equal 'my_first_name', relation.attributes[:first_name]
|
34
34
|
end
|
35
35
|
|
36
|
-
it
|
37
|
-
relation = BettyResource::Relation.new(:
|
38
|
-
assert_equal
|
39
|
-
assert_equal
|
36
|
+
it 'should allow mass-assignment when initializing' do
|
37
|
+
relation = BettyResource::Relation.new(first_name: 'my_first_name', last_name: 'my_last_name')
|
38
|
+
assert_equal 'my_first_name', relation.first_name
|
39
|
+
assert_equal 'my_last_name', relation.last_name
|
40
40
|
end
|
41
41
|
|
42
|
-
it
|
42
|
+
it 'should allow mass-assignment when already initialized' do
|
43
43
|
relation = BettyResource::Relation.new
|
44
|
-
relation.attributes = {:
|
45
|
-
assert_equal
|
46
|
-
assert_equal
|
44
|
+
relation.attributes = { first_name: 'my_first_name', last_name: 'my_last_name' }
|
45
|
+
assert_equal 'my_first_name', relation.first_name
|
46
|
+
assert_equal 'my_last_name', relation.last_name
|
47
47
|
end
|
48
48
|
|
49
|
-
it
|
49
|
+
it 'should not allow setting of id' do
|
50
50
|
relation = BettyResource::Relation.get(1)
|
51
51
|
assert_equal 1, relation.id
|
52
52
|
assert relation.id = 2
|
53
53
|
assert_equal 1, relation.id
|
54
54
|
end
|
55
55
|
|
56
|
-
it
|
57
|
-
relation = BettyResource::Relation.new(:
|
58
|
-
assert_equal({
|
56
|
+
it 'should be able to represent itself as JSON' do
|
57
|
+
relation = BettyResource::Relation.new(first_name: 'Paul', last_name: 'Engel')
|
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({
|
62
|
-
assert_equal({
|
61
|
+
assert_equal({ 'id' => 1, 'first_name' => 'Daniel', 'last_name' => 'Willemse', 'group' => { 'id' => 1, 'name' => 'group one' } }, relation.as_json)
|
62
|
+
assert_equal({ 'id' => 1, 'name' => 'group one' }, relation.group.as_json)
|
63
63
|
end
|
64
64
|
|
65
|
-
it
|
66
|
-
relation = BettyResource::Relation.new(:
|
65
|
+
it 'should save itself' do
|
66
|
+
relation = BettyResource::Relation.new(first_name: 'Stephan', last_name: 'Kaag')
|
67
67
|
assert relation.save
|
68
68
|
assert relation.id > 0
|
69
69
|
|
70
70
|
relation = BettyResource::Relation.get(relation.id)
|
71
|
-
assert_equal
|
72
|
-
assert_equal
|
71
|
+
assert_equal 'Stephan', relation.first_name
|
72
|
+
assert_equal 'Kaag', relation.last_name
|
73
73
|
end
|
74
74
|
|
75
|
-
it
|
76
|
-
relation = BettyResource::Relation.new(:
|
75
|
+
it 'should save itself when an %-character is used' do
|
76
|
+
relation = BettyResource::Relation.new(first_name: 'Stephan%')
|
77
77
|
assert relation.save
|
78
78
|
assert relation.id > 0
|
79
79
|
|
80
80
|
relation = BettyResource::Relation.get(relation.id)
|
81
|
-
assert_equal
|
81
|
+
assert_equal 'Stephan%', relation.first_name
|
82
82
|
end
|
83
83
|
|
84
|
-
it
|
84
|
+
it 'should not save itself when invalid (first_name is required)' do
|
85
85
|
relation = BettyResource::Relation.new
|
86
86
|
assert !relation.save
|
87
|
-
assert_equal({
|
87
|
+
assert_equal({ 'first_name' => ['is_required'] }, relation.errors)
|
88
88
|
|
89
|
-
relation.first_name =
|
89
|
+
relation.first_name = 'Stephan'
|
90
90
|
assert relation.save
|
91
91
|
end
|
92
92
|
|
93
|
-
it
|
93
|
+
it 'should have read-only errors messages' do
|
94
94
|
relation = BettyResource::Relation.create
|
95
|
-
assert_equal({
|
95
|
+
assert_equal({ 'first_name' => ['is_required'] }, relation.errors)
|
96
96
|
|
97
97
|
relation.errors.clear
|
98
|
-
assert_equal({
|
98
|
+
assert_equal({ 'first_name' => ['is_required'] }, relation.errors)
|
99
99
|
end
|
100
100
|
|
101
|
-
it
|
102
|
-
relation = BettyResource::Relation.new :
|
101
|
+
it 'should not resave itself when invalid' do
|
102
|
+
relation = BettyResource::Relation.new first_name: 'Piet'
|
103
103
|
assert relation.save
|
104
104
|
|
105
|
-
relation.first_name =
|
105
|
+
relation.first_name = ''
|
106
106
|
assert !relation.save
|
107
107
|
assert relation.id > 0
|
108
108
|
|
109
|
-
assert_equal
|
109
|
+
assert_equal 'Piet', BettyResource::Relation.get(relation.id).first_name
|
110
110
|
end
|
111
111
|
|
112
|
-
it
|
113
|
-
relation = BettyResource::Relation.new(:
|
112
|
+
it 'should update itself' do
|
113
|
+
relation = BettyResource::Relation.new(first_name: 'Stefan', last_name: 'Kaag')
|
114
114
|
relation.save
|
115
115
|
|
116
|
-
relation.first_name =
|
116
|
+
relation.first_name = 'Stephan'
|
117
117
|
relation.save
|
118
118
|
|
119
119
|
relation = BettyResource::Relation.get(relation.id)
|
120
|
-
assert_equal
|
120
|
+
assert_equal 'Stephan', relation.first_name
|
121
121
|
end
|
122
122
|
|
123
|
-
it
|
123
|
+
it 'should be able to fetch a belongs-to value' do
|
124
124
|
relation = BettyResource::Relation.get(1)
|
125
125
|
assert_equal 1, relation.group.id
|
126
126
|
|
127
127
|
filter = {
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
128
|
+
'operator' => 'and',
|
129
|
+
'conditions' => [
|
130
|
+
'path' => [BettyResource::Relation.property(:id).id],
|
131
|
+
'predicate' => 'eq',
|
132
|
+
'criteria' => 1
|
133
133
|
]
|
134
134
|
}
|
135
135
|
|
136
|
-
relations = BettyResource::Relation.all(:
|
136
|
+
relations = BettyResource::Relation.all(filters: [filter])
|
137
137
|
assert_equal 1, relations.size
|
138
138
|
assert_equal 1, relations[0].group.id
|
139
139
|
assert relations[0].group.object_id == relations[0].group.object_id
|
140
140
|
|
141
141
|
assert_nil BettyResource::Relation.new.group
|
142
142
|
end
|
143
|
+
|
144
|
+
it 'should be able to fetch a has-many value' do
|
145
|
+
group = BettyResource::Group.get(1)
|
146
|
+
assert_equal %w(Chris Daniel), group.relations.map(&:first_name).sort
|
147
|
+
end
|
143
148
|
end
|
144
149
|
|
145
150
|
end
|
146
151
|
end
|
147
|
-
end
|
152
|
+
end
|
data/test/unit/test_base.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative '../test_helper'
|
2
2
|
|
3
3
|
module Unit
|
4
4
|
class TestBase < MiniTest::Unit::TestCase
|
5
5
|
|
6
6
|
describe BettyResource do
|
7
|
-
it
|
7
|
+
it 'should validate its config' do
|
8
8
|
assert config = BettyResource.config
|
9
9
|
|
10
10
|
config.expects(:host).returns(nil)
|
@@ -12,25 +12,25 @@ module Unit
|
|
12
12
|
BettyResource.config
|
13
13
|
end
|
14
14
|
|
15
|
-
config.expects(:host).returns(
|
15
|
+
config.expects(:host).returns('')
|
16
16
|
assert_raises(BettyResource::Configuration::InvalidError) do
|
17
17
|
BettyResource.config
|
18
18
|
end
|
19
19
|
|
20
|
-
config.expects(:host).returns(
|
20
|
+
config.expects(:host).returns(' ')
|
21
21
|
assert_raises(BettyResource::Configuration::InvalidError) do
|
22
22
|
BettyResource.config
|
23
23
|
end
|
24
24
|
|
25
|
-
config.expects(:host).returns(
|
25
|
+
config.expects(:host).returns('localhost')
|
26
26
|
assert BettyResource.config
|
27
27
|
end
|
28
28
|
|
29
|
-
it
|
29
|
+
it 'should return a model instance' do
|
30
30
|
assert BettyResource::Relation.is_a?(BettyResource::Model)
|
31
31
|
end
|
32
32
|
|
33
|
-
it
|
33
|
+
it 'should raise NameError when an unknown model is requested' do
|
34
34
|
assert_raises(NameError) do
|
35
35
|
assert BettyResource::DoesNotExist
|
36
36
|
end
|
@@ -38,4 +38,4 @@ module Unit
|
|
38
38
|
end
|
39
39
|
|
40
40
|
end
|
41
|
-
end
|
41
|
+
end
|
@@ -1,26 +1,26 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative '../test_helper'
|
2
2
|
|
3
3
|
module Unit
|
4
4
|
class TestBase < MiniTest::Unit::TestCase
|
5
5
|
|
6
6
|
describe BettyResource::Configuration do
|
7
|
-
it
|
7
|
+
it 'should be able to validate itself' do
|
8
8
|
config = BettyResource::Configuration.new
|
9
9
|
assert_raises(BettyResource::Configuration::InvalidError) do
|
10
10
|
config.validate!
|
11
11
|
end
|
12
12
|
|
13
|
-
config.host =
|
13
|
+
config.host = 'localhost'
|
14
14
|
assert_raises(BettyResource::Configuration::InvalidError) do
|
15
15
|
config.validate!
|
16
16
|
end
|
17
17
|
|
18
|
-
config.host =
|
19
|
-
config.user =
|
20
|
-
config.password =
|
18
|
+
config.host = 'localhost'
|
19
|
+
config.user = 'foo'
|
20
|
+
config.password = 'bar'
|
21
21
|
assert_nil config.validate!
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
end
|
26
|
-
end
|
26
|
+
end
|
data/test/unit/test_model.rb
CHANGED
@@ -1,67 +1,67 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative '../test_helper'
|
2
2
|
|
3
3
|
module Unit
|
4
4
|
class TestModel < MiniTest::Unit::TestCase
|
5
5
|
|
6
6
|
describe BettyResource::Model do
|
7
|
-
it
|
7
|
+
it 'should know its properties' do
|
8
8
|
assert BettyResource::Relation.properties.is_a?(Array)
|
9
9
|
assert BettyResource::Relation.properties.any?
|
10
10
|
assert BettyResource::Relation.properties[0].is_a?(BettyResource::Model::Property)
|
11
11
|
end
|
12
12
|
|
13
|
-
it
|
13
|
+
it 'should know its attributes' do
|
14
14
|
assert_equal %w(first_name group id last_name), BettyResource::Relation.attributes.sort
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
17
|
+
it 'should return a new record instance' do
|
18
18
|
assert BettyResource::Relation.new.is_a?(BettyResource::Model::Record)
|
19
19
|
end
|
20
20
|
|
21
|
-
it
|
21
|
+
it 'should not load unexisting records' do
|
22
22
|
assert_nil BettyResource::Relation.get(-1)
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
25
|
+
it 'should fetch a record' do
|
26
26
|
relation = BettyResource::Relation.get(1)
|
27
27
|
assert_equal 1, relation.id
|
28
|
-
assert_equal
|
29
|
-
assert_equal
|
28
|
+
assert_equal 'Daniel', relation.first_name
|
29
|
+
assert_equal 'Willemse', relation.last_name
|
30
30
|
end
|
31
31
|
|
32
|
-
it
|
32
|
+
it 'should fetch a first record' do
|
33
33
|
relation = BettyResource::Relation.first
|
34
34
|
assert_equal 1, relation.id
|
35
|
-
assert_equal
|
36
|
-
assert_equal
|
35
|
+
assert_equal 'Daniel', relation.first_name
|
36
|
+
assert_equal 'Willemse', relation.last_name
|
37
37
|
|
38
|
-
relation = BettyResource::Relation.first :
|
38
|
+
relation = BettyResource::Relation.first filters: { path: BettyResource::Relation.property(:last_name).id, predicate: 'eq', criteria: 'Piet' }
|
39
39
|
assert_nil relation
|
40
40
|
|
41
|
-
relation = BettyResource::Relation.first :
|
41
|
+
relation = BettyResource::Relation.first filters: { path: BettyResource::Relation.property(:last_name).id, predicate: 'eq', criteria: 'Willemse' }
|
42
42
|
assert_equal 1, relation.id
|
43
43
|
end
|
44
44
|
|
45
|
-
it
|
45
|
+
it 'should fetch multiple records' do
|
46
46
|
relations = BettyResource::Relation.all
|
47
47
|
assert_equal 100, relations.size
|
48
48
|
assert relations.first.is_a?(BettyResource::Model::Record)
|
49
49
|
|
50
|
-
relations = BettyResource::Relation.all :
|
50
|
+
relations = BettyResource::Relation.all limit: 10
|
51
51
|
assert_equal 10, relations.size
|
52
52
|
assert relations.first.is_a?(BettyResource::Model::Record)
|
53
53
|
end
|
54
54
|
|
55
|
-
it
|
56
|
-
relation = BettyResource::Relation.create(:
|
55
|
+
it 'should directly create a record' do
|
56
|
+
relation = BettyResource::Relation.create(first_name: 'Stephan', last_name: 'Kaag')
|
57
57
|
assert relation
|
58
58
|
assert relation.id > 0
|
59
59
|
|
60
60
|
relation = BettyResource::Relation.get(relation.id)
|
61
|
-
assert_equal
|
62
|
-
assert_equal
|
61
|
+
assert_equal 'Stephan', relation.first_name
|
62
|
+
assert_equal 'Kaag', relation.last_name
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
66
|
end
|
67
|
-
end
|
67
|
+
end
|
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.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chiel Wester
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-
|
14
|
+
date: 2013-11-13 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: httparty
|
@@ -117,9 +117,13 @@ files:
|
|
117
117
|
- lib/betty_resource.rb
|
118
118
|
- lib/betty_resource/api.rb
|
119
119
|
- lib/betty_resource/configuration.rb
|
120
|
+
- lib/betty_resource/kind_extendable.rb
|
120
121
|
- lib/betty_resource/meta_data.rb
|
121
122
|
- lib/betty_resource/model.rb
|
122
123
|
- lib/betty_resource/model/property.rb
|
124
|
+
- lib/betty_resource/model/property/types/association.rb
|
125
|
+
- lib/betty_resource/model/property/types/belongs_to.rb
|
126
|
+
- lib/betty_resource/model/property/types/has_many.rb
|
123
127
|
- lib/betty_resource/model/record.rb
|
124
128
|
- lib/betty_resource/version.rb
|
125
129
|
- script/console
|
@@ -158,4 +162,3 @@ test_files:
|
|
158
162
|
- test/unit/test_base.rb
|
159
163
|
- test/unit/test_configuration.rb
|
160
164
|
- test/unit/test_model.rb
|
161
|
-
has_rdoc:
|