hyper-resource 1.0.0.lap34 → 1.0.0.lap35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f56e1c745c9154f2197accee8cbd76fb6b623fcf112f11caedc29d4dcc0ee7f
4
- data.tar.gz: c74e55520edf0064cb02ff378a70e55145a0400c21c65976de466ec283446a60
3
+ metadata.gz: 94db1a0e2799e2a5627075562dbc56ae75c94657b2508aed8d5607441566360a
4
+ data.tar.gz: cc04d0281ea6e9939a9189d077cca7a2be7a78e608de732520d6e2ab54717746
5
5
  SHA512:
6
- metadata.gz: d2fc69f4ad34347ecae9d5eac09c00c3f023b86306b1ceaab8b9593521b74db02751bbc8cb611e842f1a8f7b02f5cff2f954db9f5f2f0755facaccdd64a220c5
7
- data.tar.gz: 553518e63790f5e7bb2d8b57d24113c86a074d2d985aaf787ebb263f3b4e7409d22d136d2dada4e5a7c601a353c0588f2d5ef46854540cff3f62be786f3663d0
6
+ metadata.gz: 8d95f236d8ad5d16fe4acec3dfadd6ca9ff621345bbfae0c15053bbfedd4fb6b347b43e0f34dd32599f5e4fa5e9aa6e077235e0a5838d2d6429e6c66f4c57ca4
7
+ data.tar.gz: 9a20c71ad61301fa6e154728df772d2be101fd45a547e76afa69cefe992040ab9bae75e098f1710edc35df56430d0b54035970e47b7fcbe605dcbf6d88eb9ede
@@ -10,7 +10,6 @@ Gem::Specification.new do |s|
10
10
  s.description = "Write Browser Apps that transparently access server side resources like 'MyModel.first_name', with ease"
11
11
 
12
12
  s.files = `git ls-files`.split("\n")
13
- s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
14
13
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
14
  s.require_paths = ["lib"]
16
15
 
@@ -128,6 +128,51 @@ module HyperRecord
128
128
  end
129
129
  end
130
130
 
131
+ def has_and_belongs_to_many(direction, name = nil, options = { type: nil })
132
+ if name.is_a?(Hash)
133
+ options.merge(name)
134
+ name = direction
135
+ direction = nil
136
+ elsif name.nil?
137
+ name = direction
138
+ end
139
+ reflections[name] = { direction: direction, type: options[:type], kind: :has_and_belongs_to_many }
140
+ define_method(name) do
141
+ _register_observer
142
+ if @fetch_states[name] == 'f'
143
+ @relations[name]
144
+ elsif self.id
145
+ self.class._promise_get("#{self.class.resource_base_uri}/#{self.id}/relations/#{name}.json").then do |response|
146
+ collection = self.class._convert_array_to_collection(response.json[self.class.to_s.underscore][name], self, name)
147
+ @relations[name] = collection
148
+ @fetch_states[name] = 'f'
149
+ _notify_observers
150
+ @relations[name]
151
+ end.fail do |response|
152
+ error_message = "#{self.class.to_s}[#{self.id}].#{name}, a has_and_belongs_to_many association, failed to fetch records!"
153
+ `console.error(error_message)`
154
+ response
155
+ end
156
+ @relations[name]
157
+ else
158
+ @relations[name]
159
+ end
160
+ end
161
+ define_method("#{name}=") do |arg|
162
+ _register_observer
163
+ collection = if arg.is_a?(Array)
164
+ HyperRecord::Collection.new(arg, self, name)
165
+ elsif arg.is_a?(HyperRecord::Collection)
166
+ arg
167
+ else
168
+ raise "Argument must be a HyperRecord::Collection or a Array"
169
+ end
170
+ @relations[name] = collection
171
+ @fetch_states[name] == 'f'
172
+ @relations[name]
173
+ end
174
+ end
175
+
131
176
  def has_many(direction, name = nil, options = { type: nil })
132
177
  if name.is_a?(Hash)
133
178
  options.merge(name)
@@ -26,7 +26,7 @@ module HyperRecord
26
26
  reflections.keys.each do |relation|
27
27
  if record_hash.has_key?(relation)
28
28
  @fetch_states[relation] = 'f' # fetched
29
- if reflections[relation][:kind] == :has_many
29
+ if %i[has_many has_and_belongs_to_many].inlcude?(reflections[relation][:kind])
30
30
  if record_hash[relation].nil?
31
31
  @relations[relation] = HyperRecord::Collection.new([], self, relation)
32
32
  else
@@ -37,7 +37,7 @@ module HyperRecord
37
37
  end
38
38
  else
39
39
  unless @fetch_states[collection] == 'f'
40
- if reflections[relation][:kind] == :has_many
40
+ if %i[has_many has_and_belongs_to_many].inlcude?(reflections[relation][:kind])
41
41
  @relations[relation] = HyperRecord::Collection.new([], self, relation)
42
42
  else
43
43
  @relations[relation] = nil
@@ -20,7 +20,7 @@ class Hyperloop::Resource::RelationsController < ApplicationController
20
20
  @collection_name = params[:id].to_sym
21
21
  @collection = nil
22
22
  if @record && @record.class.reflections.has_key?(@collection_name) # guard, :id is the collection name
23
- right_class_param = if @record.class.reflections[@collection_name].association.type == :has_many
23
+ right_class_param = if %i[has_many has_and_belongs_to_many].include?(@record.class.reflections[@collection_name].association.type)
24
24
  params[:id].chop # remove the s at the end if its there
25
25
  else
26
26
  params[:id]
@@ -1,5 +1,5 @@
1
1
  module Hyperloop
2
2
  module Resource
3
- VERSION = '1.0.0.lap34'
3
+ VERSION = '1.0.0.lap35'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyper-resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.lap34
4
+ version: 1.0.0.lap35
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Biedermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-06 00:00:00.000000000 Z
11
+ date: 2018-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal