monkey_patch_happy 0.0.1 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ccffb0e27ca1c17ae260d0f95e7448bd9ee26a03
4
- data.tar.gz: eaaec9d3cbdb36f82a7493f2af2946634491efd6
3
+ metadata.gz: 1c9a9dd376b426d153814e08d7e50d0eeace62cd
4
+ data.tar.gz: 32eb630bbe57300ef80dd4b168cdfd4fffa41ead
5
5
  SHA512:
6
- metadata.gz: df57d5bf959ae6d3a99f1d009b6cb1390d6510eda57631e6815bebf7c5b83a2b0d06d69eb8591cd4c66b0b9bd39f11f6fa6f82c36140462afd38b6d1066f1b34
7
- data.tar.gz: 652b3ce4fe8c0c100a7028512ce109078a8b7fcdf91c9d57e49d90b88591a6a297bb88077d4c9260e608d7c0a7fb3e2323b1b3fba52e629341139d6bdd898ba2
6
+ metadata.gz: d514603b4f506bc9a928247e1ee31b6824297a9d78cfa663efde024fcdd48cfeca9f4e22519f3b88b9a95c82c610700e67a388b70c94174391010f6a46972361
7
+ data.tar.gz: 15bc1895e62b01983093c98395a2385d1afb38ba62c90b6d9157faad20ccd7015f553ff494f860c2f431751eced61b6e4a648f1a70251b1dd8c16a775755041d
@@ -1,7 +1,7 @@
1
- require "monkey_patch_happy/array_patch"
2
- require "monkey_patch_happy/string_patch"
3
- require "monkey_patch_happy/hash_patch"
4
- require "monkey_patch_happy/nil_patch"
1
+ # require "monkey_patch_happy/array_patch"
2
+ # require "monkey_patch_happy/string_patch"
3
+ # require "monkey_patch_happy/hash_patch"
4
+ # require "monkey_patch_happy/nil_patch"
5
5
 
6
6
  #for active_record
7
7
  require "monkey_patch_happy/active_record_extras"
