Class: Waistband::Model
- Inherits:
-
Object
- Object
- Waistband::Model
- Defined in:
- lib/waistband/model.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
/class << self.
Class Method Summary collapse
- .columns(*cols) ⇒ Object
- .create(attributes) ⇒ Object
- .create_index! ⇒ Object
- .defaults(values = {}) ⇒ Object
- .destroy_index! ⇒ Object
- .find(id) ⇒ Object
- .first ⇒ Object
- .index ⇒ Object
- .last ⇒ Object
- .query ⇒ Object
- .query_asc ⇒ Object
- .query_desc ⇒ Object
- .stringify(*cols) ⇒ Object
- .validates(*cols) ⇒ Object
- .with_index(name) ⇒ Object
Instance Method Summary collapse
- #after_save ⇒ Object
- #attributes ⇒ Object
- #before_save ⇒ Object
-
#initialize(attributes = {}) ⇒ Model
constructor
A new instance of Model.
- #read_attribute(attribute) ⇒ Object
- #save ⇒ Object
- #valid? ⇒ Boolean
- #write_attribute(attribute, val) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/waistband/model.rb', line 129 def initialize(attributes = {}) @attributes = (attributes || {}).symbolize_keys @attributes.each do |key, val| if self.class.stringify_columns.include?(key) self.send("#{key}=", val) elsif !self.class.column_names.include?(key) raise ArgumentError.new("#{key} is not a valid column name!") end end self.class.default_values.each do |k, v| self.send("#{k}=", v) if self.send(k).nil? end @errors = ::Waistband::QuickError.new end |
Instance Attribute Details
#errors ⇒ Object (readonly)
/class << self
127 128 129 |
# File 'lib/waistband/model.rb', line 127 def errors @errors end |
Class Method Details
.columns(*cols) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/waistband/model.rb', line 91 def columns(*cols) cols |= [:id, :model_type, :created_at, :updated_at] self.column_names ||= [] self.column_names |= cols.map(&:to_sym) cols.each do |col| # Normal attributes setters and getters class_eval <<-EV, __FILE__, __LINE__ + 1 def #{col} read_attribute(:#{col}) end def #{col}=(val) write_attribute(:#{col}, val) end EV # Relationship type columns: `user_id`, `app_id` to `user`, `app` if col =~ /(.*)_id$/ class_eval <<-EV, __FILE__, __LINE__ + 1 def #{$1} klass = "#{$1}".classify.constantize klass.find(#{col}) end def #{$1}=(val) write_attribute(:#{$1}_id, val.id) end EV end end end |
.create(attributes) ⇒ Object
25 26 27 28 29 |
# File 'lib/waistband/model.rb', line 25 def create(attributes) instance = new(attributes) instance.save instance end |
.create_index! ⇒ Object
65 66 67 |
# File 'lib/waistband/model.rb', line 65 def create_index! Waistband::Index.new(index_name).create! end |
.defaults(values = {}) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/waistband/model.rb', line 78 def defaults(values = {}) self.default_values ||= {} values.each do |k, v| self.default_values = self.default_values.merge({k.to_sym => v}) end end |
.destroy_index! ⇒ Object
69 70 71 |
# File 'lib/waistband/model.rb', line 69 def destroy_index! Waistband::Index.new(index_name).destroy! end |
.find(id) ⇒ Object
19 20 21 22 23 |
# File 'lib/waistband/model.rb', line 19 def find(id) attrs = index.read(id) raise ActiveRecord::RecordNotFound.new("#{self} not found!") unless attrs new attrs end |
.first ⇒ Object
49 50 51 52 53 |
# File 'lib/waistband/model.rb', line 49 def first attrs = query_asc.results.first.try(:source) return new(attrs) if attrs nil end |
.index ⇒ Object
15 16 17 |
# File 'lib/waistband/model.rb', line 15 def index @index ||= Waistband::Index.new(index_name) end |
.last ⇒ Object
55 56 57 58 59 |
# File 'lib/waistband/model.rb', line 55 def last attrs = query_desc.results.first.try(:source) return new(attrs) if attrs nil end |
.query ⇒ Object
31 32 33 34 35 |
# File 'lib/waistband/model.rb', line 31 def query q = index.query('') q.add_term('model_type', name.underscore) q end |
.query_asc ⇒ Object
43 44 45 46 47 |
# File 'lib/waistband/model.rb', line 43 def query_asc q = query q.add_sort 'created_at', 'asc' q end |
.query_desc ⇒ Object
37 38 39 40 41 |
# File 'lib/waistband/model.rb', line 37 def query_desc q = query q.add_sort 'created_at', 'desc' q end |
.stringify(*cols) ⇒ Object
86 87 88 89 |
# File 'lib/waistband/model.rb', line 86 def stringify(*cols) self.stringify_columns ||= [] self.stringify_columns |= cols.map(&:to_sym) end |
.validates(*cols) ⇒ Object
73 74 75 76 |
# File 'lib/waistband/model.rb', line 73 def validates(*cols) self.validate_columns ||= [] self.validate_columns |= cols.map(&:to_sym) end |
.with_index(name) ⇒ Object
61 62 63 |
# File 'lib/waistband/model.rb', line 61 def with_index(name) self.index_name = name.to_s end |
Instance Method Details
#after_save ⇒ Object
178 179 |
# File 'lib/waistband/model.rb', line 178 def after_save end |
#attributes ⇒ Object
189 190 191 |
# File 'lib/waistband/model.rb', line 189 def attributes Hash[self.class.column_names.map{|col| [col, self.send(col)] }] end |
#before_save ⇒ Object
175 176 |
# File 'lib/waistband/model.rb', line 175 def before_save end |
#read_attribute(attribute) ⇒ Object
193 194 195 |
# File 'lib/waistband/model.rb', line 193 def read_attribute(attribute) @attributes[attribute] end |
#save ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/waistband/model.rb', line 147 def save return false unless valid? before_save prev_id = self.id prev_created_at = self.created_at prev_updated_at = self.updated_at self.id ||= generated_id self.created_at ||= (Time.zone || Time).now.to_i self.updated_at = (Time.zone || Time).now.to_i self.model_type = self.class.name.downcase stored_json = JSON.parse store! stored = !!stored_json.try(:[], 'ok') if stored after_save else stored self.id = prev_id self.created_at = prev_created_at self.updated_at = prev_updated_at end stored end |
#valid? ⇒ Boolean
181 182 183 184 185 186 187 |
# File 'lib/waistband/model.rb', line 181 def valid? self.class.validate_columns.each do |col| @errors << "#{col} cannot be nil" if self.send(col).nil? end @errors.empty? end |
#write_attribute(attribute, val) ⇒ Object
197 198 199 200 |
# File 'lib/waistband/model.rb', line 197 def write_attribute(attribute, val) val = val.to_s if self.class.stringify_columns.include?(attribute.to_sym) @attributes[attribute] = val end |