class Fire::Model
Constants
- LEVEL_SEPARATOR
Public Class Methods
all_path_keys()
click to toggle source
# File lib/model/base.rb, line 160 def all_path_keys own_path_keys + default_path_keys end
collection_name()
click to toggle source
Klass Accessors
# File lib/model/base.rb, line 152 def collection_name self.fire_collection || default_collection_name end
connection()
click to toggle source
# File lib/model/base.rb, line 156 def connection Fire::Connection::Request.new end
create(object)
click to toggle source
# File lib/model/base.rb, line 176 def create(object) model = new(object) model.save model end
has_path_keys(*keys)
click to toggle source
# File lib/model/base.rb, line 142 def has_path_keys(*keys) self.path_keys = keys end
id_key()
click to toggle source
# File lib/model/base.rb, line 197 def id_key self.id_key_name || :id end
in_collection(name)
click to toggle source
Klass Setters
# File lib/model/base.rb, line 138 def in_collection(name) self.fire_collection = name end
new(attrs={})
click to toggle source
Calls superclass method
# File lib/model/base.rb, line 12 def initialize(attrs={}) data = self.class.prepare_hash(attrs) unless data[id_key] data[id_key] = self.class.next_id @persisted = false else @persisted = true end @original_data = data.clone super(data) end
next_id()
click to toggle source
Helpers
# File lib/model/base.rb, line 184 def next_id rand(36**8).to_s(36) end
own_path_keys()
click to toggle source
# File lib/model/base.rb, line 164 def own_path_keys self.path_keys || [] end
path_value_param(raw_value)
click to toggle source
# File lib/model/base.rb, line 188 def path_value_param(raw_value) return raw_value.to_s+?_ if raw_value.is_a?(Numeric) raw_value.to_s.parameterize end
prepare_hash(hash)
click to toggle source
# File lib/model/base.rb, line 193 def prepare_hash(hash) HashWithIndifferentAccess[hash] end
set_id_key(value)
click to toggle source
# File lib/model/base.rb, line 146 def set_id_key(value) self.id_key_name = value end
take(path_data)
click to toggle source
Record Methods
# File lib/model/base.rb, line 170 def take(path_data) path_object = new(path_data) loaded_data = connection.get(path_object.path).body loaded_data.nil? ? nil : new(loaded_data) end
Protected Class Methods
default_collection_name()
click to toggle source
# File lib/model/base.rb, line 203 def default_collection_name ActiveSupport::Inflector.demodulize(name) end
default_path_keys()
click to toggle source
# File lib/model/base.rb, line 207 def default_path_keys [ id_key ] end
Public Instance Methods
==(model_object)
click to toggle source
# File lib/model/base.rb, line 121 def ==(model_object) self.data == model_object.data end
cache(key, &value)
click to toggle source
# File lib/model/base.rb, line 129 def cache(key, &value) @cache ||= {} @cache[key] ||= value.call end
collection_name()
click to toggle source
# File lib/model/base.rb, line 34 def collection_name self.class.collection_name end
custom_data(hash=self.data)
click to toggle source
# File lib/model/base.rb, line 95 def custom_data(hash=self.data) res = hash.to_a.select do |(k, v)| !self.class.all_path_keys.include?(k) end self.class.prepare_hash(res) end
data()
click to toggle source
# File lib/model/base.rb, line 125 def data self.to_h end
delete()
click to toggle source
# File lib/model/base.rb, line 56 def delete self.class.connection.delete(path) @persisted = false end
delete_field(field)
click to toggle source
# File lib/model/base.rb, line 44 def delete_field(field) self.send("#{field}=", nil) field_path = [path, field] * LEVEL_SEPARATOR self.class.connection.delete(field_path) end
has_data?(data)
click to toggle source
# File lib/model/base.rb, line 113 def has_data?(data) return true if data.empty? self.class.prepare_hash(data).each do |k, v| return false unless self.send(k) == v end true end
id_key()
click to toggle source
Record Methods
# File lib/model/base.rb, line 26 def id_key self.class.id_key end
id_value()
click to toggle source
# File lib/model/base.rb, line 30 def id_value send(id_key) end
path()
click to toggle source
# File lib/model/base.rb, line 65 def path ([ collection_name ] + path_values) * LEVEL_SEPARATOR end
path_changed?()
click to toggle source
# File lib/model/base.rb, line 109 def path_changed? @persisted && (path_data != path_data(@original_data)) end
path_data(hash=self.data)
click to toggle source
# File lib/model/base.rb, line 102 def path_data(hash=self.data) res = hash.to_a.select do |(k, v)| self.class.all_path_keys.include?(k.to_sym) end self.class.prepare_hash(res) end
path_values()
click to toggle source
Data Methods
# File lib/model/base.rb, line 87 def path_values self.class.all_path_keys.map do |pk| path_value = send(pk) raise PathValueMissingError.new(pk) if path_value.to_s.empty? self.class.path_value_param(path_value) end end
persisted?()
click to toggle source
# File lib/model/base.rb, line 61 def persisted? @persisted end
reload()
click to toggle source
# File lib/model/base.rb, line 69 def reload loaded_data = self.class.take(path_data).data @table = loaded_data @cache = {} self end
save()
click to toggle source
# File lib/model/base.rb, line 50 def save self.class.new(@original_data).delete if path_changed? self.class.connection.set(path, self.saving_data) @persisted = true end
set(attrs)
click to toggle source
# File lib/model/base.rb, line 81 def set(attrs) @table.merge!(attrs) end
update(attrs)
click to toggle source
# File lib/model/base.rb, line 76 def update(attrs) set(attrs) save end
update_field(field, value)
click to toggle source
# File lib/model/base.rb, line 38 def update_field(field, value) self.send("#{field}=", value) field_path = [path, field] * LEVEL_SEPARATOR self.class.connection.set(field_path, value) end