Class: Hari::Entity

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming, Property::Builder
Includes:
ActiveModel::Validations, Repository, Serialization
Defined in:
lib/hari/entity.rb,
lib/hari/entity/property.rb,
lib/hari/entity/repository.rb,
lib/hari/entity/serialization.rb,
lib/hari/entity/property/builder.rb,
lib/hari/entity/serialization/date.rb,
lib/hari/entity/serialization/time.rb,
lib/hari/entity/serialization/float.rb,
lib/hari/entity/serialization/string.rb,
lib/hari/entity/serialization/boolean.rb,
lib/hari/entity/serialization/integer.rb,
lib/hari/entity/serialization/datetime.rb

Direct Known Subclasses

Node, Relation

Defined Under Namespace

Modules: Repository, Serialization Classes: Property

Instance Method Summary collapse

Methods included from Property::Builder

properties, property

Methods included from Serialization

#to_json

Methods included from Repository

#create, #create_or_update, #delete, #persist, #update

Constructor Details

#initialize(attrs = {}) ⇒ Entity

Returns a new instance of Entity.



25
26
27
28
29
30
31
32
33
# File 'lib/hari/entity.rb', line 25

def initialize(attrs = {})
  return if attrs.blank?

  attrs = attrs.with_indifferent_access

  self.class.properties.each do |prop|
    send("#{prop.name}=", attrs[prop.name]) if attrs[prop.name]
  end
end

Instance Method Details

#==(other) ⇒ Object



49
50
51
# File 'lib/hari/entity.rb', line 49

def ==(other)
  other.is_a?(Hari::Entity) && id == other.id
end

#attributesObject



35
36
37
38
39
# File 'lib/hari/entity.rb', line 35

def attributes
  self.class.properties.inject({}) do |buffer, prop|
    buffer.merge prop.name => send(prop.name)
  end
end

#destroyed?Boolean

Returns:



63
64
65
# File 'lib/hari/entity.rb', line 63

def destroyed?
  @destroyed
end

#generate_idObject



67
68
69
# File 'lib/hari/entity.rb', line 67

def generate_id
  '_e' + ::Time.now.strftime('%Y%m%d%H%M%S') + SecureRandom.hex(3)
end

#new?Boolean Also known as: new_record?

Returns:



53
54
55
# File 'lib/hari/entity.rb', line 53

def new?
  id.nil?
end

#persisted?Boolean

Returns:



59
60
61
# File 'lib/hari/entity.rb', line 59

def persisted?
  not new?
end

#to_sObject



71
72
73
74
75
76
# File 'lib/hari/entity.rb', line 71

def to_s
  attrs = attributes
  attrs.delete 'id'

  "<#{self.class} id='#{id}' attributes=#{attrs}>"
end

#write_attribute(name, value) ⇒ Object



45
46
47
# File 'lib/hari/entity.rb', line 45

def write_attribute(name, value)
  send "#{name}=", value
end