hash_attr 0.1.0

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: 8d9019eba08351ba03dca8bcf0ea53050cdc478f
4
+ data.tar.gz: eb2596c1bbcb11e960b41a77db4ffb05350f54c2
5
+ SHA512:
6
+ metadata.gz: b1b68c1d9b97d3079e7ebdd54dfcc8f45d07a20f65c0e3df28b4fd6d169e499f752b271a5dfdfd10f015780f2c369cca830f0322006625ef9cf6431e50415051
7
+ data.tar.gz: 1b6774ee30e7cdf020882681f1eb58e4ce5c9582ca24c7ebd6e4c89c8882eb309082cd8158a926496c35ada4a35eef0ef2594308d0865d5164c7d3f1c5b14c16
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .idea
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 SecondMarket Holdings, Inc
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ #hash_attr
2
+
3
+ Declare attributes as hash-attr to simply generate an Hash of their names and values.
4
+
5
+ ##Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'hash_attr'
10
+ And then execute:
11
+
12
+ $ bundle
13
+ Or install it yourself as:
14
+
15
+ $ gem install hash_attr
16
+
17
+ ##Usage
18
+
19
+ class Example
20
+ include HashAttr
21
+ hash_attr :marco, :tagada
22
+ attr_accessor :hello
23
+ end
24
+
25
+ example = Example.new
26
+
27
+ example.marco = 'polo'
28
+ example.tagada = 'tsoin'
29
+ example.hello= 'world'
30
+
31
+ example.marco
32
+ => "polo"
33
+
34
+ example.tagada
35
+ => "tsoin"
36
+
37
+ example.hello
38
+ => "world"
39
+
40
+ example.attributes
41
+ =>{:marco=>"polo", :tagada=>"tsoin"}
42
+
43
+
@@ -0,0 +1 @@
1
+ require 'hash_attr'
data/hash_attr.gemspec ADDED
@@ -0,0 +1,14 @@
1
+ require File.expand_path('../lib/hash_attr/version', __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'hash_attr'
5
+ s.version = HashAttr::VERSION
6
+ s.date = '2014-05-05'
7
+ s.summary = 'get/set designated attributes from/to a hash'
8
+ s.description = 'get/set designated attributes from/to a hash'
9
+ s.authors = ['SecondMarket']
10
+ s.email = 'engineering@secondmarket.com'
11
+ s.files = `git ls-files`.split($\)
12
+ s.homepage = 'https://github.com/secondmarket/hash-attr'
13
+ s.license = 'MIT'
14
+ end
@@ -0,0 +1,3 @@
1
+ module HashAttr
2
+ VERSION = "0.1.0"
3
+ end
data/lib/hash_attr.rb ADDED
@@ -0,0 +1,42 @@
1
+ module HashAttr
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ def hash_attr(*attrs)
8
+ attrs.each do |attr|
9
+ define_method attr.to_s do
10
+ attribute_hash[attr]
11
+ end
12
+
13
+ define_method "#{attr.to_s}=" do |value|
14
+ attribute_hash[attr] = value
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ def attribute_hash
21
+ @attribute_hash ||= {}
22
+ end
23
+
24
+ def attributes
25
+ Hash[@attribute_hash.map { |key, value| [key, value(value)] }]
26
+ end
27
+
28
+ private
29
+ def value(object)
30
+ hash_attr = lambda { |object| object.respond_to? :attribute_hash }
31
+ mappable = lambda { |object| object.respond_to? :map }
32
+
33
+ case object
34
+ when hash_attr then
35
+ object.attributes
36
+ when mappable then
37
+ object.map { |val| value(val) }
38
+ else
39
+ object
40
+ end
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_attr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - SecondMarket
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: get/set designated attributes from/to a hash
14
+ email: engineering@secondmarket.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".gitignore"
20
+ - LICENSE.md
21
+ - README.md
22
+ - config/initializers/hash_attr.rb
23
+ - hash_attr.gemspec
24
+ - lib/hash_attr.rb
25
+ - lib/hash_attr/version.rb
26
+ homepage: https://github.com/secondmarket/hash-attr
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.2.1
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: get/set designated attributes from/to a hash
50
+ test_files: []