class Swarm::HiveDweller
Attributes
associations[R]
columns[R]
hive[R]
id[R]
Public Class Methods
all(hive: Hive.default, subtypes: true)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 189 def all(hive: Hive.default, subtypes: true) hive.storage.all_of_type(storage_type, subtypes: subtypes).map { |hsh| hive.reify_from_hash(hsh.dup) } end
create(hive: Hive.default, **args)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 147 def create(hive: Hive.default, **args) new(hive: hive, created_at: Time.now, **args).save end
each(hive: Hive.default, subtypes: true) { |object| ... }
click to toggle source
# File lib/swarm/hive_dweller.rb, line 179 def each(hive: Hive.default, subtypes: true, &block) return to_enum(__method__, hive: hive, subtypes: subtypes) unless block_given? ids(hive: hive).each do |id| object = fetch(id, hive: hive) if (subtypes && object.is_a?(self)) || object.class == self yield object end end end
fetch(key, hive: Hive.default)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 170 def fetch(key, hive: Hive.default) hsh = hive.storage[storage_id_for_key(key)].dup hive.reify_from_hash(hsh) end
ids(hive: Hive.default)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 175 def ids(hive: Hive.default) hive.storage.ids_for_type(storage_type) end
inherited(subclass)
click to toggle source
Calls superclass method
# File lib/swarm/hive_dweller.rb, line 95 def inherited(subclass) super subclass.instance_variable_set(:@columns, []) subclass.instance_variable_set(:@associations, []) subclass.set_columns :updated_at, :created_at end
many_to_one(type, class_name: nil, key: nil)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 133 def many_to_one(type, class_name: nil, key: nil) define_method(type) do memo = instance_variable_get(:"@#{type}") memo || begin key ||= :"#{type}_id" associated_id = self.send(key) return nil unless associated_id klass = Swarm::Support.constantize("#{class_name || type}") instance_variable_set(:"@#{type}", klass.fetch(associated_id, :hive => hive)) end end @associations << type end
new(hive: Hive.default, **args)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 7 def initialize(hive: Hive.default, **args) @hive = hive @changed_attributes = {} set_attributes(args, record_changes: false) end
new_from_storage(**args)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 163 def new_from_storage(**args) id = args.delete(:id) new(**args).tap { |instance| instance.instance_variable_set(:@id, id) } end
one_to_many(type, class_name: nil, foreign_key: nil)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 119 def one_to_many(type, class_name: nil, foreign_key: nil) define_method(type) do memo = instance_variable_get(:"@#{type}") memo || begin associations = hive.storage.load_associations( type, owner: self, type: class_name || type, foreign_key: foreign_key ) entities = associations.map { |association| hive.reify_from_hash(association) } instance_variable_set(:"@#{type}", entities) end end @associations << type end
set_columns(*args)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 102 def set_columns(*args) args.each do |arg| define_method("#{arg}=") { |value| change_attribute(arg, value) } define_method(arg) { val = instance_variable_get(:"@#{arg}") if /_at$/.match(arg) && val.is_a?(String) val = Time.parse(val) end val } end @columns = @columns | args end
storage_id_for_key(key)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 155 def storage_id_for_key(key) if key.match(/^#{storage_type}\:/) key else "#{storage_type}:#{key}" end end
storage_type()
click to toggle source
# File lib/swarm/hive_dweller.rb, line 151 def storage_type name.split("::").last end
Public Instance Methods
==(other)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 38 def ==(other) other.is_a?(self.class) && other.to_hash == to_hash end
attributes()
click to toggle source
# File lib/swarm/hive_dweller.rb, line 64 def attributes self.class.columns.each_with_object({}) { |col_name, hsh| hsh[col_name.to_sym] = send(:"#{col_name}") } end
change_attribute(key, value, record: true)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 31 def change_attribute(key, value, record: true) if record @changed_attributes[key] = [send(key), value] end instance_variable_set(:"@#{key}", value) end
changed?()
click to toggle source
# File lib/swarm/hive_dweller.rb, line 17 def changed? !@changed_attributes.empty? end
delete()
click to toggle source
# File lib/swarm/hive_dweller.rb, line 50 def delete storage.delete(storage_id) self end
new?()
click to toggle source
# File lib/swarm/hive_dweller.rb, line 13 def new? id.nil? end
reload!()
click to toggle source
# File lib/swarm/hive_dweller.rb, line 78 def reload! hsh = hive.storage[storage_id] self.class.columns.each do |column| instance_variable_set(:"@#{column}", hsh[column.to_s]) end self.class.associations.each do |type| instance_variable_set(:"@#{type}", nil) end @changed_attributes = {} self end
save()
click to toggle source
# File lib/swarm/hive_dweller.rb, line 55 def save if new? || changed? @id ||= Swarm::Support.uuid_with_timestamp storage[storage_id] = to_hash.merge(:updated_at => Time.now) reload! end self end
set_attributes(args, record_changes: true)
click to toggle source
# File lib/swarm/hive_dweller.rb, line 21 def set_attributes(args, record_changes: true) unknown_arguments = args.keys - self.class.columns unless unknown_arguments.empty? raise ArgumentError, "unknown keywords: #{unknown_arguments.join(', ')}" end args.each do |key, value| change_attribute(key, value, record: record_changes) end end
storage()
click to toggle source
# File lib/swarm/hive_dweller.rb, line 46 def storage @hive.storage end
storage_id()
click to toggle source
# File lib/swarm/hive_dweller.rb, line 42 def storage_id self.class.storage_id_for_key(id) end
to_hash()
click to toggle source
# File lib/swarm/hive_dweller.rb, line 70 def to_hash hsh = { :id => id, :type => self.class.name } hsh.merge(attributes) end