hashrush 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3e3100a86aae0caff1da4e47129fade0ea13fdda
4
- data.tar.gz: d75d4162aeb799fbbaf64ac4e9fa8f3eb760baf9
3
+ metadata.gz: e88619fd2ae0dbc20fbde065a874b64c2fb23f92
4
+ data.tar.gz: 418552dd2bcbf06f00637214640968c57d1b3223
5
5
  SHA512:
6
- metadata.gz: fa2074173edf76e7a019a4d7ce975219fc2a28c31bce9d1d6fb0822b74db7dd86004eeea6e0842499697c37dca4631856c6e41983fb712f37f9080c9a59e974c
7
- data.tar.gz: e2f63468c05bf6ac4f8236544b18d0fc527f47538e0dbb2dcb3198845b6749134dc132b2d87f18f818f1a52107d215bbf11947419418b39ec9bd5eed60e20b7a
6
+ metadata.gz: f6b4965eeea1acd0d5938c1e3e37edb5ab6188f7c14e4057cb27e3e99b7a1966064fb1ac9bad8fffa71aaf8a240e4e7b85f153208bc5bda4f795fbc9a44db446
7
+ data.tar.gz: fb36a0f8e6e71875b5c4dc6441e43991d19caa5578e72df91d6223c8bd856bd879399ba3538bee2f7bdebff6cd54ede1b78cf623d4245872f260b59906243d7e
data/README.md CHANGED
@@ -5,7 +5,7 @@ Use when building hashes from pre-loaded variables to avoid repetiton.
5
5
 
6
6
  ## Installation
7
7
 
8
- `gem 'hashrush'` and `bundle`
8
+ `gem 'hashrush', '~> 2.0.0'` and `bundle`
9
9
 
10
10
  ## Usage
11
11
 
@@ -27,13 +27,22 @@ Hashrush shortens step 2. Use like this:
27
27
 
28
28
  ```ruby
29
29
  # Pre-load values, trivialized here
30
- name = @user.name
31
- age = @user.age
30
+ name = "Bill"
31
+ @age = 21 # since v2
32
32
 
33
33
  # Build hash
34
- hash = Hash.rush(binding, :name, :age)
34
+ hash = Hash.rush(binding, :name, :@age)
35
35
  # symbol arguments can also be packed in an array
36
- hash = Hash.rush(binding, [:name, :age])
36
+ hash = Hash.rush(binding, [:name, :@age])
37
+ # symbol arguments can also use symbol array shorthand
38
+ hash = Hash.rush(binding,
39
+ %i|name @age|
40
+ )
41
+
42
+ hash
43
+ #=> {name: "Bill", age: 21}
44
+
45
+ # NB, rushing will strip the '@' from instance variables and '@@' from class variables
37
46
 
38
47
  # Use hash in new object instantiation
39
48
  @view_variable = GemNamespace::Class.new(hash)
data/hashrush.gemspec CHANGED
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "hashrush"
7
7
  spec.date = Date.today.to_s
8
- spec.version = "1.0.0"
8
+ spec.version = "2.0.0"
9
9
  spec.authors = ["Epigene"]
10
10
  spec.email = ["augusts.bautra@gmail.com"]
11
11
 
data/lib/hashrush.rb CHANGED
@@ -2,11 +2,23 @@ module Hashrush
2
2
 
3
3
  class ::Hash
4
4
  def self.build_from_symbol_variables(binding, *args)
5
+ raise ArgumentError.new("Oops, expected the first argument to be a binding object. Try `Hash.rush(binding, :some_variable_name)`") if binding.class != Binding
6
+
5
7
  hash = Hash.new
6
- args = args.flatten if args.class == Array
7
- args.each do |arg|
8
- raise "Argments must all be symbols!" unless arg.class == Symbol
9
- hash[arg] = binding.eval("#{arg.to_s}")# if is_variable?(arg)
8
+ args = args.flatten if args.respond_to?(:flatten)
9
+ args.each_with_index do |arg, i|
10
+ raise ArgumentError.new("Oops, argument #{i+1} is not a symbol") unless arg.class == Symbol
11
+ # remove leading '@' symbols from variables
12
+ clean_key = arg.to_s.gsub(/^\@+/, '').to_sym
13
+ begin
14
+ if hash[clean_key] == nil
15
+ hash[clean_key] = binding.eval("#{arg.to_s}")# if is_variable?(arg)
16
+ else
17
+ raise ArgumentError.new("Oops, argument collision detected. :@#{clean_key} and :#{clean_key} at the same time will not work")
18
+ end
19
+ rescue NameError
20
+ raise ArgumentError.new("Oops, looks like the given binding does not have a variable '#{clean_key}'.")
21
+ end
10
22
  end
11
23
  return hash
12
24
  end
@@ -17,7 +29,3 @@ module Hashrush
17
29
  end
18
30
 
19
31
  end
20
-
21
-
22
-
23
-
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hashrush
2
- VERSION = "1.0.0"
2
+ VERSION = "2.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashrush
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Epigene
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-20 00:00:00.000000000 Z
11
+ date: 2016-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  requirements: []
93
93
  rubyforge_project:
94
- rubygems_version: 2.4.6
94
+ rubygems_version: 2.2.2
95
95
  signing_key:
96
96
  specification_version: 4
97
97
  summary: Simplifies building hashes from pre-loaded variables