Class: ArtisanMemoryRepository::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/artisan-memory-repository/models/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



3
4
5
6
7
8
# File 'lib/artisan-memory-repository/models/base.rb', line 3

def initialize(options = {})
  self.class._collections.each {|field| set(field, [])}
  options.each { |key, value| set(key, value) }
  set(:id, random_int) if saves?
  self.init
end

Class Method Details

._collectionsObject



34
35
36
# File 'lib/artisan-memory-repository/models/base.rb', line 34

def self._collections
  @_collections ||= []
end

._fieldsObject



17
18
19
# File 'lib/artisan-memory-repository/models/base.rb', line 17

def self._fields
  @_fields ||= []
end

.boolean(*fields) ⇒ Object



21
22
23
24
25
# File 'lib/artisan-memory-repository/models/base.rb', line 21

def self.boolean(*fields)
  (fields).each do |field|
    define_method("#{field}?".to_sym) { !!get(field) }
  end
end

.collections(*collections) ⇒ Object



27
28
29
30
31
32
# File 'lib/artisan-memory-repository/models/base.rb', line 27

def self.collections(*collections)
  @_collections = collections
  collections.each do |collection|
    attr_accessor collection
  end
end

.data_attributes(*fields) ⇒ Object



12
13
14
15
# File 'lib/artisan-memory-repository/models/base.rb', line 12

def self.data_attributes(*fields)
  @_fields = fields
  (fields + [:id, :saves]).each {|field| attr_accessor field }
end

Instance Method Details

#==(other) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/artisan-memory-repository/models/base.rb', line 67

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

#attributesObject



50
51
52
53
54
55
# File 'lib/artisan-memory-repository/models/base.rb', line 50

def attributes
  self.class._fields.inject({}) do |attributes, key|
    attributes[key] = get(key)
    attributes
  end
end

#get(key) ⇒ Object



42
43
44
# File 'lib/artisan-memory-repository/models/base.rb', line 42

def get(key)
  instance_variable_get("@#{key}")
end

#initObject



10
# File 'lib/artisan-memory-repository/models/base.rb', line 10

def init; end

#saves?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/artisan-memory-repository/models/base.rb', line 38

def saves?
  get(:saves).nil? ? true : !!get(:saves)
end

#set(key, value) ⇒ Object Also known as: update_attribute



46
47
48
# File 'lib/artisan-memory-repository/models/base.rb', line 46

def set(key, value)
  instance_variable_set("@#{key}", value)
end

#to_paramObject



75
76
77
# File 'lib/artisan-memory-repository/models/base.rb', line 75

def to_param
  id.to_s
end

#update_attributes(options = {}) ⇒ Object Also known as: update_attributes!



59
60
61
62
63
# File 'lib/artisan-memory-repository/models/base.rb', line 59

def update_attributes(options = {})
  return false unless saves?
  return attributes if options.nil?
  options.each { |key, value| set(key, value) }
end