Module: Firecord::Model
- Defined in:
- lib/firecord/model.rb
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
- #field(name, type = nil) ⇒ Object
- #fields ⇒ Object
- #find(id) ⇒ Object
- #repository ⇒ Object
- #root ⇒ Object
- #root_key(root_name) ⇒ Object
- #timestamps ⇒ Object
- #validate_query!(query) ⇒ Object
- #where(query) ⇒ Object
Class Method Details
.extended(model) ⇒ Object
3 4 5 6 |
# File 'lib/firecord/model.rb', line 3 def self.extended(model) model.instance_variable_set(:@fields, []) model.fields << OpenStruct.new(name: :id, type: :private_key) end |
Instance Method Details
#all ⇒ Object
20 21 22 23 24 25 |
# File 'lib/firecord/model.rb', line 20 def all repository .all .reject { |response| response.keys.include?(:error) } .map { |response| new(response).persist } end |
#field(name, type = nil) ⇒ Object
8 9 10 11 12 |
# File 'lib/firecord/model.rb', line 8 def field(name, type = nil) return if name == :timestamps @fields << OpenStruct.new(name: name, type: type) end |
#fields ⇒ Object
46 47 48 |
# File 'lib/firecord/model.rb', line 46 def fields @fields end |
#find(id) ⇒ Object
27 28 29 30 |
# File 'lib/firecord/model.rb', line 27 def find(id) response = repository.get(id) response.nil? ? nil : new(response).persist end |
#repository ⇒ Object
54 55 56 |
# File 'lib/firecord/model.rb', line 54 def repository @repository ||= Repository::Firebase.new(root) end |
#root_key(root_name) ⇒ Object
42 43 44 |
# File 'lib/firecord/model.rb', line 42 def root_key(root_name) @root = root_name end |
#timestamps ⇒ Object
14 15 16 17 18 |
# File 'lib/firecord/model.rb', line 14 def %i(created_at updated_at).each do |stamp| @fields << OpenStruct.new(name: stamp, type: :datetime) end end |
#validate_query!(query) ⇒ Object
58 59 60 61 62 |
# File 'lib/firecord/model.rb', line 58 def validate_query!(query) result = query.keys - fields.map(&:name) raise InvalidQuery, 'Your query contains invalid key(s)' \ unless result.empty? end |
#where(query) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/firecord/model.rb', line 32 def where(query) validate_query!(query) all.select do |record| result = query.map { |name, value| record.send(name) == value }.uniq result.size == 1 && result[0] == true ? true : false end end |