instance_variables_from 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0c1b5234381aae0860d06d43bdb44ed1708024d5
4
+ data.tar.gz: 70de5548ad733615f0a22a75bc7157f39361a3ae
5
+ SHA512:
6
+ metadata.gz: 023a8cfab7f4f2c2d028d17c1b82d7564ec9935a448ff65dfe22e9f2028564dd4f6fdd53721fd97467f70894de50a70e4ed83b3c64bb2682284e14040650bb57
7
+ data.tar.gz: 99464d3bb20a3da6c3ff6c36244ab5f23ada83e281f4bee6a0fdd910fc2b736b32adeecc2360c61776fc5bfc4f54794174c1d9cb08202aafcf99736b41effb61
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ /pkg
@@ -0,0 +1,20 @@
1
+ sudo: false
2
+ language: ruby
3
+
4
+ script: bundle exec ruby spec/instance_variables_from_spec.rb
5
+
6
+ rvm:
7
+ - 2.2
8
+ - 2.1
9
+ - 2.0
10
+ - rbx-2
11
+ - jruby-head
12
+ - jruby-9000
13
+
14
+ cache:
15
+ - bundler
16
+
17
+ matrix:
18
+ fast_finish: true
19
+ allow_failures:
20
+ - rvm: jruby-9000
@@ -0,0 +1,8 @@
1
+ ## CHANGELOG
2
+
3
+ ### 1.0.0
4
+
5
+ * Moved from zucker gem into its own gem
6
+ * Make it a Kernel method
7
+ * Minor refactoring, only accept symbols for whitelist
8
+ * More specific about arrays
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'minitest'
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010-2016 Jan Lelis, mail@janlelis.de
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ # Kernel#instance_variables_from [![[version]](https://badge.fury.io/rb/instance_variables_from.svg)](http://badge.fury.io/rb/instance_variables_from) [![[travis]](https://travis-ci.org/janlelis/instance_variables_from.png)](https://travis-ci.org/janlelis/instance_variables_from)
2
+
3
+ Automatically turn bindings, hashes or arrays into instance variables. Instead of:
4
+
5
+ ```ruby
6
+ def initialize(a, b)
7
+ @a = a
8
+ @b = b
9
+ end
10
+ ```
11
+
12
+ You can write:
13
+
14
+ ```ruby
15
+ def initialize(a, b)
16
+ instance_variables_from binding # will assign @a and @b
17
+ end
18
+ ```
19
+
20
+ It also works with hashes:
21
+
22
+ ```ruby
23
+ params = { c: 3, d: 4 }
24
+ instance_variables_from params # will assign @c and @d
25
+ ```
26
+
27
+ It also works with arrays:
28
+
29
+ ```ruby
30
+ list = %w[instance variable]
31
+ instance_variables_from list # will assign @_0 and @_1
32
+ ```
33
+
34
+ When you pass additional arguments, they will be interpreted as whitelist:
35
+
36
+ ```ruby
37
+ params = { c: 3, d: 4 }
38
+ instance_variables_from params, :c # will only assign @c
39
+ ```
40
+
41
+ ## Setup
42
+
43
+ Add to your `Gemfile`:
44
+
45
+ ```ruby
46
+ gem 'instance_variables_from'
47
+ ```
48
+
49
+
50
+ ## MIT License
51
+
52
+ Copyright (C) 2010-2016 Jan Lelis <http://janlelis.com>. Released under the MIT license.
@@ -0,0 +1,30 @@
1
+ # # #
2
+ # Get gemspec info
3
+
4
+ gemspec_file = Dir['*.gemspec'].first
5
+ gemspec = eval File.read(gemspec_file), binding, gemspec_file
6
+ info = "#{gemspec.name} | #{gemspec.version} | " \
7
+ "#{gemspec.runtime_dependencies.size} dependencies | " \
8
+ "#{gemspec.files.size} files"
9
+
10
+
11
+ # # #
12
+ # Gem build and install task
13
+
14
+ desc info
15
+ task :gem do
16
+ puts info + "\n\n"
17
+ print " "; sh "gem build #{gemspec_file}"
18
+ FileUtils.mkdir_p 'pkg'
19
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
20
+ puts; sh %{gem install --no-document pkg/#{gemspec.name}-#{gemspec.version}.gem}
21
+ end
22
+
23
+
24
+ # # #
25
+ # Start an IRB session with the gem loaded
26
+
27
+ desc "#{gemspec.name} | IRB"
28
+ task :irb do
29
+ sh "irb -I ./lib -r #{gemspec.name.gsub '-','/'}"
30
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) + "/lib/instance_variables_from/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "instance_variables_from"
7
+ gem.version = InstanceVariablesFrom::VERSION
8
+ gem.summary = "Turn bindings, hashes or arrays into instance variables."
9
+ gem.description = "Automatically turn bindings, hashes or arrays into instance variables."
10
+ gem.authors = ["Jan Lelis"]
11
+ gem.email = ["mail@janlelis.de"]
12
+ gem.homepage = "https://github.com/janlelis/instance_variables_from"
13
+ gem.license = "MIT"
14
+
15
+ gem.files = Dir["{**/}{.*,*}"].select{ |path| File.file?(path) && path !~ /^pkg/ }
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.required_ruby_version = "~> 2.0"
21
+ end
@@ -0,0 +1,30 @@
1
+ require_relative "instance_variables_from/version"
2
+
3
+ module Kernel
4
+ private
5
+
6
+ def instance_variables_from(obj, *whitelist)
7
+ values_to_assign = case obj
8
+ when Binding
9
+ obj.eval('local_variables').map{ |e| [obj.eval("#{e}"), e] }
10
+ when Hash
11
+ obj.map{|k,v| [v,k] }
12
+ when Array
13
+ obj.each.with_index
14
+ else
15
+ raise ArgumentError, "cannot extract instance_variables from #{obj}"
16
+ end
17
+
18
+ unless whitelist.empty?
19
+ values_to_assign.select!{ |value, key| whitelist.include? key.to_sym }
20
+ end
21
+
22
+ values_to_assign.map{ |value, key|
23
+ key = key.to_s
24
+ ivar_name = :"@#{'_' if key =~ /\A\d/}#{key}"
25
+ instance_variable_set(ivar_name, value)
26
+ ivar_name
27
+ }
28
+ end
29
+ end
30
+
@@ -0,0 +1,4 @@
1
+ module InstanceVariablesFrom
2
+ VERSION = "1.0.0".freeze
3
+ end
4
+
@@ -0,0 +1,47 @@
1
+ require_relative "../lib/instance_variables_from"
2
+ require "minitest/autorun"
3
+
4
+ describe 'Kernel#instance_variables_from' do
5
+ it 'transforms the given parameter to instance variables when in it is a binding' do
6
+ def example_method(a = 1, b = 2)
7
+ instance_variables_from binding
8
+ end
9
+
10
+ example_method
11
+ assert_equal 1, @a
12
+ assert_equal 2, @b
13
+ end
14
+
15
+ it 'transforms the given parameter to instance variables when in it is a hash' do
16
+ params = { c: 3, d: 4 }
17
+ instance_variables_from params
18
+
19
+ assert_equal 3, @c
20
+ assert_equal 4, @d
21
+ end
22
+
23
+ it 'transforms the given parameter to instance variables when in it is an array' do
24
+ list = %w[instance variable]
25
+ instance_variables_from list
26
+ assert_equal "instance", @_0
27
+ assert_equal "variable", @_1
28
+ end
29
+
30
+ it 'takes a whitelist as splat param' do
31
+ params = { c: 3, d: 4 }
32
+ instance_variables_from params, :c
33
+
34
+ assert_equal 3, @c
35
+ assert_equal false, instance_variables.include?(:@d)
36
+ end
37
+
38
+ it 'returns the instance variable names assigned' do
39
+ assert_equal [:@c, :@d], instance_variables_from({ c: 3, d: 4 })
40
+ end
41
+
42
+ it 'raises an ArgumenError for unknown objects to extract from' do
43
+ assert_raises ArgumentError do
44
+ instance_variables_from(nil)
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: instance_variables_from
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan Lelis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Automatically turn bindings, hashes or arrays into instance variables.
14
+ email:
15
+ - mail@janlelis.de
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".travis.yml"
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - MIT-LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - instance_variables_from.gemspec
28
+ - lib/.instance_variables_from.rb.swp
29
+ - lib/instance_variables_from.rb
30
+ - lib/instance_variables_from/version.rb
31
+ - spec/instance_variables_from_spec.rb
32
+ homepage: https://github.com/janlelis/instance_variables_from
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - "~>"
43
+ - !ruby/object:Gem::Version
44
+ version: '2.0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.5.1
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Turn bindings, hashes or arrays into instance variables.
56
+ test_files:
57
+ - spec/instance_variables_from_spec.rb