dragonfly_extensions 1.0.1 → 1.0.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.
data/Manifest CHANGED
@@ -1,17 +1,16 @@
1
1
  README.rdoc
2
2
  Rakefile
3
3
  dragonfly_extensions.gemspec
4
+ lib/addressable.rb
5
+ lib/attachable.rb
6
+ lib/automatically_includable.rb
7
+ lib/billable.rb
8
+ lib/binnable.rb
4
9
  lib/dragonfly_extensions.rb
5
- lib/dragonfly_extensions/addressable.rb
6
- lib/dragonfly_extensions/attachable.rb
7
- lib/dragonfly_extensions/automatically_includable.rb
8
- lib/dragonfly_extensions/billable.rb
9
- lib/dragonfly_extensions/binnable.rb
10
- lib/dragonfly_extensions/dragonfly_extensions.rb
11
- lib/dragonfly_extensions/identifiable.rb
12
- lib/dragonfly_extensions/measurable.rb
13
- lib/dragonfly_extensions/noteable.rb
14
- lib/dragonfly_extensions/product_attribute.rb
15
- lib/dragonfly_extensions/taggable_on.rb
16
- lib/dragonfly_extensions/uniqueable.rb
10
+ lib/identifiable.rb
11
+ lib/measurable.rb
12
+ lib/noteable.rb
13
+ lib/product_attribute.rb
14
+ lib/taggable_on.rb
15
+ lib/uniqueable.rb
17
16
  Manifest
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require 'rubygems'
3
3
  require 'rake'
4
4
  require 'echoe'
5
5
 
6
- Echoe.new('dragonfly_extensions', '1.0.1') do |p|
6
+ Echoe.new('dragonfly_extensions', '1.0.2') do |p|
7
7
  p.description = "Collection of Dragonfly Depot Extensions [Probably not useful for anybody, ever]"
8
8
  p.author = "Hans Masing"
9
9
  p.ignore_pattern = ["tmp/*", "script/*"]
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{dragonfly_extensions}
5
- s.version = "1.0.1"
5
+ s.version = "1.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Hans Masing"]
9
9
  s.date = %q{2010-05-10}
10
10
  s.description = %q{Collection of Dragonfly Depot Extensions [Probably not useful for anybody, ever]}
11
11
  s.email = %q{}
12
- s.extra_rdoc_files = ["README.rdoc", "lib/dragonfly_extensions.rb", "lib/dragonfly_extensions/addressable.rb", "lib/dragonfly_extensions/attachable.rb", "lib/dragonfly_extensions/automatically_includable.rb", "lib/dragonfly_extensions/billable.rb", "lib/dragonfly_extensions/binnable.rb", "lib/dragonfly_extensions/dragonfly_extensions.rb", "lib/dragonfly_extensions/identifiable.rb", "lib/dragonfly_extensions/measurable.rb", "lib/dragonfly_extensions/noteable.rb", "lib/dragonfly_extensions/product_attribute.rb", "lib/dragonfly_extensions/taggable_on.rb", "lib/dragonfly_extensions/uniqueable.rb"]
13
- s.files = ["README.rdoc", "Rakefile", "dragonfly_extensions.gemspec", "lib/dragonfly_extensions.rb", "lib/dragonfly_extensions/addressable.rb", "lib/dragonfly_extensions/attachable.rb", "lib/dragonfly_extensions/automatically_includable.rb", "lib/dragonfly_extensions/billable.rb", "lib/dragonfly_extensions/binnable.rb", "lib/dragonfly_extensions/dragonfly_extensions.rb", "lib/dragonfly_extensions/identifiable.rb", "lib/dragonfly_extensions/measurable.rb", "lib/dragonfly_extensions/noteable.rb", "lib/dragonfly_extensions/product_attribute.rb", "lib/dragonfly_extensions/taggable_on.rb", "lib/dragonfly_extensions/uniqueable.rb", "Manifest"]
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/addressable.rb", "lib/attachable.rb", "lib/automatically_includable.rb", "lib/billable.rb", "lib/binnable.rb", "lib/dragonfly_extensions.rb", "lib/identifiable.rb", "lib/measurable.rb", "lib/noteable.rb", "lib/product_attribute.rb", "lib/taggable_on.rb", "lib/uniqueable.rb"]
13
+ s.files = ["README.rdoc", "Rakefile", "dragonfly_extensions.gemspec", "lib/addressable.rb", "lib/attachable.rb", "lib/automatically_includable.rb", "lib/billable.rb", "lib/binnable.rb", "lib/dragonfly_extensions.rb", "lib/identifiable.rb", "lib/measurable.rb", "lib/noteable.rb", "lib/product_attribute.rb", "lib/taggable_on.rb", "lib/uniqueable.rb", "Manifest"]
14
14
  s.homepage = %q{}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Dragonfly_extensions", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
