├── .gitignore ├── Gemfile ├── lib ├── instance_variables_from │ └── version.rb └── instance_variables_from.rb ├── CHANGELOG.md ├── .github └── workflows │ └── test.yml ├── instance_variables_from.gemspec ├── MIT-LICENSE.txt ├── Rakefile ├── README.md └── spec └── instance_variables_from_spec.rb /.gitignore: -------------------------------------------------------------------------------- 1 | Gemfile.lock 2 | /pkg 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | 5 | gem 'minitest' 6 | gem 'rake' 7 | -------------------------------------------------------------------------------- /lib/instance_variables_from/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module InstanceVariablesFrom 4 | VERSION = "1.0.1" 5 | end 6 | 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## CHANGELOG 2 | 3 | ### 1.0.1 4 | 5 | * Relax Ruby version requirement to allow Ruby 3.0 6 | 7 | ### 1.0.0 8 | 9 | * Moved from zucker gem into its own gem 10 | * Make it a Kernel method 11 | * Minor refactoring, only accept symbols for whitelist 12 | * More specific about arrays 13 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | name: Ruby ${{ matrix.ruby }} (${{ matrix.os }}) 8 | if: "!contains(github.event.head_commit.message, '[skip ci]')" 9 | strategy: 10 | matrix: 11 | ruby: 12 | - 3.0 13 | - 2.7 14 | - 2.6 15 | - 2.5 16 | - jruby-9.2.13.0 17 | - truffleruby-20.3.0 18 | os: 19 | - ubuntu-latest 20 | runs-on: ${{matrix.os}} 21 | steps: 22 | - uses: actions/checkout@v2 23 | - name: Set up Ruby 24 | uses: ruby/setup-ruby@v1 25 | with: 26 | ruby-version: ${{matrix.ruby}} 27 | bundler-cache: true 28 | - name: Run tests 29 | run: bundle exec rake 30 | -------------------------------------------------------------------------------- /lib/instance_variables_from.rb: -------------------------------------------------------------------------------- 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 | 31 | -------------------------------------------------------------------------------- /instance_variables_from.gemspec: -------------------------------------------------------------------------------- 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 = ["hi@ruby.consulting"] 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 22 | -------------------------------------------------------------------------------- /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010-2016 Jan Lelis, https://janlelis.com 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. 21 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 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 31 | 32 | 33 | # # # 34 | # Run Specs 35 | 36 | desc "#{gemspec.name} | Spec" 37 | task :spec do 38 | if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ 39 | sh "for %f in (spec/\*.rb) do ruby spec/%f" 40 | else 41 | sh "for file in spec/*.rb; do ruby $file; done" 42 | end 43 | end 44 | task default: :spec 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kernel#instance_variables_from [![[version]](https://badge.fury.io/rb/instance_variables_from.svg)](http://badge.fury.io/rb/instance_variables_from) [![[ci]](https://github.com/janlelis/instance_variables_from/workflows/Test/badge.svg)](https://github.com/janlelis/instance_variables_from/actions?query=workflow%3ATest) 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 . Released under the MIT license. 53 | -------------------------------------------------------------------------------- /spec/instance_variables_from_spec.rb: -------------------------------------------------------------------------------- 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 48 | --------------------------------------------------------------------------------