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.



154
155
156
157
# File 'lib/active_hash/base.rb', line 154

def initialize(options = {})
  options.symbolize_keys!
  @attributes = options
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.



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

def attributes
  @attributes
end

Class Method Details

.allObject



12
13
14
15
16
17
18
19
# File 'lib/active_hash/base.rb', line 12

def all
  unless @records
    records = read_inheritable_attribute(:data) || []
    @records = records.collect {|hash| new(hash)}
    auto_assign_fields( records )
  end
  @records
end

.auto_assign_fields(array_of_hashes) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/active_hash/base.rb', line 134

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

.configuration_for_custom_finder(finder_name) ⇒ Object



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

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



21
22
23
# File 'lib/active_hash/base.rb', line 21

def count
  all.length
end

.data=(array_of_hashes) ⇒ Object



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

def data=(array_of_hashes)
  @records = nil
  write_inheritable_attribute(:data, array_of_hashes)
end

.define_custom_find_all_method(field_name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/active_hash/base.rb', line 119

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



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

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



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

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

.define_interrogator_method(field) ⇒ Object



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

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

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



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

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

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

.fields(*args) ⇒ Object



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

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

.find(id, *args) ⇒ Object



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

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



36
37
38
# File 'lib/active_hash/base.rb', line 36

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

.method_missing(method_name, *args) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/active_hash/base.rb', line 67

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

.respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
# File 'lib/active_hash/base.rb', line 59

def respond_to?(method_name)
  super ||
    begin
      config = configuration_for_custom_finder(method_name)
      config && config[:fields].all? { |field| field_names.include?(field.to_sym) }
    end
end

Instance Method Details

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

Returns:

  • (Boolean)


177
178
179
# File 'lib/active_hash/base.rb', line 177

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

#hashObject



183
184
185
# File 'lib/active_hash/base.rb', line 183

def hash
  id.hash
end

#idObject Also known as: quoted_id



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

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

#new_record?Boolean

Returns:

  • (Boolean)


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

def new_record?
  false
end

#readonly?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/active_hash/base.rb', line 169

def readonly?
  true
end

#to_paramObject



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

def to_param
  id.to_s
end