Class: Tempo::Model::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tempo/models/base.rb

Direct Known Subclasses

Composite, Log

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/tempo/models/base.rb', line 116

def initialize options={}
  id_candidate = options[:id]
  if !id_candidate
    @id = self.class.next_id
  elsif self.class.ids.include? id_candidate
    raise IdentityConflictError, "Id #{id_candidate} already exists"
  else
    @id = id_candidate
  end
  self.class.add_id @id
  self.class.add_to_index self
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/tempo/models/base.rb', line 8

def id
  @id
end

Class Method Details

.add_id(id) ⇒ Object



156
157
158
159
160
# File 'lib/tempo/models/base.rb', line 156

def self.add_id id
  @ids ||=[]
  @ids << id
  @ids.sort!
end

.add_to_index(member) ⇒ Object



150
151
152
153
154
# File 'lib/tempo/models/base.rb', line 150

def self.add_to_index member
  @index ||= []
  @index << member
  @index.sort! { |a,b| a.id <=> b.id }
end

.delete(instance) ⇒ Object



109
110
111
112
113
# File 'lib/tempo/models/base.rb', line 109

def delete instance
  id = instance.id
  index.delete( instance )
  ids.delete( id )
end

.fileObject

example: Tempo::Model::Animal -> tempo_animals.yaml



31
32
33
# File 'lib/tempo/models/base.rb', line 31

def file
  FileRecord::Record.model_filename self
end

.find(key, value) ⇒ Object

example: Tempo::Model.find(“id”, 1)



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tempo/models/base.rb', line 89

def find key, value
  key = "@#{key}".to_sym
  matches = []
  index.each do |i|
    stored_value = i.instance_variable_get( key )

    if stored_value.kind_of? String
      if value.kind_of? Regexp
        matches << i if value.match stored_value
      else
        matches << i if stored_value.downcase.include? value.to_s.downcase
      end

    elsif stored_value.kind_of? Integer
      matches << i if stored_value == value.to_i
    end
  end
  matches
end

.find_by_id(id) ⇒ Object

find by id should be exact, so we remove the array wrapper



82
83
84
85
# File 'lib/tempo/models/base.rb', line 82

def find_by_id id
  matches = find "id", id
  match = matches[0]
end

.id_counterObject

Maintain an array of unique ids for the class. Initialize new members with the next numberical id Ids can be assigned on init (for the purpose of reading in records of previous instances). An error will be raised if there is already an instance with that id.



18
19
20
# File 'lib/tempo/models/base.rb', line 18

def id_counter
  @id_counter ||= 1
end

.idsObject



22
23
24
# File 'lib/tempo/models/base.rb', line 22

def ids
  @ids ||= []
end

.increase_id_counterObject



162
163
164
165
# File 'lib/tempo/models/base.rb', line 162

def self.increase_id_counter
  @id_counter ||= 0
  @id_counter = @id_counter.next
end

.indexObject



26
27
28
# File 'lib/tempo/models/base.rb', line 26

def index
  @index ||= []
end

.method_missing(meth, *args, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tempo/models/base.rb', line 43

def method_missing meth, *args, &block

  if meth.to_s =~ /^find_by_(.+)$/
    run_find_by_method($1, *args, &block)

  elsif meth.to_s =~ /^sort_by_(.+)$/
    run_sort_by_method($1, *args, &block)
  else
    super
  end
end

.next_idObject



167
168
169
170
171
172
# File 'lib/tempo/models/base.rb', line 167

def self.next_id
  while ids.include? id_counter
    increase_id_counter
  end
  id_counter
end

.read_from_fileObject



39
40
41
# File 'lib/tempo/models/base.rb', line 39

def read_from_file
  FileRecord::Record.read_model self
end

.run_find_by_method(attrs, *args, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tempo/models/base.rb', line 62

def run_find_by_method attrs, *args, &block
  # Make an array of attribute names
  attrs = attrs.split('_and_')

  attrs_with_args = [attrs, args].transpose

  filtered = index.clone
  attrs_with_args.each do | kv |
    matches = find kv[0], kv[1]

    return matches if matches.empty?
    matches.each do |match|
      matches.delete match unless filtered.include? match
      filtered = matches
    end
  end
  filtered
end

.run_sort_by_method(attribute, args = @index.clone, &block) ⇒ Object



55
56
57
58
59
60
# File 'lib/tempo/models/base.rb', line 55

def run_sort_by_method attribute, args=@index.clone, &block
  attr = "@#{attribute}".to_sym
  args.sort! { |a,b| a.instance_variable_get( attr ) <=> b.instance_variable_get( attr ) }
  return args unless block
  block.call args
end

.save_to_fileObject



35
36
37
# File 'lib/tempo/models/base.rb', line 35

def save_to_file
  FileRecord::Record.save_model self
end

Instance Method Details

#deleteObject



144
145
146
# File 'lib/tempo/models/base.rb', line 144

def delete
  self.class.delete self
end

#freeze_dryObject

record the state of all instance variables as a hash



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/tempo/models/base.rb', line 130

def freeze_dry
  record = {}
  state = instance_variables
  state.each do |attr|
    key = attr[1..-1].to_sym
    val = instance_variable_get attr

    #val = val.to_s if val.kind_of? Time

    record[key] = val
  end
  record
end