@@ -0,0 +1,25 @@
1
+
2
+ module ActiveRecordExtras
3
+ module Relation
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def create_or_update(attributes, &block)
8
+ new_or_assign(attributes) do |obj|
9
+ block.call(obj) if block_given?
10
+ end
11
+ end
12
+
13
+
14
+ def new_or_assign(attributes)
15
+ obj = where(attributes).last || new(attributes) #不存在就创建。
16
+ # obj.assign_attributes(attributes)
17
+ yield obj
18
+ obj.updated_at = Time.new
19
+ obj.save
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ # ActiveRecord::Base.send :include, ActiveRecordExtras::Relation
@@ -0,0 +1,14 @@
1
+ class Array
2
+ #跟flatten一样,递归出所有小粒度value,并执行对应的blocks
3
+ #example: ["a","b",["c"]].each_recur {|elem| p elem}
4
+ # def each_recur(&block)
5
+ # return flatten if block.blank?
6
+ # each do |elem|
7
+ # if elem.is_a? Array
8
+ # elem.each_recur &block
9
+ # else
10
+ # block.call elem
11
+ # end
12
+ # end
13
+ # end
14
+ end
@@ -0,0 +1,35 @@
1
+ module Filterable
2
+ #use to mixin, can be include all model
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ #将参数发送对应的scope,进行条件筛选。
7
+ # as my own filter
8
+ def m_filter(filtering_params)
9
+ results = self.where(nil)
10
+ filtering_params.each do |key, value|
11
+ results = results.public_send(key, value) if value.present?
12
+ end
13
+ results
14
+ end
15
+
16
+ #as my own joins_model
17
+ # models = [:mcu,:online_status,[:setting, {:is_actived => ture}]]
18
+ def m_joins_model(models)
19
+ results = self.where(nil)
20
+ models.each do |model|
21
+ if model.class == Array
22
+ results = results.public_send(model[0],model[1])
23
+ else
24
+ results = results.public_send(model)
25
+ end
26
+ end
27
+ results
28
+ end
29
+ end
30
+
31
+ module InstanceMethods
32
+ end
33
+
34
+
35
+ end
@@ -0,0 +1,32 @@
1
+ class ::Hash
2
+ #example:
3
+ #{:a => 1, "b" => 2, "c" => 3}.fetch_all(:a, :b)
4
+ #{:a => 1, "b" => 2, "c" => 3}.fetch_all([:a, :b]) or [[:a, :b]]
5
+ def fetch_all(*args)
6
+ #values_at不支持数组
7
+ # args.flatten.map{|key| fetch(key)} #如果没找到该键值,会报错。
8
+ #compact 去掉为nil的键
9
+ args.flatten.map{|key| self[key]}
10
+ end
11
+
12
+
13
+ #str = "h", hash = {"01" => 1, "02" => 2 }
14
+ #result: {:h1 => 1, :h2 => 2 }
15
+ def format_key_to_sym(str)
16
+ new_hash = {}
17
+ self.each do |key, value|
18
+ new_hash[(str + key.to_i.to_s).to_sym] = value if key.present?
19
+ end
20
+ # self = nil #release
21
+ new_hash
22
+ end
23
+
24
+ #not test
25
+ def format_hour_key
26
+ new_hash = {}
27
+ self.each do |key, value|
28
+ new_hash[( "h" + (key.to_i + 1).to_s).to_sym] = value
29
+ end
30
+ new_hash
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ class << nil
2
+ def production?
3
+ false
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def production?
3
+ self == "production"
4
+ end
5
+ end
@@ -0,0 +1,147 @@
1
+ #计算相隔的时间。
2
+ #初始化年月日的hash等等。
3
+
4
+ module TimeHelper
5
+ #初始化hash
6
+ def get_24_hours(default_num = 0)
7
+ hash = {}
8
+ 24.times do |i|
9
+ hash[("h" + (i+1).to_s).to_sym ] = default_num
10
+ end
11
+ hash
12
+ end
13
+
14
+ def get_31_days(default_num = 0)
15
+ hash = {}
16
+ 31.times do |i|
17
+ hash[("d" + (i+1).to_s).to_sym] = default_num
18
+ end
19
+ hash
20
+ end
21
+
22
+ def get_12_months(default_num = 0)
23
+ hash = {}
24
+ 12.times do |i|
25
+ hash[("m" + (i+1).to_s).to_sym] = default_num
26
+ end
27
+ hash
28
+ end
29
+
30
+ #date.day之前总共多少小时
31
+ def apart_hours(begin_date, end_date)
32
+ hours = [(end_date - bbegin_date).to_i * 24 , 0].max
33
+ end
34
+
35
+ #两个日期之间多少天
36
+ def apart_days(begin_time, end_time)
37
+ days = [(end_time.to_date - begin_time.to_date).to_i + 1, 0].max
38
+ end
39
+
40
+ #两个日期之间多少个月
41
+ def apart_months(begin_time, end_time)
42
+ months = [((end_time.year * 12 + end_time.month) - (begin_time.year * 12 + begin_time.month) + 1), 0].max
43
+ end
44
+
45
+ #两个日期之间多少年
46
+ def apart_years(begin_time, end_time)
47
+ years = [(end_time.year - begin_time.year + 1), 0].max
48
+ end
49
+
50
+ def time_change_to_date(begin_time, end_time)
51
+ begin_date = begin_time.to_date
52
+ end_date = end_time.to_date
53
+ yield begin_date, end_date
54
+ end
55
+
56
+ #两个时间之间每个小时的个数。#1点3次,2点3次,3点3次,4点2次。
57
+ #result like : {:h1=>5,:h2=>5,:h3=>4 ...}
58
+ def hours_number(begin_time, end_time)
59
+ days = apart_days(begin_time, end_time) #相隔多少天
60
+ number_hash = get_24_hours(days) #因为每天都是24小时,所以直接用“天数 x 24”
61
+ time_now = Time.now #用来判断所选日期是否是今天。
62
+
63
+ #比如结束时间是当天的2015-11-5 20:00:00 ,那么21,22,23小时需要减掉
64
+ if end_time.strftime('%Y-%m-%d') == time_now.strftime('%Y-%m-%d')
65
+ #hour is 1..24h
66
+ (24 - time_now.hour - 1).times do |index|
67
+ number_hash[("h" + (time_now.hour + index + 1 + 1 ).to_s).to_sym] -= 1
68
+ end
69
+
70
+ #hour is 0..23h
71
+ # (24 - time_now.hour).times do |index|
72
+ # number_hash[("h" + (time_now.hour + index + 1).to_s).to_sym] -= 1
73
+ # end
74
+ end
75
+
76
+ #比如开始时间是2015-11-5 4:00:00 ,那么1,2,3小时需要减掉
77
+ begin_time.hour.times do |index|
78
+ number_hash[("h" + (index + 1).to_s).to_sym] -= 1
79
+ end
80
+ number_hash
81
+ end
82
+
83
+ #两个日期之间各天的天数。比如1号有4天,2号有3天等。
84
+ #result like : {:d1=>5,:d2=>4,:d3=>4,:d4=>4,:d5=>4,:d6=>4,:d7=>4,:d8=>4, ...}
85
+ def days_number(begin_time, end_time)
86
+ number_hash = get_31_days
87
+ today = Date.today
88
+
89
+ index = 0
90
+ remaining_date = today
91
+ while remaining_date.strftime('%Y-%m') >= begin_time.strftime('%Y-%m')
92
+ Time.days_in_month(remaining_date.month).times do |index|
93
+ number_hash[("d" + (index + 1).to_s).to_sym] += 1
94
+ end
95
+ index += 1
96
+ remaining_date = end_time - index.month
97
+ end
98
+
99
+ #比如当前日期是2015-12-25号,那么应该减去26,27,28,29,30,31
100
+ if end_time.strftime('%Y-%m') == today.strftime('%Y-%m')
101
+ (Time.days_in_month(today.month) - today.day).times do |index|
102
+ number_hash[("d" + (today.day + index + 1).to_s).to_sym] -= 1
103
+ end
104
+ end
105
+
106
+ #比如开始日期是2015-11-5 ,那么1,2,3,4号需要减掉
107
+ (begin_time.day - 1).times do |index|
108
+ number_hash[("d" + (index + 1).to_s).to_sym] -= 1
109
+ end
110
+
111
+ number_hash
112
+ end
113
+
114
+ #两个日期之间,每个月的个数
115
+ #result like : {:m1=>3, :m2=>2, :m3=>2, :m4=>2, :m5=>2, :m6=>2, :m7=>2, :m8=>3, :m9=>3, :m10=>3, :m11=>3, :m12=>3}
116
+ def months_number(begin_time, end_time)
117
+ number_hash = get_12_months
118
+ today = Date.today
119
+
120
+ index = 0
121
+ remaining_date = end_time
122
+ while remaining_date.strftime('%Y') >= begin_time.strftime('%Y')
123
+ 12.times do |index|
124
+ number_hash[("m" + (index + 1).to_s).to_sym] += 1
125
+ end
126
+
127
+ index += 1
128
+ remaining_date = end_time - index.year
129
+ end
130
+
131
+ #今年是2016,目前是1月份。那么2月..12月就需要去掉
132
+ if end_time.strftime('%Y') == today.strftime('%Y')
133
+ (12 - today.month).times do |index|
134
+ number_hash[("m" + (today.month + index + 1).to_s).to_sym] -= 1
135
+ end
136
+ end
137
+
138
+ #比如开始日期是2015-5 ,那么1,2,3,4月需要减掉
139
+ (begin_time.month - 1).times do |index|
140
+ number_hash[("m" + (index + 1).to_s).to_sym] -= 1
141
+ end
142
+
143
+ number_hash
144
+ end
145
+
146
+
147
+ end
@@ -1,3 +1,3 @@
1
1
  module MonkeyPatchHappy
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+ # require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ # describe 'Array#each_recur' do
5
+ # before(:each) do
6
+ # @name_array = ["don", "na", "lai", "fu"]
7
+ # @name_array_1 = [["don", "na"], ["lai"], "fu"]
8
+ # end
9
+
10
+ # # after(:all) do
11
+ # # User.destroy_all
12
+ # # end
13
+
14
+ # it "is array#each_recur exist?" do
15
+ # expect(@name_array.respond_to?(:each_recur)).to eq false
16
+ # end
17
+
18
+ # it "when give a block" do
19
+ # expect(@name_array.each_recur {|elem| elem.upcase}).to eql ["DON", "NA", "LAI", "FU"]
20
+ # expect(@name_array_1.each_recur {|elem| elem.upcase}).to eql ["DON", "NA", "LAI", "FU"]
21
+ # end
22
+
23
+ # it "when not give a block" do
24
+ # expect(@name_array.each_recur).to eql ["DON", "NA", "LAI", "FU"]
25
+ # expect(@name_array_1.each_recur).to eql ["DON", "NA", "LAI", "FU"]
26
+ # end
27
+ # end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe "Hash#fetch_all" do
4
+ it "fetch some key with array" do
5
+ my_hash = {"01" => 45, "02" => 78, "10" => 105, "11" => 199, "12" => 500}
6
+
7
+ expect(my_hash.fetch_all(["01", "05", "10"])).to eq [45, nil, 105]
8
+ expect(my_hash.fetch_all(["01", "12", "10"])).not_to eq [45, 44, 105]
9
+ end
10
+
11
+ it "fetch some key" do
12
+ my_hash = {"01" => 45, "02" => 78, "10" => 105, "11" => 199, "12" => 500}
13
+ expect(my_hash.fetch_all("01", "05", "10")).to eq [45, nil, 105]
14
+ end
15
+ end
16
+
17
+ describe "Hash#format_key_to_sym" do
18
+ it "format key to sym and filter blank" do
19
+ my_hash = {"01" => 45, "02" => 78, "10" => 105, "11" => 199, "12" => 500}
20
+ expect(my_hash.format_key_to_sym("h")).to eq ({:h1 => 45, :h2 => 78, :h10 => 105, :h11 => 199, :h12 => 500 })
21
+
22
+ my_hash = {"01" => 45, "" => 78, nil => 105, " " => 199, "12" => 500}
23
+ expect(my_hash.format_key_to_sym("h")).to eq ({:h1 => 45, :h12 => 500 })
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe "nil.production?" do
4
+ it " is valid " do
5
+ expect(nil.production?).to eq false
6
+ end
7
+
8
+ it " is invalid " do
9
+ expect(nil.production?).not_to eq true
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ # require 'rubygems'
2
+
3
+ # ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', File.dirname(__FILE__))
4
+ # require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
5
+ # $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ # $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ # require 'active_support/all'
8
+
9
+ require 'bundler/setup'
10
+ Bundler.setup
11
+
12
+ require 'active_support/all'
13
+ require 'monkey_patch_happy'
14
+
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe "String#production?" do
4
+ it "when is production" do
5
+ expect("production".production?).to eq true
6
+ end
7
+
8
+ it "when is not production" do
9
+ expect("development".production?).to eq false
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monkey_patch_happy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - xdchen
@@ -79,8 +79,20 @@ files:
79
79
  - README.md
