restly 0.0.1.alpha.10 → 0.0.1.alpha.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ module Restly::Associations::Base::Conditionals
2
2
 
3
3
  # Conditionals
4
4
  def valid?(val)
5
- valid_instances = Array.wrap(val).reject{ |item| item.resource_name == @association_class.resource_name }.empty?
5
+ valid_instances = Array.wrap(val).reject{ |item| item.resource_name == association_class.resource_name }.empty?
6
6
  raise Restly::Error::InvalidObject, "#{val} is not a #{association_class}" unless valid_instances
7
7
  end
8
8
 
@@ -25,7 +25,7 @@ class Restly::Associations::Base
25
25
  end
26
26
 
27
27
  def association_class
28
- [@namespace, options(:class_name)].select(&:present?).join('::').constantize
28
+ [@namespace, options[:class_name]].select(&:present?).join('::').constantize
29
29
  end
30
30
 
31
31
  private
@@ -1,3 +1,3 @@
1
1
  module Restly
2
- VERSION = "0.0.1.alpha.10"
2
+ VERSION = "0.0.1.alpha.11"
3
3
  end
data/lib/restly.rb CHANGED
@@ -23,8 +23,8 @@ module Restly
23
23
  autoload :ConcernedInheritance
24
24
 
25
25
  if defined?(Rails::Console)
26
- def self.login(username, password)
27
- Base.current_token = { access_token: Client.new.password.get_token(username, password).token }
26
+ def self.login(username, password, scope = "full")
27
+ Base.current_token = { access_token: Client.new.password.get_token(username, password, scope: scope).token }
28
28
  end
29
29
  end
30
30
 
@@ -55,11 +55,89 @@ class MemoryModel < Hash
55
55
  }
56
56
  end
57
57
 
58
+ private
59
+
58
60
  def field(attr, options={})
59
61
  options.assert_valid_keys(:default)
60
62
  self.fields[attr] = options[:default]
61
63
  end
62
64
 
65
+ def belongs_to(association, options={})
66
+ options.assert_valid_keys(:class_name)
67
+ foreign_key = options[:foreign_key] || "#{association}_id".to_sym
68
+ field foreign_key
69
+ klass = -> { (options[:class_name] || association).classify.constantize }
70
+ define_method association do
71
+
72
+ extender = Module.new do
73
+
74
+ def build
75
+ klass.new(foreign_key.to_sym => self[:id])
76
+ end
77
+
78
+ def =(obj)
79
+ raise "invalid" unless obj.is_a(klass)
80
+ obj.update(foreign_key.to_sym => self[:id])
81
+ end
82
+
83
+ end
84
+
85
+ instance = klass.call.find(self[foreign_key])
86
+ instance.extend extender
87
+
88
+ end
89
+ end
90
+
91
+ def has_one(association, options={})
92
+ options.assert_valid_keys(:class_name, :foreign_key)
93
+ foreign_key = options[:foreign_key] || "#{name}_id".to_sym
94
+ klass = -> { (options[:class_name] || association).classify.constantize }
95
+ define_method association do
96
+
97
+ extender = Module.new do
98
+
99
+ def build
100
+ klass.new(foreign_key.to_sym => self[:id])
101
+ end
102
+
103
+ def =(obj)
104
+ raise "invalid" unless obj.is_a(klass)
105
+ obj.update(foreign_key.to_sym => self[:id])
106
+ end
107
+
108
+ end
109
+
110
+ instance = klass.call.all.find {|i| i[foreign_key] == self[:id] }
111
+ instance.extend extender
112
+
113
+ end
114
+ end
115
+
116
+ def has_many(association, options={})
117
+ options.assert_valid_keys(:class_name, :foreign_key)
118
+ foreign_key = options[:foreign_key] || "#{name}_id".to_sym
119
+ klass = -> { (options[:class_name] || association).classify.constantize }
120
+ define_method association do
121
+
122
+ extender = Module.new do
123
+
124
+ def build
125
+ klass.new(foreign_key.to_sym => self[:id])
126
+ end
127
+
128
+ def <<(obj)
129
+ raise "invalid!" unless obj.is_a(klass)
130
+ obj.update(foreign_key.to_sym => self[:id])
131
+ end
132
+
133
+ end
134
+
135
+ collection = klass.call.all.select {|i| i[foreign_key] == self[:id] }
136
+ collection.extend extender
137
+
138
+ end
139
+ end
140
+
63
141
  end
64
142
 
65
143
  def initialize(hash={})
@@ -10,6 +10,9 @@ module SampleObjects
10
10
  field :created_at, default: ->{ Time.now }
11
11
  field :updated_at, default: ->{ Time.now }
12
12
 
13
+ has_many :posts
14
+ has_many :comment
15
+
13
16
  end
14
17
 
15
18
  class Post < MemoryModel
@@ -18,6 +21,31 @@ module SampleObjects
18
21
  field :created_at, default: ->{ Time.now }
19
22
  field :updated_at, default: ->{ Time.now }
20
23
 
24
+ has_many :comments
25
+ has_one :rating
26
+
27
+ end
28
+
29
+ class Comment < MemoryModel
30
+
31
+ field :content
32
+ field :created_at, default: ->{ Time.now }
33
+ field :updated_at, default: ->{ Time.now }
34
+
35
+ belongs_to :user
36
+
37
+
38
+ end
39
+
40
+ class Rating < MemoryModel
41
+
42
+ field :stars, default: -> { Random.rand(1.0..5.0).round(1) }
43
+ field :count, default: -> { Random.rand(20..100) }
44
+
45
+ accepts_nested_attributes_for :post
46
+
47
+ belongs_to :post
48
+
21
49
  end
22
50
 
23
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha.10
4
+ version: 0.0.1.alpha.11
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-09 00:00:00.000000000 Z
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth2
@@ -463,3 +463,4 @@ test_files:
463
463
  - spec/support/requester.rb
464
464
  - spec/support/routes.rb
465
465
  - spec/support/shared.rb
466
+ has_rdoc: