Module: Groovy::Model::ClassMethods
- Extended by:
- Forwardable
- Defined in:
- lib/groovy/model.rb
Instance Attribute Summary collapse
-
#context_name ⇒ Object
Returns the value of attribute context_name.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
- #add_accessors_for(col, s = schema) ⇒ Object
- #add_column(name, type, options = {}) ⇒ Object
- #add_reference(name, table_name, options = {}) ⇒ Object
- #all ⇒ Object
- #attribute_names ⇒ Object
- #callbacks ⇒ Object
- #create(attributes, key = nil) ⇒ Object
- #create!(attributes, key = nil) ⇒ Object
- #delete_all ⇒ Object
-
#find(id) ⇒ Object
def find_records(&block) records = table.select(&block) records.map do |r| find_and_init_record(r.attributes[‘_id’]) end end.
-
#index_search(column, query, options = {}, &block) ⇒ Object
def column(name) #table_name.#name"">Groonga # .search, .similar_search, etc end.
-
#insert(attributes, key = nil) ⇒ Object
called from instance too, so must by public.
- #load_schema(options = {}, &block) ⇒ Object
-
#new_from_record(record) ⇒ Object
called from Query, so needs to be public.
- #query ⇒ Object
- #render_results(results, &block) ⇒ Object
-
#schema(options = {}, &block) ⇒ Object
def plural_refs schema.plural_references end.
- #scope(name, obj) ⇒ Object
- #scopes ⇒ Object
- #set_timestamp(obj, key_name) ⇒ Object
- #similar_search(column, query, options = {}, &block) ⇒ Object
- #table ⇒ Object
- #underscore_name ⇒ Object
- #unique_values_for(column, limit: -1,, cache: false) ⇒ Object
- #update_all(attrs) ⇒ Object
- #validatable! ⇒ Object
- #where(*args, &block) ⇒ Object
Instance Attribute Details
#context_name ⇒ Object
Returns the value of attribute context_name.
54 55 56 |
# File 'lib/groovy/model.rb', line 54 def context_name @context_name end |
#table_name ⇒ Object
Returns the value of attribute table_name.
54 55 56 |
# File 'lib/groovy/model.rb', line 54 def table_name @table_name end |
Instance Method Details
#add_accessors_for(col, s = schema) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/groovy/model.rb', line 113 def add_accessors_for(col, s = schema) if s.attribute_columns.include?(col) add_attr_accessors(col) elsif s.singular_references.include?(col) add_ref_accessors(col) elsif s.plural_references.include?(col) add_vector_accessors(col) else puts "WARNING: Unknown column type: #{col}" end end |
#add_column(name, type, options = {}) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/groovy/model.rb', line 100 def add_column(name, type, = {}) @attribute_names = nil # ensure cache is cleared schema.column(name, type, ) schema.sync add_accessors_for(name) end |
#add_reference(name, table_name, options = {}) ⇒ Object
107 108 109 110 111 |
# File 'lib/groovy/model.rb', line 107 def add_reference(name, table_name, = {}) schema.reference(name, table_name, ) schema.sync add_accessors_for(name) end |
#all ⇒ Object
209 210 211 |
# File 'lib/groovy/model.rb', line 209 def all query end |
#attribute_names ⇒ Object
69 70 71 |
# File 'lib/groovy/model.rb', line 69 def attribute_names @attribute_names ||= schema.attribute_columns end |
#callbacks ⇒ Object
265 266 267 |
# File 'lib/groovy/model.rb', line 265 def callbacks @callbacks ||= {} end |
#create(attributes, key = nil) ⇒ Object
150 151 152 153 |
# File 'lib/groovy/model.rb', line 150 def create(attributes, key = nil) obj = new(attributes, nil, key) obj.save ? obj : false end |
#create!(attributes, key = nil) ⇒ Object
155 156 157 |
# File 'lib/groovy/model.rb', line 155 def create!(attributes, key = nil) create(attributes, key) or raise Error, "Invalid" end |
#delete_all ⇒ Object
163 164 165 166 |
# File 'lib/groovy/model.rb', line 163 def delete_all # all.each { |child| child.delete } table.delete { |record| record._id > -1 } end |
#find(id) ⇒ Object
def find_records(&block)
records = table.select(&block)
records.map do |r|
find_and_init_record(r.attributes['_key']['_id'])
end
end
144 145 146 147 148 |
# File 'lib/groovy/model.rb', line 144 def find(id) if record = table[id.to_i] and record.record_id new_from_record(record) end end |
#index_search(column, query, options = {}, &block) ⇒ Object
def column(name)
Groonga["#{table_name}.#{name}"] # .search, .similar_search, etc
end
177 178 179 180 |
# File 'lib/groovy/model.rb', line 177 def index_search(column, query, = {}, &block) results = table.select { |rec| rec[column].match(query) } render_results(results, &block) end |
#insert(attributes, key = nil) ⇒ Object
called from instance too, so must by public
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/groovy/model.rb', line 242 def insert(attributes, key = nil) (attributes, :created_at) (attributes, :updated_at) # remove nil attributes for integer columns, otherwise # we get a TypeError (no implicit conversion from nil to integer) attributes.each do |k, v| attributes.delete(k) if v.nil? # && schema.integer_columns.include?(k) end if table.support_key? raise "Key required" if key.nil? table.add(key, attributes) else raise "Key present, but unsupported" if key table.add(attributes) end end |
#load_schema(options = {}, &block) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/groovy/model.rb', line 85 def load_schema( = {}, &block) @schema = begin self.context_name = [:context] || Groovy.first_context_name self.table_name = [:table_name] if [:table_name] s = Schema.new(db_context, table_name, {}) yield s if block s.sync extend(PatriciaTrieMethods) if table.is_a?(Groonga::PatriciaTrie) s.column_names.each { |col| add_accessors_for(col, s) } s end end |
#new_from_record(record) ⇒ Object
called from Query, so needs to be public
126 127 128 129 |
# File 'lib/groovy/model.rb', line 126 def new_from_record(record) # new(record.attributes, record) new(nil, record) end |
#query ⇒ Object
213 214 215 |
# File 'lib/groovy/model.rb', line 213 def query query_class.new(self, table) end |
#render_results(results, &block) ⇒ Object
192 193 194 195 196 197 198 |
# File 'lib/groovy/model.rb', line 192 def render_results(results, &block) if block_given? results.each { |rec| yield new_from_record(rec) } else results.map { |rec| new_from_record(rec) } end end |
#schema(options = {}, &block) ⇒ Object
def plural_refs
schema.plural_references
end
81 82 83 |
# File 'lib/groovy/model.rb', line 81 def schema( = {}, &block) @schema ||= load_schema(, &block) end |
#scope(name, obj) ⇒ Object
221 222 223 224 225 226 227 |
# File 'lib/groovy/model.rb', line 221 def scope(name, obj) scopes.push(name) query_class.add_scope(name, obj) define_singleton_method(name) do |*args| query.public_send(name, *args) end end |
#scopes ⇒ Object
217 218 219 |
# File 'lib/groovy/model.rb', line 217 def scopes @scopes ||= [] end |
#set_timestamp(obj, key_name) ⇒ Object
261 262 263 |
# File 'lib/groovy/model.rb', line 261 def (obj, key_name) obj[key_name] = Time.now.utc if attribute_names.include?(key_name.to_sym) end |
#similar_search(column, query, options = {}, &block) ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'lib/groovy/model.rb', line 182 def similar_search(column, query, = {}, &block) unless schema.index_columns.include?(column.to_sym) raise Error, "Column '#{column}' doesn't have an index set!" end # results = table.select("#{col}:#{q}", operator: Groonga::Operation::SIMILAR) results = table.select { |rec| rec[column].similar_search(query) } render_results(results, &block) end |
#table ⇒ Object
60 61 62 63 |
# File 'lib/groovy/model.rb', line 60 def table # raise "Table name not set: #{table_name}" if table_name.nil? db_context[table_name] end |
#underscore_name ⇒ Object
65 66 67 |
# File 'lib/groovy/model.rb', line 65 def underscore_name name.gsub(/([A-Z])/) { |x| "_#{x.downcase}" }.sub(/^_/, '') end |
#unique_values_for(column, limit: -1,, cache: false) ⇒ Object
200 201 202 203 204 205 206 207 |
# File 'lib/groovy/model.rb', line 200 def unique_values_for(column, limit: -1, cache: false) cols = [column] opts = { drill_down: cols, drill_down_limit: limit, drilldown_sortby: '_key' } opts[:cache] = cache ? 'yes' : 'no' resp = table.context.select(table_name, opts) # drill_down_output_columns: cols arr = resp.body[1] # or resp.drilldowns arr.slice(2..-1) # .flatten end |
#update_all(attrs) ⇒ Object
159 160 161 |
# File 'lib/groovy/model.rb', line 159 def update_all(attrs) all.each { |child| child.update_attributes(attrs) } end |
#validatable! ⇒ Object
56 57 58 |
# File 'lib/groovy/model.rb', line 56 def validatable! include(ActiveModel::Validations) end |
#where(*args, &block) ⇒ Object
229 230 231 |
# File 'lib/groovy/model.rb', line 229 def where(*args, &block) query.where(*args, &block) end |