hash_attribute_assignment 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9165a497c6e8a9d56defc417249016c13f3ed586
4
+ data.tar.gz: d4bfaf653e01f64ee8beba36afa9d31df439198c
5
+ SHA512:
6
+ metadata.gz: be0106f885e249cc25815b64f9f375e9117a67a6d8434a30cf6b5b44eb883a18a749d00bae4a5d8cc7713b6850236ae117d194c8cd961e0e00b7d4855492d77c
7
+ data.tar.gz: c340e833cf71af71ad8cc7220908f6dda69f6ffa28e532e27dfe76e2b20b200e89aee6d51878c61d99200197b8f89e5effe11746f57ea998f9e5d3f96d277b7c
data/.gitignore ADDED
@@ -0,0 +1,39 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ ## Specific to RubyMotion:
14
+ .dat*
15
+ .repl_history
16
+ build/
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ ## Environment normalization:
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
37
+
38
+ # RubyMine
39
+ .idea/
data/.hound.yml ADDED
@@ -0,0 +1,3 @@
1
+ inherit_from: .rubocop.yml
2
+
3
+ fail_on_violations: true
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'vendor/**/*'
4
+ - 'db/migrate/**/*'
5
+ - 'db/schema.rb'
6
+
7
+ Metrics/LineLength:
8
+ Max: 120
9
+
10
+ Documentation:
11
+ Enabled: false
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Corey Alexander
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # hash_attribute_assignment
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
+ require 'hash_attribute_assignment/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'hash_attribute_assignment'
6
+ s.summary = 'Instantiate objects with a hash of instance variables'
7
+ s.version = HashAttributeAssignment::VERSION
8
+ s.author = 'Corey Alexander'
9
+ s.email = 'coreyja@gmail.com'
10
+ s.files = `git ls-files`.split("\n")
11
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
12
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
13
+ s.require_paths = ['lib']
14
+ s.license = 'MIT'
15
+ s.homepage = 'https://github.com/coreyja/hash_attribute_assignment'
16
+
17
+ s.required_ruby_version = '>= 2.1.0'
18
+ end
@@ -0,0 +1,75 @@
1
+ module HashAttributeAssignment
2
+ module ClassMethods
3
+ HashValidation = Struct.new(:proc, :message)
4
+
5
+ def validate_hash(proc, options = { message: 'Hash failed validation' })
6
+ raise ArgumentError('First arg must be a call-able') unless proc.respond_to? :call
7
+ class_variable_set(:@@hash_validations, hash_validations + [HashValidation.new(proc, options[:message])])
8
+ end
9
+
10
+ private
11
+
12
+ def hash_validations
13
+ class_variable_defined?(:@@hash_validations) ? class_variable_get(:@@hash_validations) : []
14
+ end
15
+ end
16
+ class HashValidationError < StandardError; end
17
+ class HashAttributeAssignor
18
+ def initialize(instance, hash = {})
19
+ @hash = hash
20
+ @klass = instance.class
21
+ @instance = instance
22
+ @hash_validations = klass.class_variable_get(:@@hash_validations)
23
+ end
24
+
25
+ def assign
26
+ check_required_keys if klass.const_defined?(:REQUIRED_KEYS)
27
+ merge_default_hash! if klass.const_defined?(:DEFAULT_HASH)
28
+ validate_hash!
29
+ assign_asstributes
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :klass, :instance, :hash_validations
35
+ attr_accessor :hash
36
+
37
+ def assign_asstributes
38
+ hash.each do |key, value|
39
+ instance.instance_variable_set("@#{key}".to_sym, value)
40
+ end
41
+ end
42
+
43
+ def check_required_keys
44
+ required_keys.each do |key|
45
+ raise ArgumentError, "Required key '#{key}' missing for #{klass}" unless hash.key? key
46
+ end
47
+ end
48
+
49
+ def merge_default_hash!
50
+ self.hash = default_hash.merge(hash)
51
+ end
52
+
53
+ def validate_hash!
54
+ hash_validations.each do |_validation|
55
+ raise HashValidationError, validation.message unless validation.proc.call hash
56
+ end
57
+ end
58
+
59
+ def required_keys
60
+ klass.const_get(:REQUIRED_KEYS)
61
+ end
62
+
63
+ def default_hash
64
+ klass.const_get(:DEFAULT_HASH)
65
+ end
66
+ end
67
+
68
+ def self.included(base)
69
+ base.extend ClassMethods
70
+ end
71
+
72
+ def initialize(hash = {})
73
+ HashAttributeAssignor.new(self, hash).assign
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module HashAttributeAssignment
2
+ VERSION = '0.0.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_attribute_assignment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Corey Alexander
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: coreyja@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".gitignore"
20
+ - ".hound.yml"
21
+ - ".rubocop.yml"
22
+ - LICENSE
23
+ - README.md
24
+ - hash_attribute_assignment.gemspec
25
+ - lib/hash_attribute_assignment.rb
26
+ - lib/hash_attribute_assignment/version.rb
27
+ homepage: https://github.com/coreyja/hash_attribute_assignment
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 2.1.0
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.5.1
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Instantiate objects with a hash of instance variables
51
+ test_files: []