Class: Squares::Base

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/squares/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



5
6
7
8
# File 'lib/squares/base.rb', line 5

def initialize *args
  apply *args
  trigger :after_initialize
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/squares/base.rb', line 3

def id
  @id
end

Class Method Details

.[](id) ⇒ Object Also known as: find



144
145
146
147
148
149
150
# File 'lib/squares/base.rb', line 144

def [] id
  if item = store[id]
    Marshal.restore(item).tap do |item|
      item.instance_eval 'trigger :after_find'
    end
  end
end

.[]=(*args) ⇒ Object Also known as: create



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/squares/base.rb', line 153

def []= *args
  id, instance = *args
  if instance.class == self
    instance.id = id
  elsif instance.respond_to?(:to_h)
    instance = self.new(id, instance)
  else
    raise ArgumentError.new(<<-ERR)
    You must provide an instance of #{self.name} or at least
    something which responds to #to_h"
    ERR
  end
  instance.tap do |i|
    i.instance_eval 'trigger :before_create'
    i.save
    i.instance_eval 'trigger :after_create'
  end
end

.after_create(*args, &block) ⇒ Object



288
289
290
# File 'lib/squares/base.rb', line 288

def after_create *args, &block
  add_hook :after_create, args, block
end

.after_find(*args, &block) ⇒ Object



296
297
298
# File 'lib/squares/base.rb', line 296

def after_find *args, &block
  add_hook :after_find, args, block
end

.after_initialize(*args, &block) ⇒ Object



292
293
294
# File 'lib/squares/base.rb', line 292

def after_initialize *args, &block
  add_hook :after_initialize, args, block
end

.after_save(*args, &block) ⇒ Object



304
305
306
# File 'lib/squares/base.rb', line 304

def after_save *args, &block
  add_hook :after_save, args, block
end

.before_create(*args, &block) ⇒ Object



284
285
286
# File 'lib/squares/base.rb', line 284

def before_create *args, &block
  add_hook :before_create, args, block
end

.before_destroy(*args, &block) ⇒ Object



308
309
310
# File 'lib/squares/base.rb', line 308

def before_destroy *args, &block
  add_hook :before_destroy, args, block
end

.before_save(*args, &block) ⇒ Object



300
301
302
# File 'lib/squares/base.rb', line 300

def before_save *args, &block
  add_hook :before_save, args, block
end

.defaultsObject



256
257
258
# File 'lib/squares/base.rb', line 256

def defaults
  @_defaults ||= {}
end

.delete(id) ⇒ Object



199
200
201
# File 'lib/squares/base.rb', line 199

def delete id
  store.delete id
end

.each(&block) ⇒ Object



203
204
205
# File 'lib/squares/base.rb', line 203

def each &block
  values.each &block
end

.has_key?(key) ⇒ Boolean Also known as: key?, member?, includes?

Returns:

  • (Boolean)


173
174
175
# File 'lib/squares/base.rb', line 173

def has_key? key
  store.has_key? key
end

.hooksObject

hooks



280
281
282
# File 'lib/squares/base.rb', line 280

def hooks
  @_hooks
end

.keysObject



180
181
182
# File 'lib/squares/base.rb', line 180

def keys
  store.keys
end

.modelsObject



275
276
277
# File 'lib/squares/base.rb', line 275

def models
  @_models.uniq.sort { |a,b| a.to_s <=> b.to_s }
end

.normalize_property(property) ⇒ Object



192
193
194
195
196
197
# File 'lib/squares/base.rb', line 192

def normalize_property property
  unless properties.include?(property)
    property = "#{property}?".to_sym if properties.include?("#{property}?".to_sym)
  end
  properties.include?(property) && property
end

.properties(*props) ⇒ Object



249
250
251
252
253
254
# File 'lib/squares/base.rb', line 249

def properties *props
  props.each do |prop|
    property prop, {}
  end
  @_properties
end

.property(prop, opts = {}) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/squares/base.rb', line 234

def property prop, opts={}
  @_properties ||= []
  @_properties << prop
  uniquify_properties
  define_method prop do
    get_property prop
  end
  define_method "#{prop.to_s.gsub(/\?$/, '')}=" do |v|
    set_property prop, v
  end
  if opts.has_key?(:default)
    defaults[prop] = opts[:default]
  end
end

.storeObject



271
272
273
# File 'lib/squares/base.rb', line 271

def store
  @store ||= {}
end

.store=(storage) ⇒ Object



267
268
269
# File 'lib/squares/base.rb', line 267

def store= storage
  @store = storage
end

.underscore_nameObject



260
261
262
263
264
265
# File 'lib/squares/base.rb', line 260

def underscore_name
  self.name.
    gsub(/::/, '/').
    gsub(/([^\/])([A-Z])/, '\1_\2').
    downcase
end

.valid_property?(property) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/squares/base.rb', line 188

def valid_property? property
  !!normalize_property(property)
end

.valuesObject



184
185
186
# File 'lib/squares/base.rb', line 184

def values
  store.values.map{ |i| Marshal.restore i }
end

.where(*args, &block) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/squares/base.rb', line 207

def where(*args, &block)
  result_set = []
  if block
    result_set = values.select(&block)
    return result_set if result_set.empty?
  end

  args.each do |arg|
    if arg.kind_of?(Hash)
      result_set = (!result_set.empty? ? result_set : values).reject do |i|
        failed_matches = 0
        arg.each do |k,v|
          raise ArgumentError.new("\"#{k}\" is not a valid property of #{self}") unless valid_property?(k)
          failed_matches += 1 unless i[k] == v
        end
        failed_matches > 0
      end
    elsif arg.kind_of?(Symbol)
      raise ArgumentError.new("\"#{arg}\" is not a valid property of #{self}") unless valid_property?(arg)
      result_set = (result_set.empty? ? values : result_set).select do |i|
        i[arg]
      end
    end
  end
  result_set
end

Instance Method Details

#==(other) ⇒ Object



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

def == other
  @id == other.id && properties_equal(other)
end

#[](key) ⇒ Object



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

def [](key)
  get_property key
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  set_property key, value
end

#changed?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/squares/base.rb', line 47

def changed?
  @_changed
end

#defaultsObject



67
68
69
# File 'lib/squares/base.rb', line 67

def defaults
  self.class.defaults
end

#deleteObject



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

def delete
  self.class.delete self.id
end

#destroyObject



22
23
24
25
# File 'lib/squares/base.rb', line 22

def destroy
  trigger :before_destroy
  delete
end

#propertiesObject



51
52
53
# File 'lib/squares/base.rb', line 51

def properties
  self.class.properties
end

#saveObject



10
11
12
13
14
15
16
# File 'lib/squares/base.rb', line 10

def save
  trigger :before_save
  @_changed = false
  store[@id] = Marshal.dump self.dup
  trigger :after_save
  nil
end

#to_h(key_name = :id) ⇒ Object



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

def to_h(key_name = :id)
  h = { key_name => id }
  properties.each do |property|
    h[property] = self.send(property)
  end
  h
end

#update_properties(new_properties) ⇒ Object Also known as: update_attributes



39
40
41
42
43
44
# File 'lib/squares/base.rb', line 39

def update_properties new_properties
  new_properties.each do |key, value|
    self[key] = value if valid_property?(key)
  end
  save
end

#valid_property?(property) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/squares/base.rb', line 63

def valid_property? property
  self.class.valid_property? property
end