@@ -1,6 +1,173 @@
1
- $:.unshift(File.dirname(__FILE__)) unless
2
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
1
+ #
2
+ # Monkey Patches
3
+ #
4
+ module ActiveRecord
5
+ class Base
6
+ def self.random
7
+ if (c = count) != 0
8
+ find(:first, :offset =>rand(c))
9
+ end
10
+ end
11
+
12
+ def self.all_polymorphic_types(name)
13
+ @poly_hash ||= {}.tap do |hash|
14
+ Dir.glob(File.join(RAILS_ROOT, "app", "models", "**", "*.rb")).each do |file|
15
+ klass = (File.basename(file, ".rb").camelize.constantize rescue nil)
16
+ next if klass.nil? or !klass.ancestors.include?(ActiveRecord::Base)
17
+ klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
18
+ (hash[reflection.options[:as]] ||= []) << klass
19
+ end
20
+ end
21
+ end
22
+ @poly_hash[name.to_sym]
23
+ end
24
+ end
25
+ end
3
26
 
4
- module DragonflyExtensions
5
- VERSION = '0.0.1'
27
+ module AASM
28
+ module ClassMethods
29
+ def aasm_states_hash
30
+ AASM::StateMachine[self].states.map { |state| state.name.to_s }
31
+ end
32
+ def aasm_events_hash
33
+ AASM::StateMachine[self].events.map { |event| event.first.to_s }
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+ class Hashit
40
+ def initialize(hash)
41
+ hash.each do |k,v|
42
+ self.instance_variable_set("@#{k}", v)
43
+ self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
44
+ self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
45
+ end
46
+ end
47
+
48
+ def save
49
+ hash_to_return = {}
50
+ self.instance_variables.each do |var|
51
+ hash_to_return[var.gsub("@","")] = self.instance_variable_get(var)
52
+ end
53
+ return hash_to_return
54
+ end
55
+ end
56
+
57
+ class Numeric
58
+
59
+ def to_years_and_months
60
+ case
61
+ when self == 0
62
+ "0 months"
63
+ when self < 12
64
+ "#{ self} month#{ self == 1 ? '' : 's' }"
65
+ when self == 12
66
+ "1 year"
67
+ when self > 12
68
+ years = self / 12
69
+ months = self % 12
70
+ if (months > 0)
71
+ "#{years} year#{ years > 1 ? 's' : '' } and #{ months} month#{ months == 1 ? '' : 's' }"
72
+ else
73
+ "#{years} year#{ years > 1 ? 's' : '' }"
74
+ end
75
+ end
76
+ end
77
+
78
+ def roundup(nearest=10)
79
+ self % nearest == 0 ? self : self + nearest - (self % nearest)
80
+ end
81
+
82
+ def rounddown(nearest=10)
83
+ self % nearest == 0 ? self : self - (self % nearest)
84
+ end
85
+
86
+ def roundnearest(nearest=10)
87
+ up = roundup(nearest)
88
+ down = rounddown(nearest)
89
+ if (up-self) < (self-down)
90
+ return up
91
+ else
92
+ return down
93
+ end
94
+ end
95
+
96
+ def factorial
97
+ my_i = self.to_i
98
+ if my_i <= 0
99
+ result = 0
100
+ else
101
+ result = 1
102
+ my_i.downto(2) do |n|
103
+ result = result * n
104
+ end
105
+ end
106
+ result
107
+ end
108
+
109
+ def odd?
110
+ self & 1 != 0
111
+ end
112
+
113
+ def even?
114
+ self & 1 == 0
115
+ end
116
+ end
117
+
118
+ class String
119
+ # unicode_str.initial_caps => new_str
120
+ # returns a copy of a string with initial capitals
121
+ # "Jules-Édouard".initial_caps => "J.É."
122
+ def initial_caps
123
+ self.tr('-', ' ').split(' ').map { |word| word.chars.first.upcase.to_s + "." }.join
124
+ end
125
+
126
+ def capitalize_each_word
127
+ self.downcase.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase }
128
+ end
129
+
130
+ def compress
131
+ self.gsub(' ','')
132
+ end
133
+
134
+ def truncate_words(length = 10, end_string = ' …')
135
+ words = self.split()
136
+ words[0..(length-1)].join(' ') + (words.length > length ? end_string : '')
137
+ end
138
+
139
+ def chunk_by_words(chunk_size = 8)
140
+ result = []
141
+ words = self.split
142
+ for chunk in words.in_groups_of(chunk_size)
143
+ result << chunk.join(' ')
144
+ end
145
+ result
146
+ end
147
+
148
+ def chunk_by_length(max_length = 40)
149
+ result = []
150
+ words = self.split
151
+ line = ''
152
+ for word in words
153
+ if (line.length > max_length) or (line.length + word.length > max_length)
154
+ result << line
155
+ line = ''
156
+ end
157
+ line << ' ' + word
158
+ end
159
+ result << line
160
+ result
161
+ end
162
+
163
+ def String.random_alphanumeric(length=6)
164
+ chars = ['A'..'Z','0'..'9'].map{|r|r.to_a}.flatten
165
+ ignore = ['0','1','L','A','E','I','O','U']
166
+ valid = (chars - ignore)
167
+ Array.new(length).map{valid[rand(valid.size)]}.join.downcase
168
+ end
169
+
170
+ def asset_escape
171
+ CGI::escape(self.downcase.downcase.gsub(/\s+/,'_').gsub(/[^[:alnum:]_]/, ''))
172
+ end
6
173
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 1
9
- version: 1.0.1
8
+ - 2
9
+ version: 1.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Hans Masing
@@ -26,36 +26,34 @@ extensions: []
26
26
 
