Class: MiyazakiResistance::Base
- Inherits:
-
Object
- Object
- MiyazakiResistance::Base
- Defined in:
- lib/miyazaki_resistance/base.rb,
lib/miyazaki_resistance/column.rb,
lib/miyazaki_resistance/operation.rb,
lib/miyazaki_resistance/connection.rb
Constant Summary collapse
- OPERATIONS =
{ "=" => {:string => TokyoTyrant::RDBQRY::QCSTREQ, :number => TokyoTyrant::RDBQRY::QCNUMEQ}, "!=" => {:string => TokyoTyrant::RDBQRY::QCNEGATE | TokyoTyrant::RDBQRY::QCSTREQ, :number => TokyoTyrant::RDBQRY::QCNEGATE | TokyoTyrant::RDBQRY::QCNUMEQ}, "<>" => {:string => TokyoTyrant::RDBQRY::QCNEGATE | TokyoTyrant::RDBQRY::QCSTREQ, :number => TokyoTyrant::RDBQRY::QCNEGATE | TokyoTyrant::RDBQRY::QCNUMEQ}, "include" => {:string => TokyoTyrant::RDBQRY::QCSTRINC}, "begin" => {:string => TokyoTyrant::RDBQRY::QCSTRBW}, "end" => {:string => TokyoTyrant::RDBQRY::QCSTREW}, "allinclude" => {:string => TokyoTyrant::RDBQRY::QCSTRAND}, "anyinclude" => {:string => TokyoTyrant::RDBQRY::QCSTROR}, "in" => {:string => TokyoTyrant::RDBQRY::QCSTROREQ, :number => TokyoTyrant::RDBQRY::QCNUMOREQ}, "=~" => {:string => TokyoTyrant::RDBQRY::QCSTRRX}, "!~" => {:string => TokyoTyrant::RDBQRY::QCNEGATE | TokyoTyrant::RDBQRY::QCSTRRX}, ">" => {:number => TokyoTyrant::RDBQRY::QCNUMGT}, ">=" => {:number => TokyoTyrant::RDBQRY::QCNUMGE}, "<" => {:number => TokyoTyrant::RDBQRY::QCNUMLT}, "<=" => {:number => TokyoTyrant::RDBQRY::QCNUMLE}, "between" => {:number => TokyoTyrant::RDBQRY::QCNUMBT} }
- NOT_OPERATIONS =
%w|include begin end allinclude anyinclude in between|
- DATE_TYPE =
[:datetime, :date]
- @@connection_manager =
{}
Class Attribute Summary collapse
-
.all_columns ⇒ Object
Returns the value of attribute all_columns.
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
- .column(name, type, index = :no_index) ⇒ Object (also: set_column)
- .connection ⇒ Object
- .connection_or_create ⇒ Object
- .count(args = {}) ⇒ Object
- .create(args) ⇒ Object
- .delete_all(args = []) ⇒ Object
- .find(first, args = {}) ⇒ Object
- .find_and_update(first, args = {}) ⇒ Object
- .find_by_id(target) ⇒ Object
- .find_by_query(mode, args = {}) ⇒ Object
- .first(args = {}) ⇒ Object
- .kaeru_timeout(&block) ⇒ Object
- .method_missing(name, *arguments, &block) ⇒ Object
- .plastic_data(value, type) ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes=(args) ⇒ Object
- #destroy ⇒ Object
-
#initialize(args = nil) ⇒ Base
constructor
A new instance of Base.
- #kaeru_timeout(&block) ⇒ Object
- #new_record? ⇒ Boolean
- #read_connection ⇒ Object
- #remove_pool(con) ⇒ Object
- #save ⇒ Object
- #update_attributes(args) ⇒ Object
- #write_connection ⇒ Object
Constructor Details
#initialize(args = nil) ⇒ Base
Returns a new instance of Base.
5 6 7 8 |
# File 'lib/miyazaki_resistance/base.rb', line 5 def initialize(args = nil) self.id = nil set_args(args || {}) end |
Class Attribute Details
.all_columns ⇒ Object
Returns the value of attribute all_columns.
4 5 6 |
# File 'lib/miyazaki_resistance/column.rb', line 4 def all_columns @all_columns end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/miyazaki_resistance/base.rb', line 3 def id @id end |
Class Method Details
.column(name, type, index = :no_index) ⇒ Object Also known as: set_column
6 7 8 9 10 11 12 |
# File 'lib/miyazaki_resistance/column.rb', line 6 def column(name, type, index = :no_index) self.all_columns ||= {} name = name.to_s self.__send__(:attr_accessor, name) type = :number if type.to_s == "integer" self.all_columns.update(name => type) end |
.connection ⇒ Object
29 30 31 |
# File 'lib/miyazaki_resistance/connection.rb', line 29 def connection @@connection_manager[self] end |
.connection_or_create ⇒ Object
33 34 35 |
# File 'lib/miyazaki_resistance/connection.rb', line 33 def connection_or_create @@connection_manager[self] ||= MR::Connection.new end |
.count(args = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/miyazaki_resistance/base.rb', line 84 def count(args = {}) con = read_connection if args.empty? kaeru_timeout{con.rnum} else query = TokyoTyrant::RDBQRY.new(con) query = make_conditions(query, args[:conditions]) query.respond_to?(:searchcount) ? kaeru_timeout{query.searchcount} : kaeru_timeout{query.search.count} end rescue TimeoutError remove_pool(con) retry end |
.create(args) ⇒ Object
112 113 114 |
# File 'lib/miyazaki_resistance/base.rb', line 112 def create(args) self.new(args).save end |
.delete_all(args = []) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/miyazaki_resistance/base.rb', line 98 def delete_all(args = []) con = write_connection if args.empty? con.vanish else query = TokyoTyrant::RDBQRY.new(con) query = make_conditions(query, args) kaeru_timeout{query.searchout} end rescue TimeoutError remove_pool(con) retry end |
.find(first, args = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/miyazaki_resistance/base.rb', line 41 def find(first, args={}) case first.class.name when "Fixnum" find_by_id(first) when "Array" find_by_ids(first) when "Symbol" find_by_query(first, args) else raise ArgumentError end end |
.find_and_update(first, args = {}) ⇒ Object
74 75 76 77 78 |
# File 'lib/miyazaki_resistance/base.rb', line 74 def find_and_update(first, args={}) list = self.find(first, args) list = [list] unless list.is_a?(Array) list.each {|inst| yield(inst) and inst.save} end |
.find_by_id(target) ⇒ Object
70 71 72 |
# File 'lib/miyazaki_resistance/base.rb', line 70 def find_by_id(target) find_by_ids([target]).first end |
.find_by_query(mode, args = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/miyazaki_resistance/base.rb', line 54 def find_by_query(mode, args={}) con = read_connection query = TokyoTyrant::RDBQRY.new(con) limit = (mode == :first ? 1 : args[:limit]) query = make_limit(query, limit, args[:offset]) query = make_order(query, args[:order]) query = make_conditions(query, args[:conditions]) results = kaeru_timeout{query.searchget}.map{|r| self.new(r)} limit.to_i == 1 ? results.first : results rescue TimeoutError remove_pool(con) retry end |
.first(args = {}) ⇒ Object
80 81 82 |
# File 'lib/miyazaki_resistance/base.rb', line 80 def first(args = {}) find(:first, args) end |
.kaeru_timeout(&block) ⇒ Object
37 38 39 40 |
# File 'lib/miyazaki_resistance/connection.rb', line 37 def kaeru_timeout(&block) con = connection con.kaeru_timeout(&block) end |
.method_missing(name, *arguments, &block) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/miyazaki_resistance/base.rb', line 116 def method_missing(name, *arguments, &block) if match = finder_attribute_names(name) finder = match[:finder] conditions = match[:cols].map{|col| "#{col} = ?"}.join(" ") self.class_eval %Q| def self.#{name}(*args) options = args.last.is_a?(::Hash) ? pop : {} options.update(:conditions => ["#{conditions}", args].flatten) self.find(:#{finder}, options) end | __send__(name, *arguments) else super end end |
.plastic_data(value, type) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/miyazaki_resistance/column.rb', line 15 def plastic_data(value, type) ret = case type when :datetime value ? value.to_i : nil when :date value ? Time.local(value.year, value.month, value.day).to_i : nil else value end ret.to_s end |
Instance Method Details
#attributes ⇒ Object
28 29 30 |
# File 'lib/miyazaki_resistance/column.rb', line 28 def attributes self.class.all_columns.keys.inject({}) {|hash, key| hash.merge(key => self.__send__(key))} end |
#attributes=(args) ⇒ Object
32 33 34 |
# File 'lib/miyazaki_resistance/column.rb', line 32 def attributes=(args) args.each {|key, value| self.__send__("#{key}=", value)} end |
#destroy ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/miyazaki_resistance/base.rb', line 27 def destroy raise NewRecordError if new_record? con = write_connection kaeru_timeout{con.out(self.id)} rescue TimeoutError remove_pool(con) retry end |
#kaeru_timeout(&block) ⇒ Object
55 56 57 |
# File 'lib/miyazaki_resistance/connection.rb', line 55 def kaeru_timeout(&block) self.class.kaeru_timeout(&block) end |
#new_record? ⇒ Boolean
36 37 38 |
# File 'lib/miyazaki_resistance/base.rb', line 36 def new_record? self.id.nil? end |
#read_connection ⇒ Object
43 44 45 |
# File 'lib/miyazaki_resistance/connection.rb', line 43 def read_connection self.class.read_connection end |
#remove_pool(con) ⇒ Object
51 52 53 |
# File 'lib/miyazaki_resistance/connection.rb', line 51 def remove_pool(con) self.class.remove_pool(con) end |
#save ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/miyazaki_resistance/base.rb', line 10 def save time_column_check con = write_connection self.id = kaeru_timeout{con.genuid.to_i} if new_record? kaeru_timeout {con.put(self.id, raw_attributes)} self rescue TimeoutError remove_pool(con) retry end |
#update_attributes(args) ⇒ Object
21 22 23 24 25 |
# File 'lib/miyazaki_resistance/base.rb', line 21 def update_attributes(args) raise NewRecordError if new_record? attributes = args save end |
#write_connection ⇒ Object
47 48 49 |
# File 'lib/miyazaki_resistance/connection.rb', line 47 def write_connection self.class.write_connection end |