Class: ActiveHash::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion
Defined in:
lib/active_hash/base.rb

Direct Known Subclasses

ActiveFile::Base

Defined Under Namespace

Classes: WhereChain

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:



471
472
473
474
475
476
477
478
# File 'lib/active_hash/base.rb', line 471

def initialize(attributes = {})
  attributes.symbolize_keys!
  @attributes = attributes
  attributes.dup.each do |key, value|
    send "#{key}=", value
  end
  yield self if block_given?
end

Class Method Details

.add_default_value(field_name, default_value) ⇒ Object



354
355
356
357
# File 'lib/active_hash/base.rb', line 354

def add_default_value field_name, default_value
  self.default_attributes ||= {}
  self.default_attributes[field_name] = default_value
end

.add_to_record_index(entry) ⇒ Object



158
159
160
# File 'lib/active_hash/base.rb', line 158

def add_to_record_index(entry)
  record_index.merge!(entry)
end

.all(options = {}) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/active_hash/base.rb', line 185

def all(options={})
  if options.has_key?(:conditions)
    where(options[:conditions])
  else
    @records ||= []
  end
end

.auto_assign_fields(array_of_hashes) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/active_hash/base.rb', line 423

def auto_assign_fields(array_of_hashes)
  (array_of_hashes || []).inject([]) do |array, row|
    row.symbolize_keys!
    row.keys.each do |key|
      unless key.to_s == "id"
        array << key
      end
    end
    array
  end.uniq.each do |key|
    field key
  end
end

.base_classObject

Needed for ActiveRecord polymorphic associations



440
441
442
# File 'lib/active_hash/base.rb', line 440

def base_class
  ActiveHash::Base
end

.cache_keyObject



70
71
72
73
74
75
76
# File 'lib/active_hash/base.rb', line 70

def cache_key
  if Object.const_defined?(:ActiveModel)
    model_name.cache_key
  else
    ActiveSupport::Inflector.tableize(self.name).downcase
  end
end

.compute_type(type_name) ⇒ Object



92
93
94
# File 'lib/active_hash/base.rb', line 92

def compute_type(type_name)
  self
end

.configuration_for_custom_finder(finder_name) ⇒ Object



342
343
344
345
346
347
348
349
350
# File 'lib/active_hash/base.rb', line 342

def configuration_for_custom_finder(finder_name)
  if finder_name.to_s.match(/^find_(all_)?by_(.*?)(!)?$/) && !($1 && $3)
    {
      :all? => !!$1,
      :bang? => !!$3,
      :fields => $2.split('_and_')
    }
  end
end

.countObject



238
239
240
# File 'lib/active_hash/base.rb', line 238

def count
  all.length
end

.create(attributes = {}) ⇒ Object Also known as: add



170
171
172
173
174
175
# File 'lib/active_hash/base.rb', line 170

def create(attributes = {})
  record = new(attributes)
  record.save
  mark_dirty
  record
end

.create!(attributes = {}) ⇒ Object



179
180
181
182
183
# File 'lib/active_hash/base.rb', line 179

def create!(attributes = {})
  record = new(attributes)
  record.save!
  record
end

.dataObject



104
105
106
# File 'lib/active_hash/base.rb', line 104

def data
  _data
end

.data=(array_of_hashes) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/active_hash/base.rb', line 108

def data=(array_of_hashes)
  mark_dirty
  @records = nil
  reset_record_index
  self._data = array_of_hashes
  if array_of_hashes
    auto_assign_fields(array_of_hashes)
    array_of_hashes.each do |hash|
      insert new(hash)
    end
  end
end

.define_custom_find_all_method(field_name) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/active_hash/base.rb', line 406

def define_custom_find_all_method(field_name)
  method_name = :"find_all_by_#{field_name}"
  unless singleton_methods.include?(method_name)
    the_meta_class.instance_eval do
      unless singleton_methods.include?(method_name)
        define_method(method_name) do |*args|
          args.extract_options!
          identifier = args[0]
          all.select { |record| record.send(field_name) == identifier }
        end
      end
    end
  end
