Class: Wimdu::Property

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ActiveModel::Model, ActiveModel::Validations
Defined in:
lib/wimdu/property.rb

Defined Under Namespace

Classes: InvalidPropertyError, NothingToContinue

Constant Summary collapse

ATTRIBUTES =
[:title, :property_type, :nightly_rate, :max_guests, :email, :phone_number]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProperty

Returns a new instance of Property.



24
25
26
# File 'lib/wimdu/property.rb', line 24

def initialize
 @_id = SecureRandom.hex(5)
end

Instance Attribute Details

#_idObject (readonly)

Returns the value of attribute _id.



9
10
11
# File 'lib/wimdu/property.rb', line 9

def _id
  @_id
end

Class Method Details

.connectionObject



48
49
50
# File 'lib/wimdu/property.rb', line 48

def connection
  @connection ||= PStore.new('wimdu.db')
end

.delete_allObject



58
59
60
61
62
63
64
# File 'lib/wimdu/property.rb', line 58

def delete_all
  transaction do
    connection.roots.each do |key|
      connection.delete(key)
    end
  end
end

.find(id) ⇒ Object



70
71
72
# File 'lib/wimdu/property.rb', line 70

def find(id)
  transaction{connection[id]}
end

.listObject



66
67
68
# File 'lib/wimdu/property.rb', line 66

def list
  transaction{connection.roots.map{|id| connection[id]}.map{|property| property._id}}
end

.transactionObject



52
53
54
55
56
# File 'lib/wimdu/property.rb', line 52

def transaction
  connection.transaction do
    yield
  end
end

.unsaved_attributes(property) ⇒ Object



74
75
76
77
# File 'lib/wimdu/property.rb', line 74

def unsaved_attributes(property)
  last_attribute = Wimdu::Property::ATTRIBUTES.find{|attribute| property.send(attribute).nil?} or raise NothingToContinue, "Property already saved"
  Wimdu::Property::ATTRIBUTES[Wimdu::Property::ATTRIBUTES.index(last_attribute)..-1]
end

Instance Method Details

#delete(key) ⇒ Object



42
43
44
# File 'lib/wimdu/property.rb', line 42

def delete(key)
  transaction{connection.delete(key)}
end

#saveObject



37
38
39
40
# File 'lib/wimdu/property.rb', line 37

def save
  raise InvalidPropertyError, errors.full_messages.join unless valid?
  transaction{connection[@_id] = self}
end

#update_attribute(attribute, value) ⇒ Object



32
33
34
35
# File 'lib/wimdu/property.rb', line 32

def update_attribute(attribute, value)
  self.send("#{attribute}=", value)
  self.save
end