fire-model 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91a1c4ef436ffa206209dd1c016e6ff2e5bed9cd
4
- data.tar.gz: 1a962e4011f7345f1acbd3ef1ef7d454b8b4932b
3
+ metadata.gz: 94a83f0d48f12fff80f3b51db09f807a28333547
4
+ data.tar.gz: f27302a0d64efdfb24bc97f48ff8bb91090a3866
5
5
  SHA512:
6
- metadata.gz: e37877348de04f72fbf0cea2718d2f53561764fd58af4267f746b10fbd010ce4dde637e8a4ec48ed99bafd2abde1ed613c87f4c2442a34d54f29dced2d0f1a6d
7
- data.tar.gz: 149812eafc8aaad97d46ac596338c7868bf7ce0e33f0b5d794e3b86fbd8dcf54efbc4a9c375f58c72de722887f9011772cd58b6c290c8e3808dc31ca7d1e8dd4
6
+ metadata.gz: 311cb999666f276d673a64ccb98df22200b133181ef7badfc28ffb66eb77138c66db5d39f4f0f7a1ee53926961ba464ae39c9a6496643177b0d47018562aae12
7
+ data.tar.gz: 7e868d0828ccd06dea11d2ecbf167e8f9eabe40b75cc9252d007780fb06fccfea47ddbc2d321072a1876fa0e76e5792eaff13cc21e64a371dde81522c6277f3c
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.8
1
+ 0.0.9
data/fire-model.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: fire-model 0.0.8 ruby lib
5
+ # stub: fire-model 0.0.9 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "fire-model"
9
- s.version = "0.0.8"
9
+ s.version = "0.0.9"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
@@ -29,7 +29,7 @@ Gem::Specification.new do |s|
29
29
  "lib/connection/request.rb",
30
30
  "lib/connection/response.rb",
31
31
  "lib/fire-model.rb",
32
- "lib/model/model.rb",
32
+ "lib/model/base.rb",
33
33
  "lib/model/nested/nested_model.rb",
34
34
  "lib/model/nested/parent.rb",
35
35
  "lib/model/querying/querying.rb",
data/lib/fire-model.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Fire
2
2
  require 'connection/response'
3
3
  require 'connection/request'
4
- require 'model/model'
4
+ require 'model/base'
5
5
 
6
6
  require 'ostruct'
7
7
 
@@ -196,9 +196,9 @@ module Fire
196
196
  end
197
197
 
198
198
  require_relative './querying/querying'
199
- require_relative './nested/parent'
200
199
  require_relative './nested/nested_model'
200
+ require_relative './nested/parent'
201
201
  include Querying
202
- include Parent
202
+ include NestedModel::Parent
203
203
  end
204
- end
204
+ end
@@ -24,24 +24,28 @@ module Fire
24
24
  end
25
25
  end
26
26
 
27
- def nested_in(parent, options)
27
+ def nested_in(parent, options={})
28
28
  self.parent = parent
29
29
  self.nested_options = OpenStruct.new(options)
30
30
  self.parent.has_nested(self)
31
31
  end
32
32
 
33
33
  def own_path_keys
34
- parent.all_path_keys + [ nested_options.folder ] + super()
34
+ parent.all_path_keys + [ folder ] + super()
35
35
  end
36
36
 
37
37
  def collection_name
38
38
  parent.collection_name
39
39
  end
40
+
41
+ def folder
42
+ path_value_param(self.nested_options.folder ? self.nested_options.folder : to_s.pluralize)
43
+ end
40
44
  end
41
45
 
42
46
  def method_missing(*args)
43
- if args.first.to_s == self.class.nested_options.folder
44
- self.class.nested_options.folder
47
+ if args.first.to_s == self.class.folder
48
+ self.class.folder
45
49
  else
46
50
  super
47
51
  end
@@ -1,5 +1,5 @@
1
1
  module Fire
2
- class Model
2
+ class NestedModel
3
3
  module Parent
4
4
  extend ActiveSupport::Concern
5
5
 
@@ -13,9 +13,9 @@ module Fire
13
13
  self.assigned_nested_models ||= []
14
14
  self.assigned_nested_models << nested_model
15
15
 
16
- folder = nested_model.nested_options.folder
16
+ folder = nested_model.folder
17
17
  define_method "nested_#{folder}" do
