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.



213
214
215
216
217
218
219
# File 'lib/active_hash/base.rb', line 213

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.



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

def field_names
  @field_names
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

Class Method Details

.allObject



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

def all
  @records || []
end

.auto_assign_fields(array_of_hashes) ⇒ Object



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

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



205
206
207
# File 'lib/active_hash/base.rb', line 205

def base_class
  ActiveHash::Base
end

.configuration_for_custom_finder(finder_name) ⇒ Object



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

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



49
50
51
# File 'lib/active_hash/base.rb', line 49

def count
  all.length
end

.create(attributes = {}) ⇒ Object



33
34
35
36
37
# File 'lib/active_hash/base.rb', line 33

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

.create!(attributes = {}) ⇒ Object



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

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

.data=(array_of_hashes) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/active_hash/base.rb', line 7

def data=(array_of_hashes)
  @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



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/active_hash/base.rb', line 173

def define_custom_find_all_method(field_name)
  method_name = "find_all_by_#{field_name}"
  unless singleton_methods.include?(method_name)
    metaclass.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



160
161
162
163
164
165
166
167
168
169
# File 'lib/active_hash/base.rb', line 160

def define_custom_find_method(field_name)
  method_name = "find_by_#{field_name}"
  unless singleton_methods.include?(method_name)
    metaclass.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



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

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



149
150
151
152
153
154
155
156
# File 'lib/active_hash/base.rb', line 149

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



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

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



59
60
61
# File 'lib/active_hash/base.rb', line 59

def delete_all
  @records = []
end

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



87
88
89
90
91
92
93
94
95
96
# File 'lib/active_hash/base.rb', line 87

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



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

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

.find(id, *args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/active_hash/base.rb', line 63

def find(id, *args)
  case id
    when :all
      all
    when Array
      all.select {|record| id.map(&:to_i).include?(record.id) }
    else
      find_by_id(id)
  end
end

.find_by_id(id) ⇒ Object



74
75
76
# File 'lib/active_hash/base.rb', line 74

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

.insert(record) ⇒ Object



18
19
20
21
22
# File 'lib/active_hash/base.rb', line 18

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

.method_missing(method_name, *args) ⇒ Object



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

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



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

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

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

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
# File 'lib/active_hash/base.rb', line 98

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

.transactionObject



53
54
55
56
57
# File 'lib/active_hash/base.rb', line 53

def transaction
  yield
rescue ActiveRecord::Rollback

end

Instance Method Details

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

Returns:

  • (Boolean)


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

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

#hashObject



249
250
251
# File 'lib/active_hash/base.rb', line 249

def hash
  id.hash
end

#idObject Also known as: quoted_id



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

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

#id=(id) ⇒ Object



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

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

#new_record?Boolean

Returns:

  • (Boolean)


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

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

#readonly?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/active_hash/base.rb', line 235

def readonly?
  true
end

#saveObject Also known as: save!



253
254
255
256
# File 'lib/active_hash/base.rb', line 253

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

#to_paramObject



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

def to_param
  id.to_s
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  true
end