27
27
  extra_rdoc_files:
28
28
  - README.rdoc
29
+ - lib/addressable.rb
30
+ - lib/attachable.rb
31
+ - lib/automatically_includable.rb
32
+ - lib/billable.rb
33
+ - lib/binnable.rb
29
34
  - lib/dragonfly_extensions.rb
30
- - lib/dragonfly_extensions/addressable.rb
31
- - lib/dragonfly_extensions/attachable.rb
32
- - lib/dragonfly_extensions/automatically_includable.rb
33
- - lib/dragonfly_extensions/billable.rb
34
- - lib/dragonfly_extensions/binnable.rb
35
- - lib/dragonfly_extensions/dragonfly_extensions.rb
36
- - lib/dragonfly_extensions/identifiable.rb
37
- - lib/dragonfly_extensions/measurable.rb
38
- - lib/dragonfly_extensions/noteable.rb
39
- - lib/dragonfly_extensions/product_attribute.rb
40
- - lib/dragonfly_extensions/taggable_on.rb
41
- - lib/dragonfly_extensions/uniqueable.rb
35
+ - lib/identifiable.rb
36
+ - lib/measurable.rb
37
+ - lib/noteable.rb
38
+ - lib/product_attribute.rb
39
+ - lib/taggable_on.rb
40
+ - lib/uniqueable.rb
42
41
  files:
43
42
  - README.rdoc
44
43
  - Rakefile
45
44
  - dragonfly_extensions.gemspec
45
+ - lib/addressable.rb
46
+ - lib/attachable.rb
47
+ - lib/automatically_includable.rb
48
+ - lib/billable.rb
49
+ - lib/binnable.rb
46
50
  - lib/dragonfly_extensions.rb
47
- - lib/dragonfly_extensions/addressable.rb
48
- - lib/dragonfly_extensions/attachable.rb
49
- - lib/dragonfly_extensions/automatically_includable.rb
50
- - lib/dragonfly_extensions/billable.rb
51
- - lib/dragonfly_extensions/binnable.rb
52
- - lib/dragonfly_extensions/dragonfly_extensions.rb
53
- - lib/dragonfly_extensions/identifiable.rb
54
- - lib/dragonfly_extensions/measurable.rb
55
- - lib/dragonfly_extensions/noteable.rb
56
- - lib/dragonfly_extensions/product_attribute.rb
57
- - lib/dragonfly_extensions/taggable_on.rb
58
- - lib/dragonfly_extensions/uniqueable.rb
51
+ - lib/identifiable.rb
52
+ - lib/measurable.rb
53
+ - lib/noteable.rb
54
+ - lib/product_attribute.rb
55
+ - lib/taggable_on.rb
56
+ - lib/uniqueable.rb
59
57
  - Manifest
60
58
  has_rdoc: true
61
59
  homepage: ""