18
- objects = self.class.down_levels(send(folder), nested_model.path_keys.count)
18
+ objects = self.class.down_levels(send(folder), (nested_model.path_keys || []).count)
19
19
  objects.map{|x|
20
20
  full_data = x.merge(self.path_data)
21
21
  nested_model.new(full_data)
@@ -6,34 +6,15 @@ module Fire
6
6
 
7
7
  module ClassMethods
8
8
  def query(params={}, &filter_condition)
9
- path_values, selected_keys = [], []
9
+ direct_keys = direct_path_keys(params)
10
+ full_path = ([ collection_name ] + direct_path_values(params, direct_keys)) * LEVEL_SEPARATOR
10
11
 
11
- own_path_keys.each do |key|
12
- if params[key]
13
- path_values << path_value_param(params[key])
14
- selected_keys << key
15
- else
16
- break
17
- end
18
- end
19
-
20
- full_path = ([ collection_name ] + path_values) * LEVEL_SEPARATOR
21
12
  response = connection.get(full_path).body
22
-
23
13
  return [] if response.nil?
24
14
 
25
- result = down_levels(response, (own_path_keys - selected_keys).count)
26
-
27
- filter = params.clone
28
- selected_keys.each do |sk|
29
- filter.delete(sk)
30
- end
15
+ rows = down_levels(response, (own_path_keys - direct_keys).count)
31
16
 
32
- result.map{|data| new(data) }.select do |model_object|
33
- not_filtered_by_attributes = model_object.has_data?(filter)
34
- not_filtered_by_block = block_given? ? filter_condition.(model_object) : true
35
- not_filtered_by_attributes && not_filtered_by_block
36
- end
17
+ filter_result(rows, filter_opts(params, direct_keys), filter_condition)
37
18
  end
38
19
 
39
20
  alias_method :all, :query
@@ -48,6 +29,34 @@ module Fire
48
29
  result
49
30
  end
50
31
 
32
+ def filter_result(rows, filter_opts, filter_condition)
33
+ rows.map{|data| new(data) }.select do |model_object|
34
+ not_filtered_by_attributes = model_object.has_data?(filter_opts)
35
+ not_filtered_by_block = filter_condition ? filter_condition.(model_object) : true
36
+ not_filtered_by_attributes && not_filtered_by_block
37
+ end
38
+ end
39
+
40
+ def direct_path_keys(params)
41
+ res = []
42
+ own_path_keys.each do |key|
43
+ params[key] ? (res << key) : break
44
+ end
45
+ res
46
+ end
47
+
48
+ def direct_path_values(params, direct_keys)
49
+ direct_keys.map do |dpk|
50
+ path_value_param(params[dpk])
51
+ end
52
+ end
53
+
54
+ def filter_opts(params, direct_keys)
55
+ direct_keys.each_with_object(params.clone) do |sk, res|
56
+ res.delete(sk)
57
+ end
58
+ end
59
+
51
60
  end
52
61
  end
53
62
  end
@@ -20,7 +20,7 @@ describe 'Nested Models' do
20
20
  expect(Organization.nested_models).to be_empty
21
21
 
22
22
  class Employee < Fire::NestedModel
23
- nested_in Organization, folder: 'employees'
23
+ nested_in Organization
24
24
  has_path_keys :department
25
25
  end
26
26
 
@@ -115,7 +115,7 @@ describe 'Nested Models' do
115
115
  end
116
116
 
117
117
  context 'Restrictions' do
118
- before :all do
118
+ before :each do
119
119
 
120
120
  class Hotel < Fire::Model
121
121
  has_path_keys :location, :class
@@ -128,6 +128,7 @@ describe 'Nested Models' do
128
128
  expect(->{
129
129
  class Room < Fire::NestedModel
130
130
  has_path_keys :number
131
+ nested_in Hotel, folder: 'rooms'
131
132
  end
132
133
  }).to raise_error(Fire::NestedModel::ParentModelNotSetError)
133
134
 
@@ -149,6 +150,7 @@ describe 'Nested Models' do
149
150
 
150
151
  expect(->{
151
152
  class Room < Fire::NestedModel
153
+ nested_in Hotel, folder: 'rooms'
152
154
  has_path_keys :number, :room_class
153
155
  end
154
156
  }).to be
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fire-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vitaly Tarasenko
@@ -155,7 +155,7 @@ files:
155
155
  - lib/connection/request.rb
156
156
  - lib/connection/response.rb
157
157
  - lib/fire-model.rb
158
- - lib/model/model.rb
158
+ - lib/model/base.rb
159
159
  - lib/model/nested/nested_model.rb
160
160
  - lib/model/nested/parent.rb
161
161
  - lib/model/querying/querying.rb