Permalink
Browse files

Monkey patching core classes without using module as smart dependency…

… does not work well with moduled definition
  • Loading branch information...
1 parent c9c67f7 commit 7faeb3e54b1155687a427289bfc62ea1c1c0cd8d @seanhotw seanhotw committed Jan 30, 2013
View
@@ -8,7 +8,5 @@ Motion::Project::App.setup do |app|
app.name = 'SimpleViewDemo'
app.deployment_target = '5.0'
- app.detect_dependencies = false
- app.files.unshift(Dir.glob(File.join(File.dirname(__FILE__), 'lib/simple_view/*.rb')))
- app.files.unshift(Dir.glob(File.join(File.dirname(__FILE__), 'lib/simple_view/extensions/*.rb')))
+ app.files.unshift(Dir.glob('./lib/simple_view/**/*.rb'))
end
View
@@ -5,6 +5,5 @@
end
Motion::Project::App.setup do |app|
- app.files.unshift(Dir.glob(File.join(File.dirname(__FILE__), 'simple_view/*.rb')))
- app.files.unshift(Dir.glob(File.join(File.dirname(__FILE__), 'simple_view/extensions/*.rb')))
+ app.files.unshift(Dir.glob(File.join(File.dirname(__FILE__), 'simple_view/**/*.rb')))
end
@@ -1,9 +1,5 @@
-module SimpleView
- module FixnumExtensions
- def percent
- self / 100.0
- end
+class Fixnum
+ def percent
+ self / 100.0
end
-end
-
-Fixnum.send(:include, SimpleView::FixnumExtensions)
+end
@@ -1,65 +1,61 @@
-module SimpleView
- module StringExtensions
- def underscore
- word = self.dup
- word.gsub!(/::/, '/')
- word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
- word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
- word.tr!("-", "_")
- word.downcase!
- word
- end
-
- def to_color
- html_colour = self.dup
- html_colour.gsub!(%r{[#;]}, '')
-
- case html_colour.size
- when 3
- colours = html_colour.scan(%r{[0-9A-Fa-f]}).map { |el| (el * 2).to_i(16) }
- when 6
- colours = html_colour.scan(%r<[0-9A-Fa-f]{2}>).map { |el| el.to_i(16) }
- else
- return ::UIColor.clearColor if html_colour == 'transparent' || 'clear'
- raise ArgumentError
- end
+class String
+ def underscore
+ word = self.dup
+ word.gsub!(/::/, '/')
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
+ word.tr!("-", "_")
+ word.downcase!
+ word
+ end
- ::UIColor.colorWithRed(colours[0]/255.0, green: colours[1]/255.0, blue: colours[2]/255.0, alpha: 1)
+ def to_color
+ html_colour = self.dup
+ html_colour.gsub!(%r{[#;]}, '')
+
+ case html_colour.size
+ when 3
+ colours = html_colour.scan(%r{[0-9A-Fa-f]}).map { |el| (el * 2).to_i(16) }
+ when 6
+ colours = html_colour.scan(%r<[0-9A-Fa-f]{2}>).map { |el| el.to_i(16) }
+ else
+ return ::UIColor.clearColor if html_colour == 'transparent' || 'clear'
+ raise ArgumentError
end
- def to_font
- bold = false
- italic = false
- size = ::UIFont.systemFontSize
- font_name = nil
-
- self.split(' ').each do |comp|
- if comp == "bold"
- bold = true
- elsif comp == "italic"
- italic = true
- elsif comp.to_f > 0
- size = comp.to_f
- elsif comp.length > 4
- font_name = comp
- end
- end
+ ::UIColor.colorWithRed(colours[0]/255.0, green: colours[1]/255.0, blue: colours[2]/255.0, alpha: 1)
+ end
- if font_name
- ::UIFont.fontWithName(font_name, size: size)
- elsif bold
- ::UIFont.boldSystemFontOfSize(size)
- elsif italic
- ::UIFont.italicSystemFontOfSize(size)
- else
- ::UIFont.systemFontOfSize(size)
+ def to_font
+ bold = false
+ italic = false
+ size = ::UIFont.systemFontSize
+ font_name = nil
+
+ self.split(' ').each do |comp|
+ if comp == "bold"
+ bold = true
+ elsif comp == "italic"
+ italic = true
+ elsif comp.to_f > 0
+ size = comp.to_f
+ elsif comp.length > 4
+ font_name = comp
end
end
- def to_image
- ::UIImage.imageNamed self
+ if font_name
+ ::UIFont.fontWithName(font_name, size: size)
+ elsif bold
+ ::UIFont.boldSystemFontOfSize(size)
+ elsif italic
+ ::UIFont.italicSystemFontOfSize(size)
+ else
+ ::UIFont.systemFontOfSize(size)
end
end
-end
-String.send(:include, SimpleView::StringExtensions)
+ def to_image
+ ::UIImage.imageNamed self
+ end
+end
@@ -1,9 +1,5 @@
-module SimpleView
- module UIColorExtensions
- def to_color
- self
- end
+class UIColor
+ def to_color
+ self
end
-end
-
-UIColor.send(:include, SimpleView::UIColorExtensions)
+end
@@ -1,9 +1,5 @@
-module SimpleView
- module UIFontExtensions
- def to_font
- self
- end
+class UIFont
+ def to_font
+ self
end
-end
-
-UIFont.send(:include, SimpleView::UIFontExtensions)
+end
@@ -1,9 +1,5 @@
-module SimpleView
- module UIImageExtensions
- def to_image
- self
- end
+class UIImage
+ def to_image
+ self
end
-end
-
-UIImage.send(:include, SimpleView::UIImageExtensions)
+end
@@ -1,68 +1,59 @@
-module SimpleView
- module UIViewExtensions
- module Traversing
- attr_accessor :name
+class UIView
+ attr_accessor :name
- def find name
- subviews.each do |subview|
- return subview if subview.name == name
- end
- nil
- end
- alias_method :subview, :find
-
- def sibling name
- if superview
- superview.find name
- else
- nil
- end
- end
+ def find name
+ subviews.each do |subview|
+ return subview if subview.name == name
+ end
+ nil
+ end
+ alias_method :subview, :find
- def closest name
- view = sibling name
- if view.nil? && superview
- view = superview.closest name
- end
- view
- end
+ def sibling name
+ if superview
+ superview.find name
+ else
+ nil
end
+ end
- module Dimensions
- def left
- self.frame.origin.x
- end
+ def closest name
+ view = sibling name
+ if view.nil? && superview
+ view = superview.closest name
+ end
+ view
+ end
- def setLeft value
- self.frame = [[value, self.frame.origin.y], [self.frame.size.width, self.frame.size.height]]
- end
+ def left
+ self.frame.origin.x
+ end
- def top
- self.frame.origin.y
- end
+ def setLeft value
+ self.frame = [[value, self.frame.origin.y], [self.frame.size.width, self.frame.size.height]]
+ end
- def setTop value
- self.frame = [[self.frame.origin.x, value], [self.frame.size.width, self.frame.size.height]]
- end
+ def top
+ self.frame.origin.y
+ end
- def width
- self.frame.size.width
- end
+ def setTop value
+ self.frame = [[self.frame.origin.x, value], [self.frame.size.width, self.frame.size.height]]
+ end
- def setWidth value
- self.frame = [[self.frame.origin.x, self.frame.origin.y], [value, self.frame.size.height]]
- end
+ def width
+ self.frame.size.width
+ end
- def height
- self.frame.size.height
- end
+ def setWidth value
+ self.frame = [[self.frame.origin.x, self.frame.origin.y], [value, self.frame.size.height]]
+ end
- def setHeight value
- self.frame = [[self.frame.origin.x, self.frame.origin.y], [self.frame.size.width, value]]
- end
- end
+ def height
+ self.frame.size.height
end
-end
-UIView.send(:include, SimpleView::UIViewExtensions::Traversing)
-UIView.send(:include, SimpleView::UIViewExtensions::Dimensions)
+ def setHeight value
+ self.frame = [[self.frame.origin.x, self.frame.origin.y], [self.frame.size.width, value]]
+ end
+end
@@ -1,3 +1,3 @@
module SimpleView
- VERSION = "0.5"
+ VERSION = "0.5.1"
end

0 comments on commit 7faeb3e

Please sign in to comment.