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 +4 -4
- data/README.md +14 -5
- data/hashrush.gemspec +1 -1
- data/lib/hashrush.rb +16 -8
- data/lib/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e88619fd2ae0dbc20fbde065a874b64c2fb23f92
|
4
|
+
data.tar.gz: 418552dd2bcbf06f00637214640968c57d1b3223
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 =
|
31
|
-
age =
|
30
|
+
name = "Bill"
|
31
|
+
@age = 21 # since v2
|
32
32
|
|
33
33
|
# Build hash
|
34
|
-
hash = Hash.rush(binding, :name,
|
34
|
+
hash = Hash.rush(binding, :name, :@age)
|
35
35
|
# symbol arguments can also be packed in an array
|
36
|
-
hash = Hash.rush(binding, [:name,
|
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 = "
|
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.
|
7
|
-
args.
|
8
|
-
raise "
|
9
|
-
|
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
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:
|
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:
|
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.
|
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
|