80
80
  - Rakefile
81
81
  - lib/monkey_patch_happy.rb
82
+ - lib/monkey_patch_happy/active_record_extras.rb
83
+ - lib/monkey_patch_happy/array_patch.rb
84
+ - lib/monkey_patch_happy/filterable.rb
85
+ - lib/monkey_patch_happy/hash_patch.rb
86
+ - lib/monkey_patch_happy/nil_patch.rb
87
+ - lib/monkey_patch_happy/string_patch.rb
88
+ - lib/monkey_patch_happy/time_helper.rb
82
89
  - lib/monkey_patch_happy/version.rb
83
90
  - monkey_patch_happy.gemspec
91
+ - spec/array_spec.rb
92
+ - spec/hash_spec.rb
93
+ - spec/nil_spec.rb
94
+ - spec/spec_helper.rb
95
+ - spec/string_spec.rb
84
96
  homepage: http://rubygems.org/gems/monkey_patch_happy
85
97
  licenses:
86
98
  - MIT
@@ -105,4 +117,9 @@ rubygems_version: 2.1.9
105
117
  signing_key:
106
118
  specification_version: 4
107
119
  summary: some extends for ruby on rails
108
- test_files: []
120
+ test_files:
121
+ - spec/array_spec.rb
122
+ - spec/hash_spec.rb
123
+ - spec/nil_spec.rb
124
+ - spec/spec_helper.rb
125
+ - spec/string_spec.rb