Class: ActiveHash::Base

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

Direct Known Subclasses

ActiveFile::Base

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(options = {})
  options.symbolize_keys!
  @attributes = options
  options.each do |key, value|
    send "#{key}=", value
  end
end

Class Attribute Details

.field_namesObject (readonly)

Returns the value of attribute field_names.



9
10
11
# File 'lib/active_hash/base.rb', line 9

def field_names
  @field_names
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



247
248
249
# File 'lib/active_hash/base.rb', line 247

def attributes
  @attributes
end

Class Method Details

.allObject



57
58
59
# File 'lib/active_hash/base.rb', line 57

def all
  @records || []
end

.auto_assign_fields(array_of_hashes) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/active_hash/base.rb', line 205

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



222
223
224
# File 'lib/active_hash/base.rb', line 222

def base_class
  ActiveHash::Base
end

.configuration_for_custom_finder(finder_name) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/active_hash/base.rb', line 134

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

.countObject



61
62
63
# File 'lib/active_hash/base.rb', line 61

def count
  all.length
end

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



43
44
45
46
47
48
# File 'lib/active_hash/base.rb', line 43

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

.create!(attributes = {}) ⇒ Object



51
52
53
54
55
# File 'lib/active_hash/base.rb', line 51

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

.data=(array_of_hashes) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/active_hash/base.rb', line 15

def data=(array_of_hashes)
  mark_dirty
  @records = nil
  write_inheritable_attribute(: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



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/active_hash/base.rb', line 190

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 |name|
          all.select {|record| record.send(field_name) == name }
        end
      end
    end
  end
end

.define_custom_find_method(field_name) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/active_hash/base.rb', line 177

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 |name|
        all.detect {|record| record.send(field_name) == name }
      end
    end
  end
end

.define_getter_method(field, default_value) ⇒ Object



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

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

.define_interrogator_method(field) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/active_hash/base.rb', line 166

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



155
156
157
158
159
160
161
162
# File 'lib/active_hash/base.rb', line 155

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



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

def delete_all
  mark_dirty
  @records = []
end

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



104
105
106
107
108
109
110
111
112
113
# File 'lib/active_hash/base.rb', line 104

def field(field_name, options = {})
  @field_names ||= []
  @field_names << field_name

  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

.fields(*args) ⇒ Object



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

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

.find(id, *args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/active_hash/base.rb', line 76

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

.find_by_id(id) ⇒ Object



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

def find_by_id(id)
  all.detect {|record| record.id == id.to_i}
end

.insert(record) ⇒ Object



27
28
29
30
31
32
# File 'lib/active_hash/base.rb', line 27

def insert(record)
  @records ||= []
  record.attributes[:id] ||= next_id
  mark_dirty
  @records << record
end

.mark_cleanObject



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

def mark_clean
  self.dirty = false
end

.mark_dirtyObject



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

def mark_dirty
  self.dirty = true
end

.method_missing(method_name, *args) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/active_hash/base.rb', line 125

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 } }
  config[:all?] ? matches : matches.first
end

.next_idObject



34
35
36
37
38
39
40
41
# File 'lib/active_hash/base.rb', line 34

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

.reloadObject



226
227
228
229
# File 'lib/active_hash/base.rb', line 226

def reload
  self.data = read_inheritable_attribute(:data)
  mark_clean
end

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

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
# File 'lib/active_hash/base.rb', line 115

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



11
12
13
# File 'lib/active_hash/base.rb', line 11

def the_meta_class
  respond_to?(:singleton_class) ? singleton_class : metaclass
end

.transactionObject



65
66
67
68
69
# File 'lib/active_hash/base.rb', line 65

def transaction
  yield
rescue ActiveRecord::Rollback

end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  attributes[key]
end

#[]=(key, val) ⇒ Object



261
262
263
# File 'lib/active_hash/base.rb', line 261

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

#destroyed?Boolean

Returns:

  • (Boolean)


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

def destroyed?
  false
end

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

Returns:

  • (Boolean)


291
292
293
# File 'lib/active_hash/base.rb', line 291

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

#hashObject



297
298
299
# File 'lib/active_hash/base.rb', line 297

def hash
  id.hash
end

#idObject Also known as: quoted_id



265
266
267
# File 'lib/active_hash/base.rb', line 265

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

#id=(id) ⇒ Object



269
270
271
# File 'lib/active_hash/base.rb', line 269

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

#marked_for_destruction?Boolean

Returns:

  • (Boolean)


312
313
314
# File 'lib/active_hash/base.rb', line 312

def marked_for_destruction?
  false
end

#new_record?Boolean

Returns:

  • (Boolean)


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

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

#readonly?Boolean

Returns:

  • (Boolean)


283
284
285
# File 'lib/active_hash/base.rb', line 283

def readonly?
  true
end

#saveObject Also known as: save!



301
302
303
304
# File 'lib/active_hash/base.rb', line 301

def save
  self.class.insert(self)
  true
end

#to_paramObject



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

def to_param
  id.to_s
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  true
end