Module: Muve::Model

Includes:
Error, Helper
Included in:
Location, Movement, Place, Traveller
Defined in:
lib/muve/model.rb,
lib/muve.rb

Overview

Muve models imitate some behavior that one may commonly expect of typical models. There are mechanisms in place for creation, modification, retrieval and the removal of resources.

In order to make this a flexible solution, models never take care of handling the persisting of the resources they manange. That part of the lifing is dispatched to the Model::Store object which may be though of as an adaptor.

– TODO: Include ActiveRecord::Model instead of using this tedious implementation ++

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

symbolize_keys

Class Method Details

.connectionObject

Connection to the datastore



48
49
50
51
52
53
54
# File 'lib/muve.rb', line 48

def self.connection
  begin
  @@conn
  rescue => e
    raise NotConfigured, "the connection has not been defined"
  end
end

.connection=(connection) ⇒ Object

Set the connection to the datastore



39
40
41
# File 'lib/muve.rb', line 39

def self.connection=connection
  (@@conn = connection) if (connection)
end

.databaseObject

Database instance to be used by the adaptor



57
58
59
60
61
62
63
# File 'lib/muve.rb', line 57

def self.database
  begin
    @@db
  rescue => e
    raise NotConfigured, "the database has not been defined"
  end
end

.database=(database) ⇒ Object



43
44
45
# File 'lib/muve.rb', line 43

def self.database=database
  (@@db = database) if (database)
end

.handlerObject



50
51
52
# File 'lib/muve/model.rb', line 50

def self.handler
  @handler
end

.included(base) ⇒ Object



35
36
37
# File 'lib/muve/model.rb', line 35

def self.included(base)
  base.extend ClassMethods
end

.init(handler = nil) ⇒ Object

Initializes the Muve::Model class. Use the Muve::Model::init method to set a adaptor to take care of the retrieval and storage of resources.



46
47
48
# File 'lib/muve/model.rb', line 46

def self.init(handler=nil)
  @handler = handler if handler
end

Instance Method Details

#==(rival) ⇒ Object



39
40
41
42
# File 'lib/muve/model.rb', line 39

def ==(rival)
  return false unless rival.kind_of? self.class
  self.attributes == rival.attributes
end

#attributesObject

Returns a Hash of the attributes for the current resource.



141
142
143
144
145
146
147
148
# File 'lib/muve/model.rb', line 141

def attributes
  self.class.extract_attributes(
    resource: self,
    fields: fields,
    invalid_attributes: invalid_attributes,
    id: self.id
  )
end

#attributes=(data) ⇒ Object



135
136
137
138
# File 'lib/muve/model.rb', line 135

def attributes=(data)
  Helper.symbolize_keys(data).each { |k, v| self.public_send("#{k}=", v) }
  self
end

#connectionObject

Connection to the datastore



29
30
31
# File 'lib/muve.rb', line 29

def connection
  Model.connection
end

#databaseObject

Database instance for the model



34
35
36
# File 'lib/muve.rb', line 34

def database
  Model.database
end

#destroyObject

Destroy a resource



92
93
94
95
96
# File 'lib/muve/model.rb', line 92

def destroy
  if adaptor.delete(self.class, id) == true
    @destroyed = true 
  end
end

#destroyed?Boolean

Returns true if the resource in question has been destroyed

Returns:

  • (Boolean)


112
113
114
# File 'lib/muve/model.rb', line 112

def destroyed?
  @destroyed
end

#idObject



126
127
128
# File 'lib/muve/model.rb', line 126

def id
  @id
end

#initialize(params = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/muve/model.rb', line 19

def initialize(params={})
  params = {} unless params
  params.each do |attr, value|
    next if invalid_attributes.include? attr.to_s
    self.public_send "#{attr}=", value
  end

  @new_record = true
  @destroyed = false
end

#invalid?Boolean

Returns true if the resource fails any validation

Returns:

  • (Boolean)


122
123
124
# File 'lib/muve/model.rb', line 122

def invalid?
  !valid?
end

#new_record?Boolean

Returns true if the resource has recently been instantiated but not yet written to the data store.

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/muve/model.rb', line 100

def new_record?
  @new_record = true if @new_record.nil?
  @new_record
end

#persisted?Boolean

Returns true if the resource is not newly instantiated or recently destroyed.

Returns:

  • (Boolean)


107
108
109
# File 'lib/muve/model.rb', line 107

def persisted?
  !(new_record? || destroyed?)
end

#reloadObject



30
31
32
33
# File 'lib/muve/model.rb', line 30

def reload
  self.send(:populate, extract(adaptor.get(self.class, id), self.class)) if id
  self
end

#saveObject

Save a resource



84
85
86
87
88
89
# File 'lib/muve/model.rb', line 84

def save
  # TODO: be more verbose about the nature of the failure, if any
  (create_or_update if valid?) or false
rescue => e
  false
end

#save!Object

Save a resource and raises an SaveError on failure



76
77
78
79
80
81
# File 'lib/muve/model.rb', line 76

def save!
  raise ValidationError, "validation failed" unless valid?
  create_or_update
rescue => e
  raise SaveError, "Save failed because #{e} was raised"
end

#to_hash(level = 0, limit = 1) ⇒ Object

Returns a hash of the object and subsequently containing objects that respond to #to_hash. In order to avoid circular reference hell you can set the limit.

By default on the the root object and its children a explored. Everything beyond that range is discarded.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/muve/model.rb', line 62

def to_hash(level=0, limit=1)
  hash = {}
  attributes.map { |k, v|
    if v.respond_to? :to_hash
      (hash[k.to_sym] = v.to_hash(level+1, limit)) if level < limit
    else
      #(raise AssocError, "#Associated #{v.class} for #{k} must respond to #to_hash or be a Hash") unless v.kind_of? Hash
      hash[k.to_sym] = v
    end
  }
  hash
end

#to_paramObject

The parameterized identifier of the resource



131
132
133
# File 'lib/muve/model.rb', line 131

def to_param
  id && id.to_s
end

#valid?Boolean

Returns a true if the resource passes all validations

Returns:

  • (Boolean)


117
118
119
# File 'lib/muve/model.rb', line 117

def valid?
  false
end