@@ -1,173 +0,0 @@
1
- #
2
- # Monkey Patches
3
- #
4
- module ActiveRecord
5
- class Base
6
- def self.random
7
- if (c = count) != 0
8
- find(:first, :offset =>rand(c))
9
- end
10
- end
11
-
12
- def self.all_polymorphic_types(name)
13
- @poly_hash ||= {}.tap do |hash|
14
- Dir.glob(File.join(RAILS_ROOT, "app", "models", "**", "*.rb")).each do |file|
15
- klass = (File.basename(file, ".rb").camelize.constantize rescue nil)
16
- next if klass.nil? or !klass.ancestors.include?(ActiveRecord::Base)
17
- klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
18
- (hash[reflection.options[:as]] ||= []) << klass
19
- end
20
- end
21
- end
22
- @poly_hash[name.to_sym]
23
- end
24
- end
25
- end
26
-
27
- module AASM
28
- module ClassMethods
29
- def aasm_states_hash
30
- AASM::StateMachine[self].states.map { |state| state.name.to_s }
31
- end
32
- def aasm_events_hash
33
- AASM::StateMachine[self].events.map { |event| event.first.to_s }
34
- end
35
- end
36
- end
37
-
38
-
39
- class Hashit
40
- def initialize(hash)
41
- hash.each do |k,v|
42
- self.instance_variable_set("@#{k}", v)
43
- self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
44
- self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
45
- end
46
- end
47
-
48
- def save
49
- hash_to_return = {}
50
- self.instance_variables.each do |var|
51
- hash_to_return[var.gsub("@","")] = self.instance_variable_get(var)
52
- end
53
- return hash_to_return
54
- end
55
- end
56
-
57
- class Numeric
58
-
59
- def to_years_and_months
60
- case
61
- when self == 0
62
- "0 months"
63
- when self < 12
64
- "#{ self} month#{ self == 1 ? '' : 's' }"
65
- when self == 12
66
- "1 year"
67
- when self > 12
68
- years = self / 12
69
- months = self % 12
70
- if (months > 0)
71
- "#{years} year#{ years > 1 ? 's' : '' } and #{ months} month#{ months == 1 ? '' : 's' }"
72
- else
73
- "#{years} year#{ years > 1 ? 's' : '' }"
74
- end
75
- end
76
- end
77
-
78
- def roundup(nearest=10)
79
- self % nearest == 0 ? self : self + nearest - (self % nearest)
80
- end
81
-
82
- def rounddown(nearest=10)
83
- self % nearest == 0 ? self : self - (self % nearest)
84
- end
85
-
86
- def roundnearest(nearest=10)
87
- up = roundup(nearest)
88
- down = rounddown(nearest)
89
- if (up-self) < (self-down)
90
- return up
91
- else
92
- return down
93
- end
94
- end
95
-
96
- def factorial
97
- my_i = self.to_i
98
- if my_i <= 0
99
- result = 0
100
- else
101
- result = 1
102
- my_i.downto(2) do |n|
103
- result = result * n
104
- end
105
- end
106
- result
107
- end
108
-
109
- def odd?
110
- self & 1 != 0
111
- end
112
-
113
- def even?
114
- self & 1 == 0
115
- end
116
- end
117
-
118
- class String
119
- # unicode_str.initial_caps => new_str
120
- # returns a copy of a string with initial capitals
121
- # "Jules-Édouard".initial_caps => "J.É."
122
- def initial_caps
123
- self.tr('-', ' ').split(' ').map { |word| word.chars.first.upcase.to_s + "." }.join
124
- end
125
-
126
- def capitalize_each_word
127
- self.downcase.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase }
128
- end
129
-
130
- def compress
131
- self.gsub(' ','')
132
- end
133
-
134
- def truncate_words(length = 10, end_string = ' …')
135
- words = self.split()
136
- words[0..(length-1)].join(' ') + (words.length > length ? end_string : '')
137
- end
138
-
139
- def chunk_by_words(chunk_size = 8)
140
- result = []
141
- words = self.split
142
- for chunk in words.in_groups_of(chunk_size)
143
- result << chunk.join(' ')
144
- end
145
- result
146
- end
147
-
148
- def chunk_by_length(max_length = 40)
149
- result = []
150
- words = self.split
151
- line = ''
152
- for word in words
153
- if (line.length > max_length) or (line.length + word.length > max_length)
154
- result << line
155
- line = ''
156
- end
157
- line << ' ' + word
158
- end
159
- result << line
160
- result
161
- end
162
-
163
- def String.random_alphanumeric(length=6)
164
- chars = ['A'..'Z','0'..'9'].map{|r|r.to_a}.flatten
165
- ignore = ['0','1','L','A','E','I','O','U']
166
- valid = (chars - ignore)
167
- Array.new(length).map{valid[rand(valid.size)]}.join.downcase
168
- end
169
-
170
- def asset_escape
171
- CGI::escape(self.downcase.downcase.gsub(/\s+/,'_').gsub(/[^[:alnum:]_]/, ''))
172
- end
173
- end