Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: lukaszkorecki/super-envy
base: d09edc6337
...
head fork: lukaszkorecki/super-envy
compare: 6ab1b054e5
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
View
28 lib/super-envy/extender.rb
@@ -4,8 +4,7 @@ class Extender
def initialize hash
@hash = hash
can_be_upgraded?
- create_accessors!
-
+ create_methods!
end
private
@@ -13,14 +12,29 @@ def can_be_upgraded?
raise "This object is not a hash!" unless @hash.is_a? Hash
end
- def create_accessors!
+ def create_methods!
@hash.keys.map {|k| [k.to_s.gsub('-', '_').to_sym , k ] }.each do |name,original_key|
+ add_methods(name, original_key)
+ end
+ end
+
+ private
+ def add_methods name, key
+ acc = proc do |&b|
+ v = @hash.fetch key
+ b ? b.call(v) : v
+ end
+ def_meth name, &acc
- puts "definiingin method: #{name}"
- puts "with keyj #{original_key}"
- puts "and value #{@hash[original_key]}"
- self.define_method(name) { @hash[original_key] }
+ chk = proc do |&b|
+ v = @hash.fetch key
+ not (v.nil? or v.empty?)
end
+ def_meth :"#{name}?", &chk
+ end
+
+ def def_meth name, &block
+ self.class.send(:define_method, name, &block)
end
end
end
View
18 spec/behaviour_spec.rb
@@ -1,18 +0,0 @@
-require 'spec_helper'
-
-describe 'Top-level wrapper' do
-
- it 'can be attached to any hash instance' do
- h = {:hi => 'how are you'}
-
- e = SuperEnvy::Extender.new h
-
- e.should respond_to :hi
-
- end
-
-
- it 'throws an exception if it doesnt receive a hash' do
- expect { SuperEnvy::Extender.new "lol"}.should raise_error
- end
-end
View
38 spec/extender_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+describe 'Extender' do
+
+ context 'Base' do
+ it 'throws an exception if it doesnt receive a hash' do
+ expect { SuperEnvy::Extender.new "lol"}.should raise_error
+ end
+
+ it 'can be attached to any hash instance' do
+ h = {:hi => 'how are you'}
+ e = SuperEnvy::Extender.new h
+ e.should respond_to :hi
+ end
+ end
+
+
+
+ context 'Dynamicaly created methods' do
+ let(:hash) { {:oi => 'yo', :foo => 'bar', :bar => ''} }
+ let(:ext) { SuperEnvy::Extender.new hash }
+
+ it 'defines accessor method' do
+ ext.oi.should == 'yo'
+ end
+
+ it 'defines question-marked boolean checker method (or whatever the name is) which checks if a variable is blank' do
+ ext.oi?.should == true
+ ext.bar?.should == false
+ end
+
+
+ it 'defines accesor method which accepts a block and returns result of that block' do
+ ext.foo { |val| val.reverse }.should == 'rab'
+
+ end
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.