Class: ArtisanMemoryRepository::Base
- Inherits:
-
Object
- Object
- ArtisanMemoryRepository::Base
show all
- Defined in:
- lib/artisan-memory-repository/models/base.rb
Constant Summary
collapse
- @@next_id =
1
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
4
5
6
7
8
9
|
# File 'lib/artisan-memory-repository/models/base.rb', line 4
def initialize(options = {})
self.class._collections.each {|field| set(field, [])}
options.each { |key, value| set(key, value) }
set(:id, next_id) if saves?
self.init
end
|
Class Method Details
._collections ⇒ Object
35
36
37
|
# File 'lib/artisan-memory-repository/models/base.rb', line 35
def self._collections
@_collections ||= []
end
|
._fields ⇒ Object
18
19
20
|
# File 'lib/artisan-memory-repository/models/base.rb', line 18
def self._fields
@_fields ||= []
end
|
.boolean(*fields) ⇒ Object
22
23
24
25
26
|
# File 'lib/artisan-memory-repository/models/base.rb', line 22
def self.boolean(*fields)
(fields).each do |field|
define_method("#{field}?".to_sym) { !!get(field) }
end
end
|
.collections(*collections) ⇒ Object
28
29
30
31
32
33
|
# File 'lib/artisan-memory-repository/models/base.rb', line 28
def self.collections(*collections)
@_collections = collections
collections.each do |collection|
attr_accessor collection
end
end
|
.data_attributes(*fields) ⇒ Object
13
14
15
16
|
# File 'lib/artisan-memory-repository/models/base.rb', line 13
def self.data_attributes(*fields)
@_fields = fields
(fields + [:id, :saves]).each {|field| attr_accessor field }
end
|
Instance Method Details
#==(other) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/artisan-memory-repository/models/base.rb', line 68
def ==(other)
return false if other.nil?
return false if other.class != self.class
self.class._fields.each do |attr|
return false if self.send(attr) != other.send(attr)
end
end
|
#attributes ⇒ Object
51
52
53
54
55
56
|
# File 'lib/artisan-memory-repository/models/base.rb', line 51
def attributes
self.class._fields.inject({}) do |attributes, key|
attributes[key] = get(key)
attributes
end
end
|
#get(key) ⇒ Object
43
44
45
|
# File 'lib/artisan-memory-repository/models/base.rb', line 43
def get(key)
instance_variable_get("@#{key}")
end
|
#init ⇒ Object
11
|
# File 'lib/artisan-memory-repository/models/base.rb', line 11
def init; end
|
#saves? ⇒ Boolean
39
40
41
|
# File 'lib/artisan-memory-repository/models/base.rb', line 39
def saves?
get(:saves).nil? ? true : !!get(:saves)
end
|
#set(key, value) ⇒ Object
Also known as:
update_attribute
47
48
49
|
# File 'lib/artisan-memory-repository/models/base.rb', line 47
def set(key, value)
instance_variable_set("@#{key}", value)
end
|
#to_param ⇒ Object
76
77
78
|
# File 'lib/artisan-memory-repository/models/base.rb', line 76
def to_param
id.to_s
end
|
#update_attributes(options = {}) ⇒ Object
Also known as:
update_attributes!
60
61
62
63
64
|
# File 'lib/artisan-memory-repository/models/base.rb', line 60
def update_attributes(options = {})
return false unless saves?
return attributes if options.nil?
options.each { |key, value| set(key, value) }
end
|