module PostJson::FinderMethods
Public Instance Methods
any?()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 14 def any? execute { |documents| documents.any? } end
blank?()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 18 def blank? execute { |documents| documents.blank? } end
count(column_name = nil, options = {})
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 22 def count(column_name = nil, options = {}) selector = if column_name.present? define_selector(column_name) else column_name end execute { |documents| documents.count(selector, options) } end
delete(id_or_array)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 31 def delete(id_or_array) where(id: id_or_array).delete_all end
delete_all(conditions = nil)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 35 def delete_all(conditions = nil) where(conditions).execute { |documents| documents.delete_all } end
destroy(id)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 39 def destroy(id) value = id ? id.to_s.downcase : nil execute { |documents| documents.destroy(value) } end
destroy_all(conditions = nil)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 44 def destroy_all(conditions = nil) where(conditions).execute { |documents| documents.destroy_all } end
empty?()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 48 def empty? execute { |documents| documents.empty? } end
exists?(conditions = nil)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 52 def exists?(conditions = nil) query = case conditions when nil self when Numeric, String where({id: conditions}) else where(conditions) end query.execute { |documents| documents.exists? } end
find(*args)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 65 def find(*args) if args.length == 0 execute { |documents| documents.find } elsif args.length == 1 && args[0].is_a?(Array) ids = args[0].map{|id| id ? id.to_s.downcase : nil} execute { |documents| documents.find(ids) } elsif args.length == 1 && args[0].is_a?(Array) == false id = args[0] ? args[0].to_s.downcase : nil execute { |documents| documents.find(id) } else ids = args.map{|id| id ? id.to_s.downcase : nil} execute { |documents| documents.find(ids) } end end
find_by(*args)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 80 def find_by(*args) where(*args).first end
find_by!(*args)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 84 def find_by!(*args) find_by(*args) or raise ActiveRecord::RecordNotFound end
find_each(options = {})
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 88 def find_each(options = {}) execute { |documents| documents.find_each(options) } end
find_in_batches(options = {})
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 92 def find_in_batches(options = {}) execute { |documents| documents.find_in_batches(options) } end
first(limit = nil)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 96 def first(limit = nil) if limit limit(limit).execute { |documents| documents.first } else execute { |documents| documents.first } end end
first!()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 104 def first! execute { |documents| documents.first! } end
first_or_create(attributes = {})
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 108 def first_or_create(attributes = {}) attributes = where_values_hash.with_indifferent_access.deep_merge(attributes) first or create(attributes) end
first_or_initialize(attributes = {})
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 113 def first_or_initialize(attributes = {}) attributes = where_values_hash.with_indifferent_access.deep_merge(attributes) first or model_class.new(attributes) end
ids()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 118 def ids pluck('id') end
last(limit = nil)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 122 def last(limit = nil) reverse_order.first(limit) end
last!()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 126 def last! reverse_order.first! end
load()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 130 def load execute { |documents| documents.load } end
many?()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 134 def many? execute { |documents| documents.many? } end
pluck(*selectors)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 138 def pluck(*selectors) selectors = join_arguments(*selectors) if selectors == "" [] elsif selectors == "*" execute { |documents| documents.pluck("\"#{table_name}\".__doc__body") } elsif selectors == "id" execute { |documents| documents.pluck("\"#{table_name}\".id") } else result = nil execute { |documents| result = documents.pluck("json_selectors('#{selectors}', \"#{table_name}\".__doc__body)") } if selectors.include?(",") result else result.flatten(1) end end end
select(*selectors)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 157 def select(*selectors) selectors = selectors.flatten(1) if selectors.length == 0 [] elsif selectors.length == 1 selector = selectors[0] case selector when String, Symbol selector = selector.to_s.gsub(/\s+/, '') if selector == "*" pluck("*").map { |body| body ? Hashie::Mash.new(body) : body } else selectors = selector.split(",") if selectors.length == 1 select({selector => selector}) else select(selectors) end end when Hash flat_hash = selector.flatten_hash pluck(flat_hash.values).map do |row| flat_body = if flat_hash.keys.length == 1 {flat_hash.keys[0] => row} else Hash[flat_hash.keys.zip(row)] end deep_hash = flat_body.deepen_hash Hashie::Mash.new(deep_hash) if deep_hash end else raise ArgumentError, "Invalid argument(s): #{selectors.inspect}" end else select(Hash[selectors.zip(selectors)]) end end
size()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 195 def size execute { |documents| documents.size } end
take(limit = nil)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 199 def take(limit = nil) execute { |documents| documents.take(limit) } end
take!()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 203 def take! execute { |documents| documents.take! } end
to_a()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 207 def to_a execute { |documents| documents.to_a } end
to_sql()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 211 def to_sql execute { |documents| documents.to_sql } end
where_values_hash()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 215 def where_values_hash where_equals = query_tree[:where_equal] || [] values_hash = where_equals.inject({}) do |result, where_equal| key = where_equal[:attribute] result[key] = where_equal[:argument] result end values_hash.deepen_hash end
Protected Instance Methods
active_record_send_invocations()
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 289 def active_record_send_invocations prepare_query_tree_for_method_mapping(query_tree) end
define_selector(attribute_name)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 227 def define_selector(attribute_name) case attribute_name.to_s when primary_key "\"#{table_name}\".#{primary_key}" else "json_selector('#{attribute_name}', \"#{table_name}\".__doc__body)" end end
prepare_query_tree_for_method_mapping(query_tree)
click to toggle source
# File lib/post_json/concerns/finder_methods.rb, line 236 def prepare_query_tree_for_method_mapping(query_tree) prepared_query_tree = query_tree.map do |method_sym, arguments_collection| arguments_collection.map do |arguments| case method_sym when :limit [:limit, arguments] when :offset [:offset, arguments] when :order name, direction = arguments.split(" ") selector = define_selector(name) [:order, "#{selector} #{direction}"] when :where_function function = arguments[:function] escape_sql_single_quote = function.gsub("'", "''") condition = "js_filter('#{escape_sql_single_quote}', '#{arguments[:arguments]}', \"#{table_name}\".__doc__body) = 1" [:where, condition] when :where_forward if arguments[0].is_a?(String) json_regex = "json_([^ =]+)" arguments[0] = arguments[0].gsub(/^#{json_regex}\ /) {"json_selector('#{$1}', \"#{table_name}\".__doc__body) "} arguments[0] = arguments[0].gsub(/\ #{json_regex}\ /) {" json_selector('#{$1}', \"#{table_name}\".__doc__body) "} arguments[0] = arguments[0].gsub(/\ #{json_regex}$/) {" json_selector('#{$1}', \"#{table_name}\".__doc__body)"} end [:where, arguments] when :where_equal selector = define_selector(arguments[:attribute]) selector_is_primary_key = arguments[:attribute].to_s == primary_key argument = arguments[:argument] case argument when Array values = argument.map{|v| v ? v.to_s : nil} values = values.map{|v| v.try(:downcase)} if selector_is_primary_key [:where, "(#{selector} IN (?))", values] when Range first_value = argument.first ? argument.first.to_s : nil first_value = first_value.try(:downcase) if selector_is_primary_key last_value = argument.last ? argument.last.to_s : nil last_value = last_value.try(:downcase) if selector_is_primary_key [:where, "(#{selector} BETWEEN ? AND ?)", first_value, last_value] else value = argument ? argument.to_s : nil value = value.try(:downcase) if selector_is_primary_key [:where, "#{selector} = ?", value] end else raise NotImplementedError, "Query tree method '#{method_sym}' not mapped to Active Record." end end end prepared_query_tree.flatten(1) end