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
- .configuration_for_custom_finder(finder_name) ⇒ Object
- .count ⇒ 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
- .field(field_name, options = {}) ⇒ Object
- .fields(*args) ⇒ Object
- .find(id, *args) ⇒ Object
- .find_by_id(id) ⇒ Object
- .method_missing(method_name, *args) ⇒ Object
- .respond_to?(method_name) ⇒ Boolean
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
- #id ⇒ Object (also: #quoted_id)
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
- #new_record? ⇒ Boolean
- #readonly? ⇒ Boolean
- #to_param ⇒ Object
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( = {}) .symbolize_keys! @attributes = end |
Class Attribute Details
.field_names ⇒ Object (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
#attributes ⇒ Object (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
.all ⇒ Object
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 |
.count ⇒ Object
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) .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) .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, = {}) @field_names ||= [] @field_names << field_name define_getter_method(field_name, [: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) = args. args.each do |field| field(field, ) 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
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: ==
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 |
#hash ⇒ Object
183 184 185 |
# File 'lib/active_hash/base.rb', line 183 def hash id.hash end |
#id ⇒ Object 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
165 166 167 |
# File 'lib/active_hash/base.rb', line 165 def new_record? false end |
#readonly? ⇒ Boolean
169 170 171 |
# File 'lib/active_hash/base.rb', line 169 def readonly? true end |
#to_param ⇒ Object
173 174 175 |
# File 'lib/active_hash/base.rb', line 173 def to_param id.to_s end |