end

.define_custom_find_method(field_name) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/active_hash/base.rb', line 391

def define_custom_find_method(field_name)
  method_name = :"find_by_#{field_name}"
  unless singleton_methods.include?(method_name)
    the_meta_class.instance_eval do
      define_method(method_name) do |*args|
        args.extract_options!
        identifier = args[0]
        all.detect { |record| record.send(field_name) == identifier }
      end
    end
  end
end

.define_getter_method(field, default_value) ⇒ Object



359
360
361
362
363
364
365
# File 'lib/active_hash/base.rb', line 359

def define_getter_method(field, default_value)
  unless instance_methods.include?(field.to_sym)
    define_method(field) do
      attributes[field].nil? ? default_value : attributes[field]
    end
  end
end

.define_interrogator_method(field) ⇒ Object



380
381
382
383
384
385
386
387
# File 'lib/active_hash/base.rb', line 380

def define_interrogator_method(field)
  method_name = :"#{field}?"
  unless instance_methods.include?(method_name)
    define_method(method_name) do
      send(field).present?
    end
  end
end

.define_setter_method(field) ⇒ Object



369
370
371
372
373
374
375
376
# File 'lib/active_hash/base.rb', line 369

def define_setter_method(field)
  method_name = :"#{field}="
  unless instance_methods.include?(method_name)
    define_method(method_name) do |new_val|
      @attributes[field] = new_val
    end
  end
end

.delete_allObject



256
257
258
259
260
# File 'lib/active_hash/base.rb', line 256

def delete_all
  mark_dirty
  reset_record_index
  @records = []
end

.empty?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/active_hash/base.rb', line 100

def empty?
  false
end

.exists?(record) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
# File 'lib/active_hash/base.rb', line 121

def exists?(record)
  if record.id.present?
    record_index[record.id.to_s].present?
  end
end

.field(field_name, options = {}) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
# File 'lib/active_hash/base.rb', line 293

def field(field_name, options = {})
  validate_field(field_name)
  field_names << field_name

  add_default_value(field_name, options[:default]) if options[:default]
  define_getter_method(field_name, options[:default])
  define_setter_method(field_name)
  define_interrogator_method(field_name)
  define_custom_find_method(field_name)
  define_custom_find_all_method(field_name)
end

.field_namesObject



82
83
84
# File 'lib/active_hash/base.rb', line 82

def field_names
  @field_names ||= []
end

.fields(*args) ⇒ Object



286
287
288
289
290
291
# File 'lib/active_hash/base.rb', line 286

def fields(*args)
  options = args.extract_options!
  args.each do |field|
    field(field, options)
  end
end

.find(id, *args) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/active_hash/base.rb', line 262

def find(id, * args)
  case id
    when nil
      nil
    when :all
      all
    when :first
      all(*args).first
    when Array
      id.map { |i| find(i) }
    else
      find_by_id(id) || begin
        raise RecordNotFound.new("Couldn't find #{name} with ID=#{id}")
      end
  end
end

.find_by(options) ⇒ Object



212
213
214
# File 'lib/active_hash/base.rb', line 212

def find_by(options)
  where(options).first
end

.find_by!(options) ⇒ Object



216
217
218
# File 'lib/active_hash/base.rb', line 216

def find_by!(options)
  find_by(options) || (raise RecordNotFound.new("Couldn't find #{name}"))
end

.find_by_id(id) ⇒ Object



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

def find_by_id(id)
  index = record_index[id.to_s]
  index and @records[index]
end

.insert(record) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/active_hash/base.rb', line 127

def insert(record)
  @records ||= []
  record[:id] ||= next_id
  validate_unique_id(record) if dirty
  mark_dirty

  add_to_record_index({ record.id.to_s => @records.length })
  @records << record
end

.mark_cleanObject



463
464
465
# File 'lib/active_hash/base.rb', line 463

def mark_clean
  self.dirty = false
end

.mark_dirtyObject



457
458
459
# File 'lib/active_hash/base.rb', line 457

