sugar-high 0.4.4.2 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.4.2
1
+ 0.4.5
@@ -1,3 +1,5 @@
1
+ require 'sugar-high/kind_of'
2
+
1
3
  class Class
2
4
  def include_and_extend(the_module, options={})
3
5
  options[:instance_methods] ||= :InstanceMethods
@@ -8,4 +10,71 @@ class Class
8
10
  include main_module.const_get(options[:instance_methods]) if main_module.const_defined?(options[:instance_methods])
9
11
  extend main_module.const_get(options[:class_methods]) if main_module.const_defined?(options[:class_methods])
10
12
  end
11
- end
13
+ end
14
+
15
+ module ClassExt
16
+ def is_class?(clazz)
17
+ clazz.is_a?(Class) && (clazz.respond_to? :new)
18
+ end
19
+
20
+ def is_module?(clazz)
21
+ clazz.is_a?(Module) && !(clazz.respond_to? :new)
22
+ end
23
+
24
+ def class_exists?(name)
25
+ is_class? Module.const_get(name)
26
+ rescue NameError
27
+ return false
28
+ end
29
+
30
+ def module_exists?(name)
31
+ is_module? Module.const_get(name)
32
+ rescue NameError
33
+ return false
34
+ end
35
+
36
+ def try_class name
37
+ return name if name.kind_of?(Class)
38
+ found = Module.const_get(name) if name.kind_of_label?
39
+ return found if found.is_a?(Class)
40
+ rescue
41
+ false
42
+ end
43
+
44
+ def try_module name
45
+ return name if name.kind_of?(Module)
46
+ found = Module.const_get(name) if name.kind_of_label?
47
+ return found if found.is_a?(Module)
48
+ rescue
49
+ false
50
+ end
51
+
52
+ def try_module_only name
53
+ return name if is_module?(name)
54
+ found = Module.const_get(name) if name.kind_of_label?
55
+ return found if is_module?(found)
56
+ rescue
57
+ false
58
+ end
59
+
60
+
61
+ def find_first_class *names
62
+ classes = names.flatten.compact.uniq.inject([]) do |res, class_name|
63
+ found_class = try_class(class_name.to_s.camelize)
64
+ res << found_class if found_class
65
+ res
66
+ end
67
+ raise "Not one Class for any of: #{names} is currently loaded" if classes.empty?
68
+ classes.first
69
+ end
70
+
71
+ def find_first_module *names
72
+ modules = names.flatten.compact.uniq.inject([]) do |res, class_name|
73
+ found_class = try_class(class_name.to_s.camelize)
74
+ res << found_class if found_class
75
+ res
76
+ end
77
+ raise "Not one Module for any of: #{names} is currently loaded" if modules.empty?
78
+ modules.first
79
+ end
80
+ end
@@ -1,4 +1,5 @@
1
1
  require "active_support/inflector"
2
+ require "sugar-high/enumerable"
2
3
 
3
4
  class Object
4
5
  def kinda_file?
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+ require 'sugar-high/class_ext'
3
+
4
+ class Trial
5
+ include ClassExt
6
+ end
7
+
8
+ class Hello
9
+ end
10
+
11
+ module GoodBye
12
+ end
13
+
14
+ def trial
15
+ @trial ||= Trial.new
16
+ end
17
+
18
+ describe ClassExt do
19
+ describe '#try_module' do
20
+ it "should return false if no module found" do
21
+ trial.try_module('Blip').should be_false
22
+ trial.try_module(:Blip).should be_false
23
+ trial.try_module(nil).should be_false
24
+ end
25
+
26
+ it "should return module if found" do
27
+ trial.try_module('GoodBye').should be_a(Module)
28
+ trial.try_module(:GoodBye).should be_a(Module)
29
+ end
30
+
31
+ it "should return false if only class of that name is found" do
32
+ trial.try_module('Hello').should be_true
33
+ end
34
+ end
35
+
36
+ describe '#try_class' do
37
+ it "should return false if no class found" do
38
+ trial.try_class('Blip').should be_false
39
+ trial.try_class(:Blip).should be_false
40
+ trial.try_class(nil).should be_false
41
+ end
42
+
43
+ it "should return class if found" do
44
+ trial.try_class('Hello').should be_a(Class)
45
+ trial.try_class(:Hello).should be_a(Class)
46
+ end
47
+
48
+ it "should return false if only class of that name is found" do
49
+ trial.try_class('GoodBye').should be_false
50
+ end
51
+ end
52
+
53
+ describe '#class_exists?' do
54
+ it "should return false if no class found" do
55
+ trial.class_exists?('Blip').should be_false
56
+ end
57
+
58
+ it "should return true if class found" do
59
+ trial.class_exists?('Hello').should be_true
60
+ end
61
+
62
+ it "should return false if module found" do
63
+ trial.class_exists?('GoodBye').should be_false
64
+ end
65
+ end
66
+
67
+ describe '#module_exists?' do
68
+ it "should return false if no module found" do
69
+ trial.module_exists?('Blip').should be_false
70
+ end
71
+
72
+ it "should return true if module found" do
73
+ trial.module_exists?('GoodBye').should be_true
74
+ end
75
+
76
+ it "should return false if only class found" do
77
+ trial.module_exists?('Hello').should be_false
78
+ end
79
+ end
80
+ end
data/sugar-high.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sugar-high}
8
- s.version = "0.4.4.2"
8
+ s.version = "0.4.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Kristian Mandrup}]
12
- s.date = %q{2011-06-07}
12
+ s.date = %q{2011-06-09}
13
13
  s.description = %q{More Ruby sugar - inspired by the 'zuker' project}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -68,6 +68,7 @@ Gem::Specification.new do |s|
68
68
  "spec/sugar-high/arguments_spec.rb",
69
69
  "spec/sugar-high/array_spec.rb",
70
70
  "spec/sugar-high/blank_spec.rb",
71
+ "spec/sugar-high/class_ext_spec.rb",
71
72
  "spec/sugar-high/file/file_dsl_spec.rb",
72
73
  "spec/sugar-high/file_mutate/append_content_spec.rb",
73
74
  "spec/sugar-high/file_mutate/delete_spec.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugar-high
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4.2
4
+ version: 0.4.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-07 00:00:00.000000000Z
12
+ date: 2011-06-09 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2154025940 !ruby/object:Gem::Requirement
16
+ requirement: &2153304260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '2.5'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2154025940
24
+ version_requirements: *2153304260
25
25
  description: More Ruby sugar - inspired by the 'zuker' project
26
26
  email: kmandrup@gmail.com
27
27
  executables: []
@@ -81,6 +81,7 @@ files:
81
81
  - spec/sugar-high/arguments_spec.rb
82
82
  - spec/sugar-high/array_spec.rb
83
83
  - spec/sugar-high/blank_spec.rb
84
+ - spec/sugar-high/class_ext_spec.rb
84
85
  - spec/sugar-high/file/file_dsl_spec.rb
85
86
  - spec/sugar-high/file_mutate/append_content_spec.rb
86
87
  - spec/sugar-high/file_mutate/delete_spec.rb