class Related::Entity

Attributes

attributes[R]
id[R]

Public Class Methods

create(attributes = {}) click to toggle source
# File lib/related/entity.rb, line 110
def self.create(attributes = {})
  self.new(attributes).save
end
decrement!(id, attribute, by = 1) click to toggle source
# File lib/related/entity.rb, line 135
def self.decrement!(id, attribute, by = 1)
  raise Related::NotFound if id.blank?
  Related.redis.hincrby(id.to_s, attribute.to_s, -by.to_i)
end
find(*args) click to toggle source
# File lib/related/entity.rb, line 114
def self.find(*args)
  options = args.size > 1 && args.last.is_a?(Hash) ? args.pop : {}
  args.size == 1 && !args.first.is_a?(Array) ?
    find_one(args.first, options) :
    find_many(args.flatten, options)
end
increment!(id, attribute, by = 1) click to toggle source
# File lib/related/entity.rb, line 130
def self.increment!(id, attribute, by = 1)
  raise Related::NotFound if id.blank?
  Related.redis.hincrby(id.to_s, attribute.to_s, by.to_i)
end
new(attributes = {}) click to toggle source
# File lib/related/entity.rb, line 22
def initialize(attributes = {})
  @attributes = {}

  @_internal_id = attributes.delete(:id) || Related.generate_id

  attributes.each do |key,value|
    serializer = self.class.property_serializer(key)
    @attributes[key.to_s] = serializer ?
      serializer.to_string(value) : value
  end
end
properties() click to toggle source
# File lib/related/entity.rb, line 126
def self.properties
  @properties ? @properties.keys : []
end
property(name, klass=nil, &block) click to toggle source
# File lib/related/entity.rb, line 121
def self.property(name, klass=nil, &block)
  @properties ||= {}
  @properties[name.to_sym] = Serializer.new(klass, block)
end

Private Class Methods

find_fields(id, fields) click to toggle source
# File lib/related/entity.rb, line 189
def self.find_fields(id, fields)
  res = Related.redis.hmget(id.to_s, *fields)
  if res
    attributes = {}
    res.each_with_index do |value, i|
      attributes[fields[i]] = value
    end
    attributes
  end
end
find_many(ids, options = {}) click to toggle source
# File lib/related/entity.rb, line 213
def self.find_many(ids, options = {})
  res = pipelined_fetch(ids) do |id|
    if options[:fields]
      Related.redis.hmget(id.to_s, *options[:fields])
    else
      Related.redis.hgetall(id.to_s)
    end
  end
  objects = []

  ids.each_with_index do |id,i|
    if options[:fields]
      attributes = {}
      res[i].each_with_index do |value, i|
        attributes[options[:fields][i]] = value
      end
      klass = get_model(options[:model], attributes)
      objects << klass.new.send(:load_attributes, id, attributes)
    else
      attributes = res[i].is_a?(Array) ? Hash[*res[i]] : res[i]
      klass = get_model(options[:model], attributes)
      objects << klass.new.send(:load_attributes, id, attributes)
    end
  end
  objects
end
find_one(id, options = {}) click to toggle source
# File lib/related/entity.rb, line 200
def self.find_one(id, options = {})
  attributes = options[:fields] ?
    find_fields(id, options[:fields]) :
    Related.redis.hgetall(id.to_s)
  if attributes.empty?
    if Related.redis.exists(id) == false
      raise Related::NotFound, id
    end
  end
  klass = get_model(options[:model], attributes)
  klass.new.send(:load_attributes, id, attributes)
end
get_model(model, attributes) click to toggle source
# File lib/related/entity.rb, line 240
def self.get_model(model, attributes)
  return self unless model

  model.is_a?(Proc) ? model.call(attributes) : model
end
pipelined_fetch(ids, &block) click to toggle source
# File lib/related/entity.rb, line 246
def self.pipelined_fetch(ids, &block)
  Related.redis.pipelined do
    ids.each do |id|
      block.call(id)
    end
  end
rescue Redis::Distributed::CannotDistribute
  ids.map do |id|
    block.call(id)
  end
