Class: ActiveHash::Base
- Inherits:
-
Object
- Object
- ActiveHash::Base
- Defined in:
- lib/active_hash/base.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.field_names ⇒ Object
readonly
Returns the value of attribute field_names.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
- .all ⇒ Object
- .auto_assign_fields(array_of_hashes) ⇒ Object
-
.base_class ⇒ Object
Needed for ActiveRecord polymorphic associations.
- .configuration_for_custom_finder(finder_name) ⇒ Object
- .count ⇒ Object
- .create(attributes = {}) ⇒ Object (also: add)
- .create!(attributes = {}) ⇒ Object
- .data=(array_of_hashes) ⇒ Object
- .define_custom_find_all_method(field_name) ⇒ Object
- .define_custom_find_method(field_name) ⇒ Object
- .define_getter_method(field, default_value) ⇒ Object
- .define_interrogator_method(field) ⇒ Object
- .define_setter_method(field) ⇒ Object
- .delete_all ⇒ Object
- .field(field_name, options = {}) ⇒ Object
- .fields(*args) ⇒ Object
- .find(id, *args) ⇒ Object
- .find_by_id(id) ⇒ Object
- .insert(record) ⇒ Object
- .method_missing(method_name, *args) ⇒ Object
- .next_id ⇒ Object
- .reload ⇒ Object
- .respond_to?(method_name, include_private = false) ⇒ Boolean
- .transaction ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #destroyed? ⇒ Boolean
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
- #id ⇒ Object (also: #quoted_id)
- #id=(id) ⇒ Object
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
- #marked_for_destruction? ⇒ Boolean
- #new_record? ⇒ Boolean
- #readonly? ⇒ Boolean
- #save ⇒ Object (also: #save!)
- #to_param ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
228 229 230 231 232 233 234 |
# File 'lib/active_hash/base.rb', line 228 def initialize( = {}) .symbolize_keys! @attributes = .each do |key, value| send "#{key}=", value end end |
Class Attribute Details
.field_names ⇒ Object (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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
226 227 228 |
# File 'lib/active_hash/base.rb', line 226 def attributes @attributes end |
Class Method Details
.all ⇒ Object
50 51 52 |
# File 'lib/active_hash/base.rb', line 50 def all @records || [] end |
.auto_assign_fields(array_of_hashes) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/active_hash/base.rb', line 197 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_class ⇒ Object
Needed for ActiveRecord polymorphic associations
214 215 216 |
# File 'lib/active_hash/base.rb', line 214 def base_class ActiveHash::Base end |
.configuration_for_custom_finder(finder_name) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/active_hash/base.rb', line 126 def configuration_for_custom_finder(finder_name) if finder_name.to_s.match(/^find_(all_)?by_(.*)/) { :all? => !!$1, :fields => $2.split('_and_') } end end |
.count ⇒ Object
54 55 56 |
# File 'lib/active_hash/base.rb', line 54 def count all.length end |
.create(attributes = {}) ⇒ Object Also known as: add
37 38 39 40 41 |
# File 'lib/active_hash/base.rb', line 37 def create(attributes = {}) record = new(attributes) record.save record end |
.create!(attributes = {}) ⇒ Object
44 45 46 47 48 |
# File 'lib/active_hash/base.rb', line 44 def create!(attributes = {}) record = new(attributes) record.save! record end |
.data=(array_of_hashes) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/active_hash/base.rb', line 11 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
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/active_hash/base.rb', line 182 def define_custom_find_all_method(field_name) method_name = "find_all_by_#{field_name}" unless singleton_methods.include?(method_name) .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
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/active_hash/base.rb', line 169 def define_custom_find_method(field_name) method_name = "find_by_#{field_name}" unless singleton_methods.include?(method_name) .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
137 138 139 140 141 142 143 |
# File 'lib/active_hash/base.rb', line 137 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
158 159 160 161 162 163 164 165 |
# File 'lib/active_hash/base.rb', line 158 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
147 148 149 150 151 152 153 154 |
# File 'lib/active_hash/base.rb', line 147 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_all ⇒ Object
64 65 66 |
# File 'lib/active_hash/base.rb', line 64 def delete_all @records = [] end |
.field(field_name, options = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/active_hash/base.rb', line 96 def field(field_name, = {}) @field_names ||= [] @field_names << field_name define_getter_method(field_name, [: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
89 90 91 92 93 94 |
# File 'lib/active_hash/base.rb', line 89 def fields(*args) = args. args.each do |field| field(field, ) end end |
.find(id, *args) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/active_hash/base.rb', line 68 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
83 84 85 |
# File 'lib/active_hash/base.rb', line 83 def find_by_id(id) all.detect {|record| record.id == id.to_i} end |
.insert(record) ⇒ Object
22 23 24 25 26 |
# File 'lib/active_hash/base.rb', line 22 def insert(record) @records ||= [] record.attributes[:id] ||= next_id @records << record end |
.method_missing(method_name, *args) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/active_hash/base.rb', line 117 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_id ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/active_hash/base.rb', line 28 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 |
.reload ⇒ Object
218 219 220 |
# File 'lib/active_hash/base.rb', line 218 def reload self.data = read_inheritable_attribute(:data) end |
.respond_to?(method_name, include_private = false) ⇒ Boolean
107 108 109 110 111 112 113 114 115 |
# File 'lib/active_hash/base.rb', line 107 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 |
.transaction ⇒ Object
58 59 60 61 62 |
# File 'lib/active_hash/base.rb', line 58 def transaction yield rescue ActiveRecord::Rollback end |
Instance Method Details
#[](key) ⇒ Object
236 237 238 |
# File 'lib/active_hash/base.rb', line 236 def [](key) attributes[key] end |
#[]=(key, val) ⇒ Object
240 241 242 |
# File 'lib/active_hash/base.rb', line 240 def []=(key, val) attributes[key] = val end |
#destroyed? ⇒ Boolean
258 259 260 |
# File 'lib/active_hash/base.rb', line 258 def destroyed? false end |
#eql?(other) ⇒ Boolean Also known as: ==
270 271 272 |
# File 'lib/active_hash/base.rb', line 270 def eql?(other) other.instance_of?(self.class) and not id.nil? and (id == other.id) end |
#hash ⇒ Object
276 277 278 |
# File 'lib/active_hash/base.rb', line 276 def hash id.hash end |
#id ⇒ Object Also known as: quoted_id
244 245 246 |
# File 'lib/active_hash/base.rb', line 244 def id attributes[:id] ? attributes[:id] : nil end |
#id=(id) ⇒ Object
248 249 250 |
# File 'lib/active_hash/base.rb', line 248 def id=(id) attributes[:id] = id end |
#marked_for_destruction? ⇒ Boolean
291 292 293 |
# File 'lib/active_hash/base.rb', line 291 def marked_for_destruction? false end |
#new_record? ⇒ Boolean
254 255 256 |
# File 'lib/active_hash/base.rb', line 254 def new_record? ! self.class.all.include?(self) end |
#readonly? ⇒ Boolean
262 263 264 |
# File 'lib/active_hash/base.rb', line 262 def readonly? true end |
#save ⇒ Object Also known as: save!
280 281 282 283 |
# File 'lib/active_hash/base.rb', line 280 def save self.class.insert(self) true end |
#to_param ⇒ Object
266 267 268 |
# File 'lib/active_hash/base.rb', line 266 def to_param id.to_s end |
#valid? ⇒ Boolean
287 288 289 |
# File 'lib/active_hash/base.rb', line 287 def valid? true end |