ror_hack 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 89bd9b601388e515327f54a438851c184712845d
4
+ data.tar.gz: 960b5265161146e4f44ada17a1a30ef7e38b4375
5
+ SHA512:
6
+ metadata.gz: 1879b7200427e66ff0bc6d9afacf9e0ef3f6d1a6caca67451dfa3402154d9ab890efcf5f25994f44a10f32d101a19173a6c3de4211a8d02e667fadce246ee5e4
7
+ data.tar.gz: 3cce17839660596de9b1e601500ff8b716fd9dd19384b5f2ad5bde9c4f29bcfce1f0a8aeaa1109106811f91f7d22141404ab795423df3f45069d3323868784c7
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ror_hack.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 happyMing
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # RorHack
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ror_hack'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ror_hack
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/ror_hack/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,159 @@
1
+ require 'request_store'
2
+ module RorHack
3
+
4
+ module ActiveRecordBaseSingletonClassHack
5
+
6
+ # 用于模型使用view(视图)时的创建新对象后,获取的对象的id总是null或0的bug,需要建一个字段,这个方法应在放在所有其他before_create和after_create和after_commit的回调前面,并且视图中,应包含hash_column这个字段,实际上hash_column字段是在real_model这个模型的对应表中.
7
+ def use_mysql_view(real_model, *args)
8
+ opts = args.extract_options!.with_indifferent_access
9
+ self.primary_key = 'id'
10
+ hash_column = (opts[:hash_column]||'mysql_view_bug_fix_id').to_s
11
+ fails '没有设置hash列。' unless hash_column.in?(real_model.column_names) && hash_column.in?(self.column_names)
12
+
13
+ after_initialize do
14
+ self.id = nil if id.is?(0)
15
+ end
16
+
17
+ before_create do
18
+ begin
19
+ self[hash_column] = SecureRandom.hex
20
+ end while real_model.exists?(hash_column => self[hash_column])
21
+ end
22
+
23
+ after_create do
24
+ id = real_model.find_by(hash_column => self[hash_column]).id
25
+ self.id = id
26
+ end
27
+ end
28
+
29
+ def in_and_ref(table)
30
+ includes(table).references(table.to_s.pluralize)
31
+ end
32
+
33
+ [:save, :create, :update].each do |type|
34
+ define_method "assign_on_#{type}" do |column, value, options = {}|
35
+ options = options.with_indifferent_access
36
+ send "before_#{type}" do
37
+ block = lambda do
38
+ tmp_value = if value.is_a? Proc
39
+ instance_eval(&value)
40
+ else
41
+ value
42
+ end
43
+ send("#{column}=", tmp_value)
44
+ end
45
+ if options.key?(:if)
46
+ if options[:if].is_a? Proc
47
+ block.call if instance_eval(&options[:if])
48
+ else
49
+ block.call if options[:if]
50
+ end
51
+ next
52
+ end
53
+ if options.key?(:unless)
54
+ if options[:unless].is_a? Proc
55
+ block.call unless instance_eval(&options[:unless])
56
+ else
57
+ block.call unless options[:if]
58
+ end
59
+ next
60
+ end
61
+ block.call
62
+ end
63
+ end
64
+ end
65
+
66
+ # 序列化属性.
67
+ def serialize_hack(attr_name, class_name = Object, options = {})
68
+ serialize(attr_name, class_name)
69
+
70
+ if class_name == Array && options.with_indifferent_access['delete_blank_string']
71
+ before_save do
72
+ new_array = send(attr_name)
73
+ new_array.delete_if do |item|
74
+ item.is_a?(String) && item.blank?
75
+ end
76
+ send(attr_name.to_s + '=', new_array)
77
+ end
78
+ end
79
+ end
80
+
81
+ def ming(str, _options = {})
82
+ human_attribute_name(str, options = {})
83
+ end
84
+ end
85
+
86
+ module ActiveRecordBaseHack
87
+
88
+ # 返回某个枚举字段的英文对应的locales名称。
89
+ def method_missing(method, *args, &block)
90
+ method_name = method.to_s
91
+ naked_name = method_name.remove('_chinese_desc')
92
+ if method_name.end_with?('_chinese_desc') && respond_to?(naked_name)
93
+ return self.class.ming("#{ naked_name }.#{ self.send naked_name }")
94
+ end
95
+ super
96
+ end
97
+ end
98
+
99
+ module ControllerRequestUglyInject
100
+ def self.included(mod)
101
+ ActionController::Base.class_eval do
102
+ before_filter do
103
+ params = {
104
+ user: (current_user rescue nil),
105
+ request_ip: request.env['HTTP_X_REAL_IP'] || request.remote_ip,
106
+ session: session
107
+ }
108
+ RequestStore.store[:controller_params] = OpenStruct.new(params).freeze
109
+ end
110
+ end
111
+ mod.class_eval do
112
+ delegate :dingo_info, to: :class
113
+
114
+ def self.dingo_info
115
+ if RequestStore.store.key?(:controller_params)
116
+ RequestStore.store.fetch(:controller_params)
117
+ else
118
+ OpenStruct.new.freeze
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+ # 用于定义可在类继承连上继承的实例变量。区别于了变量,兄弟类之间不会互相影响。
126
+ module ClassLevelInheritableAttributes
127
+ def self.included(base)
128
+ base.extend(ClassMethods)
129
+ end
130
+
131
+ module ClassMethods
132
+ def inheritable_attributes(*args)
133
+ @inheritable_attributes ||= [:inheritable_attributes]
134
+ @inheritable_attributes += args
135
+ args.each do |arg|
136
+ class_eval <<-RUBY
137
+ class << self; attr_accessor :#{arg} end
138
+ RUBY
139
+ end
140
+ @inheritable_attributes
141
+ end
142
+
143
+ def inherited(subclass)
144
+ super
145
+ (@inheritable_attributes||[]).each do |inheritable_attribute|
146
+ instance_var = "@#{inheritable_attribute}"
147
+ subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+ end
154
+
155
+ class ActiveRecord::Base
156
+ extend RorHack::ActiveRecordBaseSingletonClassHack
157
+ include RorHack::ActiveRecordBaseHack
158
+ include RorHack::ControllerRequestUglyInject
159
+ end
@@ -0,0 +1,79 @@
1
+ module RorHack
2
+ module ArrayHack
3
+ def multi_group_by(*keys)
4
+ group_by_block = lambda do |array, ks|
5
+ key = ks.shift
6
+ return array unless key
7
+ if key.is_a? Proc
8
+ hash = array.group_by(&key)
9
+ else
10
+ hash = array.group_by { |i| i[key] }
11
+ end
12
+ if ks.present?
13
+ hash.keys.each do |k|
14
+ hash[k] = group_by_block.call hash[k], ks.dup
15
+ end
16
+ end
17
+ return hash
18
+ end
19
+ group_by_block.call self, keys
20
+ end
21
+
22
+ def include_any?(*args)
23
+ args.any? { |i| i.in? self }
24
+ end
25
+
26
+ def include_all?(*args)
27
+ args.all? { |i| i.in? self }
28
+ end
29
+
30
+ def all_blank?
31
+ self.all?(&:blank?)
32
+ end
33
+
34
+ def any_blank?
35
+ self.any?(&:blank?)
36
+ end
37
+
38
+ def all_present?
39
+ self.all?(&:present?)
40
+ end
41
+
42
+ def any_present?
43
+ self.any?(&:present?)
44
+ end
45
+
46
+ def right_strip_nil
47
+ self.reverse!
48
+ left_strip_nil
49
+ reverse
50
+ end
51
+
52
+ # 清空左边为nil的所有元素.
53
+ def left_strip_nil
54
+ while first.nil?
55
+ delete_at(0)
56
+ break if size == 0
57
+ end
58
+ self
59
+ end
60
+
61
+ def strip_nil
62
+ left_strip_nil.right_strip_nil
63
+ end
64
+
65
+ def right_push(num, ele = nil)
66
+ push(ele) while size < num
67
+ self
68
+ end
69
+
70
+ def left_push(num, ele = nil)
71
+ unshift(ele) while size < num
72
+ self
73
+ end
74
+ end
75
+ end
76
+
77
+ class Array
78
+ include RorHack::ArrayHack
79
+ end
@@ -0,0 +1,47 @@
1
+ module RorHack
2
+ module HashHack
3
+ def add_placeholder(key_array, value = nil)
4
+ keys = self.keys.map(&:to_sym)
5
+ key_array.each do |key|
6
+ next if key.to_sym.in? keys # hash本身已经有这个键,则跳过复制。
7
+ if value
8
+ self[key] = value.deep_dup
9
+ else
10
+ self[key] = default(nil)
11
+ end
12
+ end
13
+ self
14
+ end
15
+
16
+ # 两个hash的顺序也相等?
17
+ def sort_eql?(hash)
18
+ return false unless hash.is_a? Hash
19
+ to_a == hash.to_a
20
+ end
21
+
22
+ # 默认不做排序,用hash本身的顺序.
23
+ def get_last_value(&block)
24
+ keys = self.keys
25
+ keys = keys.sort(&block) if block
26
+ self[keys.last]
27
+ end
28
+
29
+ def delete_recursively(array)
30
+ self.except!(*array)
31
+ each do |_k, v|
32
+ if v.is_a? Hash
33
+ v.delete_recursively(array)
34
+ end
35
+ end
36
+ self
37
+ end
38
+ end
39
+ end
40
+
41
+ class Hash
42
+ include RorHack::HashHack
43
+ end
44
+
45
+ class HashWithIndifferentAccess
46
+ include RorHack::HashHack
47
+ end
@@ -0,0 +1,12 @@
1
+ module RorHack
2
+ module KernelHack
3
+ # 将eval中数据绑定移动到前面,样式好看一些。
4
+ def petty_eval(bind, str)
5
+ eval str, bind
6
+ end
7
+ end
8
+ end
9
+
10
+ module Kernel
11
+ include RorHack::KernelHack
12
+ end
@@ -0,0 +1,14 @@
1
+ module RorHack
2
+ module ModuleHack
3
+ # 创建类对象的实例变量。
4
+ def singleton_attr_accessor(*args)
5
+ singleton_class.class_eval do
6
+ attr_accessor(*args)
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ class Module
13
+ include RorHack::ModuleHack
14
+ end
@@ -0,0 +1,12 @@
1
+ module RorHack
2
+ module NilClassHack
3
+ def j2h(_type = nil)
4
+ {}.with_indifferent_access
5
+ end
6
+
7
+ end
8
+ end
9
+
10
+ class NilClass
11
+ include RorHack::NilClassHack
12
+ end
@@ -0,0 +1,83 @@
1
+ module RorHack
2
+ module ObjectHack
3
+ def exist_and_eql?(other)
4
+ self && (self == other)
5
+ end
6
+
7
+ # 实例变量获取,如果不存在,则设为某个值
8
+ def instance_variable_fetch(name, value = nil)
9
+ instance_variable_get("@#{ name }").tap do |i|
10
+ return i unless i.is?(nil)
11
+ end
12
+ block_given? && my_value = yield
13
+ value && my_value = value
14
+ instance_variable_set("@#{ name }", my_value)
15
+ end
16
+
17
+ def is?(other)
18
+ self == other
19
+ end
20
+
21
+ def is_not?(other)
22
+ !is?(other)
23
+ end
24
+
25
+ # 可能想给has_one传递参数,用此可以设定对象的属性,来获取一些特殊关联。
26
+ def passport_block(object, variable, value)
27
+ new = false
28
+ unless object.instance_variable_defined?("@#{variable}")
29
+ object.instance_variable_set("@#{variable}", nil)
30
+ new = true
31
+ end
32
+ tmp_value = object.instance_variable_get("@#{variable}")
33
+ begin
34
+ object.instance_variable_set("@#{variable}", value)
35
+ yield
36
+ ensure
37
+ object.instance_variable_set("@#{variable}", tmp_value)
38
+ object.remove_instance_variable("@#{variable}") if new
39
+ end
40
+ end
41
+
42
+ def is_not_a?(arg)
43
+ !self.is_a?(arg)
44
+ end
45
+
46
+ def not_in?(arg)
47
+ !self.in?(arg)
48
+ end
49
+
50
+ def p2n
51
+ result = presence
52
+ result = 0 if result.nil?
53
+ result
54
+ end
55
+
56
+ def p2s
57
+ result = presence
58
+ result = '' if result.nil?
59
+ result
60
+ end
61
+
62
+ def p2h
63
+ result = presence
64
+ result = {} if result.nil?
65
+ result
66
+ end
67
+
68
+ def p2a
69
+ result = presence
70
+ result = [] if result.nil?
71
+ result
72
+ end
73
+
74
+ def p2ros
75
+ result = presence
76
+ result = OpenStruct.new if result.nil?
77
+ end
78
+ end
79
+ end
80
+
81
+ class Object
82
+ include RorHack::ObjectHack
83
+ end
@@ -0,0 +1,51 @@
1
+ module RorHack
2
+ class LookupStack
3
+ def initialize(bindings = [])
4
+ @bindings = bindings
5
+ end
6
+
7
+ def method_missing(m, *args)
8
+ @bindings.reverse_each do |bind|
9
+ begin
10
+ method = eval("method(%s)" % m.inspect, bind)
11
+ rescue NameError
12
+ else
13
+ return method.call(*args)
14
+ end
15
+ begin
16
+ value = eval(m.to_s, bind)
17
+ return value
18
+ rescue NameError
19
+ end
20
+ end
21
+ raise NoMethodError
22
+ end
23
+
24
+ def push_binding(bind)
25
+ @bindings.push bind
26
+ end
27
+
28
+ def push_instance(obj)
29
+ @bindings.push obj.instance_eval { binding }
30
+ end
31
+
32
+ def push_hash(vars)
33
+ push_instance Struct.new(*vars.keys).new(*vars.values)
34
+ end
35
+
36
+ def run_proc(p, *args)
37
+ instance_exec(*args, &p)
38
+ end
39
+ end
40
+
41
+ module ProcHack
42
+ def call_with_binding(bind, *args)
43
+ LookupStack.new([bind]).run_proc(self, *args)
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ class Proc
50
+ include RorHack::ProcHack
51
+ end
@@ -0,0 +1,48 @@
1
+ module RorHack
2
+ module StringHack
3
+
4
+ def center_truncate(head= 4, tail= 3)
5
+ head = (head + 3 + tail)
6
+ return self unless self.length > head + tail
7
+ self.truncate(head, omission: "...#{self.last(tail)}")
8
+ end
9
+
10
+ def j2h(type = Object::HashWithIndifferentAccess)
11
+ temp = JSON.parse(presence || '{}')
12
+ if type == Object::HashWithIndifferentAccess
13
+ if temp.is_a? Array
14
+ hash = { temp: temp }.with_indifferent_access
15
+ temp = hash[:temp]
16
+ else
17
+ temp = temp.with_indifferent_access
18
+ end
19
+ end
20
+ temp
21
+ end
22
+
23
+ def lstyle
24
+ "%#{strip}%"
25
+ end
26
+
27
+ def llstyle
28
+ "#{strip}%"
29
+ end
30
+
31
+ def rlstyle
32
+ "%#{strip}"
33
+ end
34
+
35
+ def zero2nil
36
+ if strip == '0'
37
+ nil
38
+ else
39
+ self
40
+ end
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ class String
47
+ include RorHack::StringHack
48
+ end
@@ -0,0 +1,3 @@
1
+ module RorHack
2
+ VERSION = "0.1.2"
3
+ end
data/lib/ror_hack.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'ror_hack/active_record_hack'
2
+ require 'ror_hack/array_hack'
3
+ require 'ror_hack/hash_hack'
4
+ require 'ror_hack/kernel_hack'
5
+ require 'ror_hack/module_hack'
6
+ require 'ror_hack/nil_hack'
7
+ require 'ror_hack/object_hack'
8
+ require 'ror_hack/string_hack'
9
+ require 'ror_hack/proc_hack'
10
+ require 'ror_hack/version'
11
+
data/ror_hack.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ror_hack/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ror_hack"
8
+ spec.version = RorHack::VERSION
9
+ spec.authors = ["happyMing"]
10
+ spec.email = ["happyming9527@gmail.com"]
11
+ spec.summary = %q{ruby on rails library hack.}
12
+ spec.description = %q{add some methods to modules of ruby or rails.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency 'request_store'
24
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ror_hack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - happyMing
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: request_store
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: add some methods to modules of ruby or rails.
56
+ email:
57
+ - happyming9527@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/ror_hack.rb
68
+ - lib/ror_hack/active_record_hack.rb
69
+ - lib/ror_hack/array_hack.rb
70
+ - lib/ror_hack/hash_hack.rb
71
+ - lib/ror_hack/kernel_hack.rb
72
+ - lib/ror_hack/module_hack.rb
73
+ - lib/ror_hack/nil_hack.rb
74
+ - lib/ror_hack/object_hack.rb
75
+ - lib/ror_hack/proc_hack.rb
76
+ - lib/ror_hack/string_hack.rb
77
+ - lib/ror_hack/version.rb
78
+ - ror_hack.gemspec
79
+ homepage: ''
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.6
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: ruby on rails library hack.
103
+ test_files: []