fire-model 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/Gemfile +17 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/fire-model.gemspec +74 -0
- data/lib/connection/request.rb +46 -0
- data/lib/connection/response.rb +19 -0
- data/lib/fire-model.rb +28 -0
- data/lib/model.rb +220 -0
- data/spec/models/fire_model_spec.rb +223 -0
- data/spec/spec_helper.rb +16 -0
- metadata +171 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7af5be10accd37f7897f73aaf3c9d361a3ab3750
|
4
|
+
data.tar.gz: 042a151cfaa0ab58bb224a25878eab3553103c89
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9755db6aa5104dd42df36d2fb64e7e461c9ae7eb9b023550d19781aee79df88cec60916ec61693f5bba8ebfbdcc1218f0e25fafa696455a11de75cd43fb7fa1b
|
7
|
+
data.tar.gz: 22d64bd1fb51f4de592c4a35f9f69e65fa11d258a21517dff1deba4cf32dcc7ab105ef9e2f783657117e2d5d455a7b10811573d61e6c14405a6804d9e9a4d20f
|
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem 'activesupport', '>= 2.3.5'
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem 'shoulda', '>= 0'
|
10
|
+
gem 'rdoc', '~> 3.12'
|
11
|
+
gem 'rspec', '~> 3.2'
|
12
|
+
gem 'pry', '~> 0.10.1'
|
13
|
+
gem 'httpclient', '~> 2.6'
|
14
|
+
gem 'bundler', '~> 1.0'
|
15
|
+
gem 'jeweler', '~> 2.0.1'
|
16
|
+
gem 'simplecov', '>= 0'
|
17
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2015 Vitaly Tarasenko
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= fire-model
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to fire-model
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
9
|
+
* Fork the project.
|
10
|
+
* Start a feature/bugfix branch.
|
11
|
+
* Commit and push until you are happy with your contribution.
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2015 Vitaly Tarasenko. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
17
|
+
gem.name = "fire-model"
|
18
|
+
gem.homepage = "http://github.com/tarvit/fire-model"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Simple library for Firebase }
|
21
|
+
gem.description = %Q{You can define your Firebase models, set collection names, CRUD your data. }
|
22
|
+
gem.email = "vetal.tarasenko@gmail.com"
|
23
|
+
gem.authors = ["Vitaly Tarasenko"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "Code coverage detail"
|
36
|
+
task :simplecov do
|
37
|
+
ENV['COVERAGE'] = "true"
|
38
|
+
Rake::Task['test'].execute
|
39
|
+
end
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rdoc/task'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "fire-model #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/fire-model.gemspec
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: fire-model 0.0.1 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "fire-model"
|
9
|
+
s.version = "0.0.1"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib"]
|
13
|
+
s.authors = ["Vitaly Tarasenko"]
|
14
|
+
s.date = "2015-06-17"
|
15
|
+
s.description = "You can define your Firebase models, set collection names, CRUD your data. "
|
16
|
+
s.email = "vetal.tarasenko@gmail.com"
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.rdoc"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
"Gemfile",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"fire-model.gemspec",
|
29
|
+
"lib/connection/request.rb",
|
30
|
+
"lib/connection/response.rb",
|
31
|
+
"lib/fire-model.rb",
|
32
|
+
"lib/model.rb",
|
33
|
+
"spec/models/fire_model_spec.rb",
|
34
|
+
"spec/spec_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = "http://github.com/tarvit/fire-model"
|
37
|
+
s.licenses = ["MIT"]
|
38
|
+
s.rubygems_version = "2.2.2"
|
39
|
+
s.summary = "Simple library for Firebase"
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
s.specification_version = 4
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
46
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
47
|
+
s.add_development_dependency(%q<rspec>, ["~> 3.2"])
|
48
|
+
s.add_development_dependency(%q<pry>, ["~> 0.10.1"])
|
49
|
+
s.add_development_dependency(%q<httpclient>, ["~> 2.6"])
|
50
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
51
|
+
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
52
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
55
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
56
|
+
s.add_dependency(%q<rspec>, ["~> 3.2"])
|
57
|
+
s.add_dependency(%q<pry>, ["~> 0.10.1"])
|
58
|
+
s.add_dependency(%q<httpclient>, ["~> 2.6"])
|
59
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
60
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
61
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
62
|
+
end
|
63
|
+
else
|
64
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
65
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
66
|
+
s.add_dependency(%q<rspec>, ["~> 3.2"])
|
67
|
+
s.add_dependency(%q<pry>, ["~> 0.10.1"])
|
68
|
+
s.add_dependency(%q<httpclient>, ["~> 2.6"])
|
69
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
70
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
71
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Fire
|
2
|
+
module Connection
|
3
|
+
class Request
|
4
|
+
require 'httpclient'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@client = HTTPClient.new(base_url: Fire.config.base_uri)
|
9
|
+
@client.default_header['Content-Type'] = 'application/json'
|
10
|
+
end
|
11
|
+
|
12
|
+
[ :get, :delete ].each do |method_type|
|
13
|
+
method = <<METHOD
|
14
|
+
def #{method_type}(path, query={})
|
15
|
+
process(:#{method_type}, path, query)
|
16
|
+
end
|
17
|
+
METHOD
|
18
|
+
class_eval(method)
|
19
|
+
end
|
20
|
+
|
21
|
+
[ :post, :put, :patch ].each do |method_type|
|
22
|
+
method = <<METHOD
|
23
|
+
def #{method_type}(path, value, query={})
|
24
|
+
process(:#{method_type}, path, query, value.to_json)
|
25
|
+
end
|
26
|
+
METHOD
|
27
|
+
class_eval(method)
|
28
|
+
end
|
29
|
+
|
30
|
+
alias_method :set, :put
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def process(method, path, query={}, body=nil)
|
35
|
+
response = @client.request(method, "#{path}.json", body: body, query: prepare_options(query), follow_redirect: true)
|
36
|
+
Fire::Connection::Response.new(response)
|
37
|
+
end
|
38
|
+
|
39
|
+
def prepare_options(query_options)
|
40
|
+
query_options.merge(Fire.config.auth)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Fire
|
2
|
+
module Connection
|
3
|
+
require 'active_support/all'
|
4
|
+
|
5
|
+
class Response
|
6
|
+
attr_reader :response
|
7
|
+
delegate :status, to: :response
|
8
|
+
|
9
|
+
def initialize(response)
|
10
|
+
@response = response
|
11
|
+
end
|
12
|
+
|
13
|
+
def body
|
14
|
+
JSON.parse(response.body, :quirks_mode => true)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/fire-model.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Fire
|
2
|
+
require 'connection/response'
|
3
|
+
require 'connection/request'
|
4
|
+
require 'model'
|
5
|
+
require 'ostruct'
|
6
|
+
|
7
|
+
def self.setup(options)
|
8
|
+
configuration = {}
|
9
|
+
configuration[:base_uri] = base_uri(options[:firebase_path])
|
10
|
+
configuration[:auth] = (options[:firebase_auth] || {})
|
11
|
+
@config = OpenStruct.new(configuration)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.config
|
15
|
+
@config
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.drop!
|
19
|
+
Fire::Connection::Request.new.delete(?/)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def self.base_uri(uri)
|
25
|
+
raise ArgumentError.new('base_uri must be a valid https uri') if uri !~ URI::regexp(%w(https))
|
26
|
+
uri.end_with?(?/) ? uri : (uri + ?/)
|
27
|
+
end
|
28
|
+
end
|
data/lib/model.rb
ADDED
@@ -0,0 +1,220 @@
|
|
1
|
+
module Fire
|
2
|
+
require 'ostruct'
|
3
|
+
require 'active_support'
|
4
|
+
|
5
|
+
class Model < OpenStruct
|
6
|
+
|
7
|
+
LEVEL_SEPARATOR = ?/
|
8
|
+
|
9
|
+
cattr_accessor :firebase_path
|
10
|
+
cattr_accessor :global_fire_collection, :global_path_keys
|
11
|
+
|
12
|
+
def initialize(attrs={})
|
13
|
+
data = self.class.prepare_hash(attrs)
|
14
|
+
unless data[id_key]
|
15
|
+
data[id_key] = self.class.next_id
|
16
|
+
@persisted = false
|
17
|
+
else
|
18
|
+
@persisted = true
|
19
|
+
end
|
20
|
+
@original_data = data.clone
|
21
|
+
super(data)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Record Methods
|
25
|
+
|
26
|
+
def id_key
|
27
|
+
self.class.id_key
|
28
|
+
end
|
29
|
+
|
30
|
+
def collection_name
|
31
|
+
self.class.collection_name
|
32
|
+
end
|
33
|
+
|
34
|
+
def save
|
35
|
+
self.class.new(@original_data).delete if path_changed?
|
36
|
+
self.class.connection.set(path, self.data)
|
37
|
+
@persisted = true
|
38
|
+
end
|
39
|
+
|
40
|
+
def delete
|
41
|
+
self.class.connection.delete(path)
|
42
|
+
@persisted = false
|
43
|
+
end
|
44
|
+
|
45
|
+
def persisted?
|
46
|
+
@persisted
|
47
|
+
end
|
48
|
+
|
49
|
+
def path
|
50
|
+
([ collection_name ] + path_values) * LEVEL_SEPARATOR
|
51
|
+
end
|
52
|
+
|
53
|
+
# Data Methods
|
54
|
+
|
55
|
+
def path_values
|
56
|
+
self.class.path_keys.map do |pk|
|
57
|
+
path_value = send(pk)
|
58
|
+
raise PathValueMissingError.new(pk) if path_value.to_s.empty?
|
59
|
+
self.class.path_value_param(path_value)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def custom_data(hash=self.data)
|
64
|
+
res = hash.to_a.select do |(k, v)|
|
65
|
+
!self.class.path_keys.include?(k)
|
66
|
+
end
|
67
|
+
self.class.prepare_hash(res)
|
68
|
+
end
|
69
|
+
|
70
|
+
def path_data(hash=self.data)
|
71
|
+
res = hash.to_a.select do |(k, v)|
|
72
|
+
self.class.path_keys.include?(k.to_sym)
|
73
|
+
end
|
74
|
+
self.class.prepare_hash(res)
|
75
|
+
end
|
76
|
+
|
77
|
+
def path_changed?
|
78
|
+
@persisted && (path_data != path_data(@original_data))
|
79
|
+
end
|
80
|
+
|
81
|
+
def has_data?(data)
|
82
|
+
return true if data.empty?
|
83
|
+
self.class.prepare_hash(data).each do |k, v|
|
84
|
+
return false unless self.send(k) == v
|
85
|
+
end
|
86
|
+
true
|
87
|
+
end
|
88
|
+
|
89
|
+
def ==(model_object)
|
90
|
+
self.data == model_object.data
|
91
|
+
end
|
92
|
+
|
93
|
+
def data
|
94
|
+
self.to_h
|
95
|
+
end
|
96
|
+
|
97
|
+
class << self
|
98
|
+
|
99
|
+
# Klass Setters
|
100
|
+
|
101
|
+
def in_collection(name)
|
102
|
+
self.global_fire_collection ||= {}
|
103
|
+
self.global_fire_collection[default_collection_name] = name
|
104
|
+
end
|
105
|
+
|
106
|
+
def has_path_keys(*keys)
|
107
|
+
self.global_path_keys ||= {}
|
108
|
+
self.global_path_keys[default_collection_name]=keys
|
109
|
+
end
|
110
|
+
|
111
|
+
# Klass Accessors
|
112
|
+
|
113
|
+
def collection_name
|
114
|
+
custom_name = (global_fire_collection && global_fire_collection[default_collection_name])
|
115
|
+
custom_name || default_collection_name
|
116
|
+
end
|
117
|
+
|
118
|
+
def connection
|
119
|
+
Fire::Connection::Request.new
|
120
|
+
end
|
121
|
+
|
122
|
+
def path_keys
|
123
|
+
own_path_keys + default_path_keys
|
124
|
+
end
|
125
|
+
|
126
|
+
def own_path_keys
|
127
|
+
(self.global_path_keys || { })[default_collection_name] ||= []
|
128
|
+
end
|
129
|
+
|
130
|
+
# Record Methods
|
131
|
+
|
132
|
+
def query(params={}, &filter_condition)
|
133
|
+
path_values, selected_keys = [], []
|
134
|
+
|
135
|
+
own_path_keys.each do |key|
|
136
|
+
if params[key]
|
137
|
+
path_values << path_value_param(params[key])
|
138
|
+
selected_keys << key
|
139
|
+
else
|
140
|
+
break
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
full_path = ([ collection_name ] + path_values) * LEVEL_SEPARATOR
|
145
|
+
response = connection.get(full_path).body
|
146
|
+
|
147
|
+
return [] if response.nil?
|
148
|
+
result = response.values
|
149
|
+
|
150
|
+
(own_path_keys - selected_keys).count.times do
|
151
|
+
result = result.map(&:values).flatten.compact
|
152
|
+
end
|
153
|
+
|
154
|
+
filter = params.clone
|
155
|
+
selected_keys.each do |sk|
|
156
|
+
filter.delete(sk)
|
157
|
+
end
|
158
|
+
|
159
|
+
result.map{|data| new(data) }.select do |model_object|
|
160
|
+
not_filtered_by_attributes = model_object.has_data?(filter)
|
161
|
+
not_filtered_by_block = block_given? ? filter_condition.(model_object) : true
|
162
|
+
not_filtered_by_attributes && not_filtered_by_block
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
alias_method :all, :query
|
167
|
+
|
168
|
+
def take(path_data)
|
169
|
+
path_object = new(path_data)
|
170
|
+
loaded_data = connection.get(path_object.path).body
|
171
|
+
loaded_data.nil? ? nil : new(loaded_data)
|
172
|
+
end
|
173
|
+
|
174
|
+
def create(object)
|
175
|
+
model = new(object)
|
176
|
+
model.save
|
177
|
+
model
|
178
|
+
end
|
179
|
+
|
180
|
+
# Helpers
|
181
|
+
|
182
|
+
def next_id
|
183
|
+
rand(36**8).to_s(36)
|
184
|
+
end
|
185
|
+
|
186
|
+
def path_value_param(raw_value)
|
187
|
+
return raw_value.to_s+?_ if raw_value.is_a?(Numeric)
|
188
|
+
raw_value.to_s.parameterize
|
189
|
+
end
|
190
|
+
|
191
|
+
def prepare_hash(hash)
|
192
|
+
HashWithIndifferentAccess[hash]
|
193
|
+
end
|
194
|
+
|
195
|
+
def id_key
|
196
|
+
:id
|
197
|
+
end
|
198
|
+
|
199
|
+
protected
|
200
|
+
|
201
|
+
def default_collection_name
|
202
|
+
ActiveSupport::Inflector.demodulize(name)
|
203
|
+
end
|
204
|
+
|
205
|
+
def default_path_keys
|
206
|
+
[ id_key ]
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
class FireModelError < StandardError; end
|
212
|
+
|
213
|
+
class PathValueMissingError < FireModelError
|
214
|
+
def initialize(key)
|
215
|
+
super "Required path key '#{ key }' is not set!"
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
end
|
220
|
+
end
|
@@ -0,0 +1,223 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Fire Models' do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
Fire.drop!
|
7
|
+
end
|
8
|
+
|
9
|
+
after :each do
|
10
|
+
Fire.drop!
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should have a collection & connection' do
|
14
|
+
fm = Fire::Model.new
|
15
|
+
expect(fm.collection_name).to eq('Model')
|
16
|
+
expect(Fire::Model.connection).to be_a(Fire::Connection::Request)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should have subclasses with own collection names' do
|
20
|
+
class SampleModel < Fire::Model
|
21
|
+
end
|
22
|
+
|
23
|
+
class SecondModel < Fire::Model
|
24
|
+
in_collection 'PerfectModel'
|
25
|
+
end
|
26
|
+
|
27
|
+
class ThirdModel < Fire::Model
|
28
|
+
in_collection 'BestModel'
|
29
|
+
end
|
30
|
+
|
31
|
+
sm1 = SampleModel.new
|
32
|
+
expect(sm1.collection_name).to eq('SampleModel')
|
33
|
+
|
34
|
+
sm2 = SecondModel.new
|
35
|
+
expect(sm2.collection_name).to eq('PerfectModel')
|
36
|
+
|
37
|
+
sm3 = ThirdModel.new
|
38
|
+
expect(sm3.collection_name).to eq('BestModel')
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should build paths for simple subclasses' do
|
42
|
+
|
43
|
+
class TestModel < Fire::Model
|
44
|
+
in_collection 'ExampleModel'
|
45
|
+
end
|
46
|
+
|
47
|
+
tm = TestModel.new
|
48
|
+
expect(tm.id).to be
|
49
|
+
expect(tm.path_values.count).to eq(1)
|
50
|
+
|
51
|
+
tm.id = 'id_first'
|
52
|
+
expect(tm.to_h).to eq({ id: 'id_first' })
|
53
|
+
expect(tm.path_values).to eq([ 'id_first' ])
|
54
|
+
expect(tm.path).to eq('ExampleModel/id_first')
|
55
|
+
|
56
|
+
tm = TestModel.new(id: 'id_second')
|
57
|
+
expect(tm.id).to be
|
58
|
+
expect(tm.path_values).to eq([ 'id_second' ])
|
59
|
+
expect(tm.path).to eq('ExampleModel/id_second')
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should compare attributes' do
|
63
|
+
|
64
|
+
class SomeModel < Fire::Model
|
65
|
+
end
|
66
|
+
|
67
|
+
tm = SomeModel.new(value: 1, name: 'a')
|
68
|
+
|
69
|
+
expect(tm.has_data?(value: 1)).to be_truthy
|
70
|
+
expect(tm.has_data?(value: 1, name: 'a')).to be_truthy
|
71
|
+
expect(tm.has_data?('name' => 'a')).to be_truthy
|
72
|
+
expect(tm.has_data?(value: 2)).to be_falsey
|
73
|
+
|
74
|
+
expect(tm == SomeModel.new(tm.to_h)).to be_truthy
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
it 'should build paths for subclasses with custom path keys' do
|
79
|
+
|
80
|
+
class Address < Fire::Model
|
81
|
+
has_path_keys(:country, :region, :city, :street, :number)
|
82
|
+
end
|
83
|
+
|
84
|
+
ad = Address.new(country: 'Ukraine', region: 'Cherkasy', city: 'Cherkasy')
|
85
|
+
expect(ad.id).to be
|
86
|
+
expect(->{
|
87
|
+
ad.path
|
88
|
+
}).to raise_error(Fire::Model::PathValueMissingError)
|
89
|
+
|
90
|
+
ad.id = 'my_id'
|
91
|
+
ad.street = 'Some Street'
|
92
|
+
ad.number = 101
|
93
|
+
expect(ad.path).to eq('Address/ukraine/cherkasy/cherkasy/some-street/101_/my_id')
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'Persistence' do
|
97
|
+
|
98
|
+
class Animal < Fire::Model
|
99
|
+
has_path_keys(:kingdom, :phylum, :subphylum, :class, :order, :family)
|
100
|
+
end
|
101
|
+
|
102
|
+
let(:rabbit_path_data) do
|
103
|
+
{
|
104
|
+
kingdom: 'Animalia',
|
105
|
+
phylum: 'Chordata',
|
106
|
+
subphylum: 'Vertebrata',
|
107
|
+
class: 'Mammalia',
|
108
|
+
order: 'Lagomorpha',
|
109
|
+
family: 'Leporidae',
|
110
|
+
}
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'should query save/delete object with path keys' do
|
114
|
+
expect(Animal.all).to eq([])
|
115
|
+
expect(Animal.take(rabbit_path_data)).to be_nil
|
116
|
+
|
117
|
+
rabbit = Animal.new(rabbit_path_data.merge(name: 'Bunny', age: 2))
|
118
|
+
expect(rabbit.persisted?).to be_falsey
|
119
|
+
|
120
|
+
rabbit.save
|
121
|
+
expect(rabbit.persisted?).to be_truthy
|
122
|
+
|
123
|
+
loaded_rabbit = Animal.take(rabbit_path_data.merge(id: rabbit.id))
|
124
|
+
expect(loaded_rabbit.persisted?).to be_truthy
|
125
|
+
|
126
|
+
expect(loaded_rabbit).to eq(rabbit)
|
127
|
+
|
128
|
+
expect(loaded_rabbit).to be_a(Animal)
|
129
|
+
expect(loaded_rabbit.age).to eq(2)
|
130
|
+
expect(loaded_rabbit.name).to eq('Bunny')
|
131
|
+
expect(loaded_rabbit.kingdom).to eq('Animalia')
|
132
|
+
|
133
|
+
expect(Animal.all).to eq([ loaded_rabbit ])
|
134
|
+
|
135
|
+
loaded_rabbit.delete
|
136
|
+
|
137
|
+
expect(Animal.all).to eq([ ])
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
context 'Querying & Filtering' do
|
142
|
+
|
143
|
+
it 'should perform complex queries & filters' do
|
144
|
+
|
145
|
+
class LibraryBook < Fire::Model
|
146
|
+
has_path_keys(:library, :floor, :row_number, :shelf)
|
147
|
+
end
|
148
|
+
|
149
|
+
LibraryBook.create(library: 'Shevchenko', floor: 1, row_number: 1, shelf: 10, name: 'Kobzar', author: 'T.G. Shevchenko')
|
150
|
+
LibraryBook.create(library: 'Shevchenko', floor: 1, row_number: 1, shelf: 15, name: 'Eneida', author: 'I. Kotlyrevskiy')
|
151
|
+
LibraryBook.create(library: 'Shevchenko', floor: 2, row_number: 15, shelf: 115, name: 'Lord Of The Rings', author: ' J.R.R. Tolkien')
|
152
|
+
LibraryBook.create(library: 'Skovoroda', floor: 1, row_number: 25, shelf: 34, name: 'Harry Potter', author: 'J.K. Rowling')
|
153
|
+
LibraryBook.create(library: 'Skovoroda', floor: 2, row_number: 12, shelf: 15, name: 'Hobbit', author: ' J.R.R. Tolkien')
|
154
|
+
|
155
|
+
expect(LibraryBook.all.map(&:name)).to eq([ 'Kobzar', 'Eneida', 'Lord Of The Rings', 'Harry Potter', 'Hobbit' ])
|
156
|
+
|
157
|
+
# Query by library
|
158
|
+
expect(LibraryBook.query(library: 'Shevchenko').map(&:name)).to eq([ 'Kobzar', 'Eneida', 'Lord Of The Rings' ])
|
159
|
+
expect(LibraryBook.query(library: 'Skovoroda').map(&:name)).to eq([ 'Harry Potter', 'Hobbit' ])
|
160
|
+
|
161
|
+
# Query by library, floor
|
162
|
+
expect(LibraryBook.query(library: 'Shevchenko', floor: 1).map(&:name)).to eq([ 'Kobzar', 'Eneida' ])
|
163
|
+
|
164
|
+
# Query by library, floor, row
|
165
|
+
expect(LibraryBook.query(library: 'Shevchenko', floor: 1, row_number: 1).map(&:name)).to eq([ 'Kobzar', 'Eneida' ])
|
166
|
+
|
167
|
+
# Query by shelf
|
168
|
+
expect(LibraryBook.query(shelf: 15).map(&:name)).to eq([ 'Eneida', 'Hobbit' ])
|
169
|
+
|
170
|
+
# Query by author
|
171
|
+
expect(LibraryBook.query(author: ' J.R.R. Tolkien').map(&:name)).to eq([ 'Lord Of The Rings', 'Hobbit' ])
|
172
|
+
|
173
|
+
# Query by math condition
|
174
|
+
expect(LibraryBook.query{|m| m.row_number % 5 == 0 }.map(&:name)).to eq([ 'Lord Of The Rings', 'Harry Potter' ])
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
context 'Updating' do
|
180
|
+
|
181
|
+
class Point < Fire::Model
|
182
|
+
has_path_keys(:x, :y)
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'should update data' do
|
186
|
+
p1 = Point.create(x: 1, y: 1, value: 1)
|
187
|
+
p2 = Point.create(x: 1, y: 2, value: 2)
|
188
|
+
p3 = Point.create(x: 2, y: 1, value: 3)
|
189
|
+
p4 = Point.create(x: 1, y: 1, value: 4)
|
190
|
+
|
191
|
+
expect(Point.all.map(&:value).sort).to eq([ 1, 2, 3, 4 ].sort)
|
192
|
+
|
193
|
+
p1.value = 5
|
194
|
+
expect(p1.path_changed?).to be_falsey
|
195
|
+
p1.save
|
196
|
+
|
197
|
+
expect(Point.all.map(&:value).sort).to eq([ 5, 2, 3, 4 ].sort)
|
198
|
+
|
199
|
+
reloaded_point = Point.take(x: p2.x, y: p2.y, id: p2.id)
|
200
|
+
reloaded_point.value = 6
|
201
|
+
|
202
|
+
expect(reloaded_point.path_changed?).to be_falsey
|
203
|
+
|
204
|
+
reloaded_point.save
|
205
|
+
|
206
|
+
expect(Point.all.map(&:value).sort).to eq([ 5, 6, 3, 4 ].sort)
|
207
|
+
|
208
|
+
p1.delete
|
209
|
+
|
210
|
+
expect(Point.all.map(&:value).sort).to eq([ 6, 3, 4].sort)
|
211
|
+
|
212
|
+
p3.x = 4
|
213
|
+
expect(p3.path_changed?).to be_truthy
|
214
|
+
p3.save
|
215
|
+
|
216
|
+
expect(Point.all.map(&:value).sort).to eq([ 6, 3, 4].sort)
|
217
|
+
end
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
end
|
222
|
+
|
223
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
# gems
|
5
|
+
require 'pry'
|
6
|
+
|
7
|
+
# files
|
8
|
+
require_relative '../lib/fire-model'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
config.before :all do
|
13
|
+
Fire.setup(firebase_path: ENV['TEST_FIREBASE_URL'])
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fire-model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vitaly Tarasenko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: shoulda
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rdoc
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.10.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.10.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: httpclient
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.6'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: bundler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: jeweler
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ~>
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 2.0.1
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ~>
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 2.0.1
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: 'You can define your Firebase models, set collection names, CRUD your
|
126
|
+
data. '
|
127
|
+
email: vetal.tarasenko@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files:
|
131
|
+
- LICENSE.txt
|
132
|
+
- README.rdoc
|
133
|
+
files:
|
134
|
+
- .document
|
135
|
+
- Gemfile
|
136
|
+
- LICENSE.txt
|
137
|
+
- README.rdoc
|
138
|
+
- Rakefile
|
139
|
+
- VERSION
|
140
|
+
- fire-model.gemspec
|
141
|
+
- lib/connection/request.rb
|
142
|
+
- lib/connection/response.rb
|
143
|
+
- lib/fire-model.rb
|
144
|
+
- lib/model.rb
|
145
|
+
- spec/models/fire_model_spec.rb
|
146
|
+
- spec/spec_helper.rb
|
147
|
+
homepage: http://github.com/tarvit/fire-model
|
148
|
+
licenses:
|
149
|
+
- MIT
|
150
|
+
metadata: {}
|
151
|
+
post_install_message:
|
152
|
+
rdoc_options: []
|
153
|
+
require_paths:
|
154
|
+
- lib
|
155
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - '>='
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - '>='
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
165
|
+
requirements: []
|
166
|
+
rubyforge_project:
|
167
|
+
rubygems_version: 2.2.2
|
168
|
+
signing_key:
|
169
|
+
specification_version: 4
|
170
|
+
summary: Simple library for Firebase
|
171
|
+
test_files: []
|