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
- #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.
45 46 47 |
# File 'lib/groovy/model.rb', line 45 def context_name @context_name end |
#table_name ⇒ Object
Returns the value of attribute table_name.
45 46 47 |
# File 'lib/groovy/model.rb', line 45 def table_name @table_name end |
Instance Method Details
#add_accessors_for(col, s = schema) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/groovy/model.rb', line 100 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
87 88 89 90 91 92 |
# File 'lib/groovy/model.rb', line 87 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
94 95 96 97 98 |
# File 'lib/groovy/model.rb', line 94 def add_reference(name, table_name, = {}) schema.reference(name, table_name, ) schema.sync add_accessors_for(name) end |
#all ⇒ Object
196 197 198 |
# File 'lib/groovy/model.rb', line 196 def all query end |
#attribute_names ⇒ Object
56 57 58 |
# File 'lib/groovy/model.rb', line 56 def attribute_names @attribute_names ||= schema.attribute_columns end |
#callbacks ⇒ Object
252 253 254 |
# File 'lib/groovy/model.rb', line 252 def callbacks @callbacks ||= {} end |
#create(attributes, key = nil) ⇒ Object
137 138 139 140 |
# File 'lib/groovy/model.rb', line 137 def create(attributes, key = nil) obj = new(attributes, nil, key) obj.save ? obj : false end |
#create!(attributes, key = nil) ⇒ Object
142 143 144 |
# File 'lib/groovy/model.rb', line 142 def create!(attributes, key = nil) create(attributes, key) or raise Error, "Invalid" end |
#delete_all ⇒ Object
150 151 152 153 |
# File 'lib/groovy/model.rb', line 150 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
131 132 133 134 135 |
# File 'lib/groovy/model.rb', line 131 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
164 165 166 167 |
# File 'lib/groovy/model.rb', line 164 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
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/groovy/model.rb', line 229 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/groovy/model.rb', line 72 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
113 114 115 116 |
# File 'lib/groovy/model.rb', line 113 def new_from_record(record) # new(record.attributes, record) new(nil, record) end |
#query ⇒ Object
200 201 202 |
# File 'lib/groovy/model.rb', line 200 def query query_class.new(self, table) end |
#render_results(results, &block) ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/groovy/model.rb', line 179 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
68 69 70 |
# File 'lib/groovy/model.rb', line 68 def schema( = {}, &block) @schema ||= load_schema(, &block) end |
#scope(name, obj) ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/groovy/model.rb', line 208 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
204 205 206 |
# File 'lib/groovy/model.rb', line 204 def scopes @scopes ||= [] end |
#set_timestamp(obj, key_name) ⇒ Object
248 249 250 |
# File 'lib/groovy/model.rb', line 248 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
169 170 171 172 173 174 175 176 177 |
# File 'lib/groovy/model.rb', line 169 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
51 52 53 54 |
# File 'lib/groovy/model.rb', line 51 def table # raise "Table name not set: #{table_name}" if table_name.nil? db_context[table_name] end |
#unique_values_for(column, limit: -1,, cache: false) ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/groovy/model.rb', line 187 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
146 147 148 |
# File 'lib/groovy/model.rb', line 146 def update_all(attrs) all.each { |child| child.update_attributes(attrs) } end |
#validatable! ⇒ Object
47 48 49 |
# File 'lib/groovy/model.rb', line 47 def validatable! include(ActiveModel::Validations) end |
#where(*args, &block) ⇒ Object
216 217 218 |
# File 'lib/groovy/model.rb', line 216 def where(*args, &block) query.where(*args, &block) end |