def mark_dirty
  self.dirty = true
end

.match_options?(record, options) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
223
224
225
226
227
228
# File 'lib/active_hash/base.rb', line 220

def match_options?(record, options)
  options.all? do |col, match|
    if match.kind_of?(Array)
      match.any? { |v| normalize(v) == normalize(record[col]) }
    else
      normalize(record[col]) == normalize(match)
    end
  end
end

.method_missing(method_name, *args) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/active_hash/base.rb', line 323

def method_missing(method_name, *args)
  return super unless respond_to? method_name

  config = configuration_for_custom_finder(method_name)
  attribute_pairs = config[:fields].zip(args)
  matches = all.select { |base| attribute_pairs.all? { |field, value| base.send(field).to_s == value.to_s } }

  if config[:all?]
    matches
  else
    result = matches.first
    if config[:bang?]
      result || raise(RecordNotFound, "Couldn\'t find #{name} with #{attribute_pairs.collect { |pair| "#{pair[0]} = #{pair[1]}" }.join(', ')}")
    else
      result
    end
  end
end

.next_idObject



137
138
139
140
141
142
143
144
# File 'lib/active_hash/base.rb', line 137

def next_id
  max_record = all.max { |a, b| a.id <=> b.id }
  if max_record.nil?
    1
  elsif max_record.id.is_a?(Numeric)
    max_record.id.succ
  end
end

.normalize(v) ⇒ Object



232
233
234
# File 'lib/active_hash/base.rb', line 232

def normalize(v)
  v.respond_to?(:to_sym) ? v.to_sym : v
end

.pluck(*column_names) ⇒ Object



242
243
244
# File 'lib/active_hash/base.rb', line 242

def pluck(*column_names)
  column_names.map { |column_name| all.map(&column_name.to_sym) }.inject(&:zip)
end

.pluralize_table_namesObject



96
97
98
# File 'lib/active_hash/base.rb', line 96

def pluralize_table_names
  true
end

.polymorphic_nameObject

Needed for ActiveRecord polymorphic associations(rails/rails#32148)



445
446
447
# File 'lib/active_hash/base.rb', line 445

def polymorphic_name
  base_class.name
end

.primary_keyObject



78
79
80
# File 'lib/active_hash/base.rb', line 78

def primary_key
  "id"
end

.record_indexObject



146
147
148
# File 'lib/active_hash/base.rb', line 146

def record_index
  @record_index ||= {}
end

.reloadObject



449
450
451
452
453
# File 'lib/active_hash/base.rb', line 449

def reload
  reset_record_index
  self.data = _data
  mark_clean
end

.reset_record_indexObject



152
153
154
# File 'lib/active_hash/base.rb', line 152

def reset_record_index
  record_index.clear
end

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

Returns:

  • (Boolean)


313
314
315
316
317
318
319
320
321
# File 'lib/active_hash/base.rb', line 313

def respond_to?(method_name, include_private=false)
  super ||
    begin
      config = configuration_for_custom_finder(method_name)
      config && config[:fields].all? do |field|
        field_names.include?(field.to_sym) || field.to_sym == :id
      end
    end
end

.the_meta_classObject



86
87
88
89
90
# File 'lib/active_hash/base.rb', line 86

def the_meta_class
  class << self
    self
  end
end

.transactionObject



246
247
248
249
250
251
252
253
254
# File 'lib/active_hash/base.rb', line 246

def transaction
  yield
rescue LocalJumpError => err
  raise err
rescue StandardError => e
  unless Object.const_defined?(:ActiveRecord) && e.is_a?(ActiveRecord::Rollback)
    raise e
  end
end

.validate_field(field_name) ⇒ Object



305
306
307
308
309
# File 'lib/active_hash/base.rb', line 305

def validate_field(field_name)
  if [:attributes].include?(field_name.to_sym)
    raise ReservedFieldError.new("#{field_name} is a reserved field in ActiveHash.  Please use another name.")
  end
end

.validate_unique_id(record) ⇒ Object

Raises:



164
165
166
# File 'lib/active_hash/base.rb', line 164

def validate_unique_id(record)
  raise IdError.new("Duplicate ID found for record #{record.attributes.inspect}") if record_index.has_key?(record.id.to_s)
end

.where(options = :chain) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/active_hash/base.rb', line 193

def where(options = :chain)
  if options == :chain
    return WhereChain.new(self)
  elsif options.blank?
    return @records
  end

  # use index if searching by id
  if options.key?(:id) || options.key?("id")
    ids = (options.delete(:id) || options.delete("id"))
    candidates = Array.wrap(ids).map { |id| find_by_id(id) }.compact
  end
  return candidates if options.blank?

  (candidates || @records || []).select do |record|
    match_options?(record, options)
  end
end

Instance Method Details

#[](key) ⇒ Object



488
489
490
# File 'lib/active_hash/base.rb', line 488

def [](key)
  attributes[key]
end

#[]=(key, val) ⇒ Object



497
498
499
# File 'lib/active_hash/base.rb', line 497

def []=(key, val)
  @attributes[key] = val
end

#_read_attribute(key) ⇒ Object Also known as: read_attribute



492
493
494
# File 'lib/active_hash/base.rb', line 492

def _read_attribute(key)
  attributes[key]
end

#attributesObject



480
481
482
483
484
485
486
# File 'lib/active_hash/base.rb', line 480

def attributes
  if self.class.default_attributes
    (self.class.default_attributes.merge @attributes).freeze
  else
    @attributes
  end
end

#cache_keyObject



537
538
539
540
541
542
543
544
545
546
# File 'lib/active_hash/base.rb', line 537

def cache_key
  case
    when new_record?
      "#{self.class.cache_key}/new"
    when timestamp = self[:updated_at]
      "#{self.class.cache_key}/#{id}-#{timestamp.to_s(:number)}"
    else
      "#{self.class.cache_key}/#{id}"
  end
end

#destroyed?Boolean

Returns:

  • (Boolean)


515
516
517
# File 'lib/active_hash/base.rb', line 515

def destroyed?
  false
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


527
528
529
# File 'lib/active_hash/base.rb', line 527

def eql?(other)
  other.instance_of?(self.class) and not id.nil? and (id == other.id)
end

#errorsObject



548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/active_hash/base.rb', line 548

def errors
  obj = Object.new

  def obj.[](key)
    []
  end

  def obj.full_messages()
    []
  end

  obj
end

#hashObject



533
534
535
# File 'lib/active_hash/base.rb', line 533

def hash
  id.hash
end

#idObject Also known as: quoted_id



501
502
503
# File 'lib/active_hash/base.rb', line 501

def id
  attributes[:id] ? attributes[:id] : nil
end

#id=(id) ⇒ Object



505
506
507
# File 'lib/active_hash/base.rb', line 505

def id=(id)
  @attributes[:id] = id
end

#marked_for_destruction?Boolean

Returns:

  • (Boolean)


575
576
577
# File 'lib/active_hash/base.rb', line 575

def marked_for_destruction?
  false
end

#new_record?Boolean

Returns:

  • (Boolean)


511
512
513
# File 'lib/active_hash/base.rb', line 511

def new_record?
  !self.class.all.include?(self)
end

#persisted?Boolean

Returns:

  • (Boolean)


519
520
521
# File 'lib/active_hash/base.rb', line 519

def persisted?
  self.class.all.map(&:id).include?(id)
end

#readonly?Boolean

Returns:

  • (Boolean)


523
524
525
# File 'lib/active_hash/base.rb', line 523

def readonly?
  true
end

#save(*args) ⇒ Object Also known as: save!



562
563
564
565
566
567
# File 'lib/active_hash/base.rb', line 562

def save(*args)
  unless self.class.exists?(self)
    self.class.insert(self)
  end
  true
end

#to_paramObject



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

def to_param
  id.present? ? id.to_s : nil
end

#valid?Boolean

Returns:

  • (Boolean)


571
572
573
# File 'lib/active_hash/base.rb', line 571

def valid?
  true
end