end
property_serializer(property) click to toggle source
# File lib/related/entity.rb, line 258
def self.property_serializer(property)
  @properties ||= {}
  @properties[property.to_sym]
end

Public Instance Methods

==(other) click to toggle source
# File lib/related/entity.rb, line 84
def ==(other)
  other.is_a?(Related::Entity) && self.to_key == other.to_key
end
attribute(name) click to toggle source
# File lib/related/entity.rb, line 49
def attribute(name)
  read_attribute(name)
end
decrement!(attribute, by = 1) click to toggle source
# File lib/related/entity.rb, line 144
def decrement!(attribute, by = 1)
  self.class.decrement!(@id, attribute, by)
end
destroy() click to toggle source
# File lib/related/entity.rb, line 106
def destroy
  delete
end
destroyed?() click to toggle source
# File lib/related/entity.rb, line 98
def destroyed?
  @destroyed
end
has_attribute?(name) click to toggle source
# File lib/related/entity.rb, line 63
def has_attribute?(name)
  @attributes ||= {}
  @attributes.has_key?(name.to_s) ||
    @attributes.has_key?(name) ||
    @properties.has_key?(name.to_sym)
end
increment!(attribute, by = 1) click to toggle source
# File lib/related/entity.rb, line 140
def increment!(attribute, by = 1)
  self.class.increment!(@id, attribute, by)
end
method_missing(sym, *args, &block) click to toggle source
# File lib/related/entity.rb, line 70
def method_missing(sym, *args, &block)
  if sym.to_s =~ /=$/
    name = sym.to_s[0..-2]
    serializer = self.class.property_serializer(name)
    write_attribute(name,
      serializer ? serializer.to_string(args.first) :
                   args.first)
  else
    serializer = self.class.property_serializer(sym)
    serializer ? serializer.from_string(read_attribute(sym)) :
                 read_attribute(sym)
  end
end
new?() click to toggle source
# File lib/related/entity.rb, line 88
def new?
  @id.nil? ? true : false
end
Also aliased as: new_record?
new_record?()
Alias for: new?
persisted?() click to toggle source
# File lib/related/entity.rb, line 94
def persisted?
  !new?
end
read_attribute(name) click to toggle source
# File lib/related/entity.rb, line 53
def read_attribute(name)
  @attributes ||= {}
  @attributes[name.to_s] || @attributes[name]
end
save() click to toggle source
# File lib/related/entity.rb, line 102
def save
  create_or_update
end
to_s() click to toggle source
# File lib/related/entity.rb, line 34
def to_s
  self.id
end
write_attribute(name, value) click to toggle source
# File lib/related/entity.rb, line 58
def write_attribute(name, value)
  @attributes ||= {}
  @attributes[name.to_s] = value
end

Private Instance Methods

create() click to toggle source
# File lib/related/entity.rb, line 162
def create
  run_callbacks :create do
    raise Related::ValidationsFailed, self unless valid?(:create)
    @id = @_internal_id
    @attributes.merge!('created_at' => Time.now.utc.iso8601)
    Related.redis.hmset(@id, *@attributes.to_a.flatten)
  end
  self
end
create_or_update() click to toggle source
# File lib/related/entity.rb, line 156
def create_or_update
  run_callbacks :save do
    new? ? create : update
  end
end
delete() click to toggle source
# File lib/related/entity.rb, line 181
def delete
  run_callbacks :destroy do
    Related.redis.del(id)
    @destroyed = true
  end
  self
end
load_attributes(id, attributes) click to toggle source
# File lib/related/entity.rb, line 150
def load_attributes(id, attributes)
  @id = id
  @attributes = attributes
  self
end
update() click to toggle source
# File lib/related/entity.rb, line 172
def update
  run_callbacks :update do
    raise Related::ValidationsFailed, self unless valid?(:update)
    @attributes.merge!('updated_at' => Time.now.utc.iso8601)
    Related.redis.hmset(@id, *@attributes.to_a.flatten)
  end
  self
end