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

class methods



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/tempo/models/base.rb', line 153

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.



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

def id
  @id
end

Class Method Details

.delete(instance) ⇒ Object



146
147
148
149
150
# File 'lib/tempo/models/base.rb', line 146

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

.fileObject

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



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

def file
  FileRecord::FileUtility.new(self).filename
end

.find(key, value) ⇒ Object

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



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

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



119
120
121
122
# File 'lib/tempo/models/base.rb', line 119

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.



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

def id_counter
  @id_counter ||= 1
end

.idsObject



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

def ids
  @ids ||= []
end

.indexObject



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

def index
  @index ||= []
end

.instances_have_attributes?(attrs) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
# File 'lib/tempo/models/base.rb', line 84

def instances_have_attributes?(attrs)
    finder_attrs = attrs.map { |key| "@#{key}".to_sym }
    model_attrs = index.map { |i| i.instance_variables }.flatten.uniq

    # Make sure all attributes are present in index objects
    (model_attrs & finder_attrs).size == finder_attrs.size
end

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tempo/models/base.rb', line 54

def method_missing(meth, *args, &block)
  if respond_to? meth
    if meth.to_s =~ /^find_by_(.+)$/
      self.class.send(:define_method, meth) do |*args|
        run_find_by_method($1, *args)
      end
      self.send(meth, *args)

    elsif meth.to_s =~ /^sort_by_(.+)$/
      self.class.send(:define_method, meth) do |*args, &block|
        run_sort_by_method($1, *args, &block)
      end
      self.send(meth, *args, &block)

    else
      super
    end
  else
    super
  end
end

.read_from_file(options = {}) ⇒ Object

pass custom directory through in options



50
51
52
# File 'lib/tempo/models/base.rb', line 50

def read_from_file(options={})
  FileRecord::Record.read_model self, options
end

.respond_to?(method_id, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/tempo/models/base.rb', line 76

def respond_to?(method_id, include_private = false)
  if method_id.to_s =~ /^find_by_(.+)$/ || method_id.to_s =~ /^sort_by_(.+)$/
    instances_have_attributes? $1.split('_and_')
  else
    super
  end
end

.run_find_by_method(attrs, *args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tempo/models/base.rb', line 99

def run_find_by_method(attrs, *args)
  # 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



92
93
94
95
96
97
# File 'lib/tempo/models/base.rb', line 92

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_file(options = {}) ⇒ Object

pass custom directory through in options



45
46
47
# File 'lib/tempo/models/base.rb', line 45

def save_to_file(options={})
  FileRecord::Record.save_model self, options
end

Instance Method Details

#deleteObject



178
179
180
# File 'lib/tempo/models/base.rb', line 178

def delete
  self.class.delete self
end

#freeze_dryObject

record the state of all instance variables as a hash



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

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