blizz 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/Readme.md +24 -0
  2. data/lib/blizz.rb +57 -0
  3. metadata +48 -0
data/Readme.md ADDED
@@ -0,0 +1,24 @@
1
+ # Blizz
2
+ [ruby]
3
+
4
+ Blizz let's you easily load hashes into objects, adding accessors, without modifying too much your base class
5
+
6
+ ```ruby
7
+ class Example
8
+ include Blizz::Resource
9
+ end
10
+
11
+ hash = { a: "b", c: "d" }
12
+
13
+ example = Blizz.load Example, hash
14
+ p example.a # => "b"
15
+ p example.c # => "d"
16
+ p example.class # => Example
17
+ ```
18
+
19
+ Simple isn't it?
20
+
21
+ Useful for prototyping domain models, DDD, etc :)
22
+
23
+
24
+ TODO: release it as a gem asap :)
data/lib/blizz.rb ADDED
@@ -0,0 +1,57 @@
1
+ class Blizz
2
+
3
+ VERSION = "0.1"
4
+
5
+ def self.load(klass, hash)
6
+ page = klass.new
7
+ page.load hash
8
+ page
9
+ end
10
+
11
+ module Resource
12
+
13
+ def klass
14
+ self.class
15
+ end
16
+
17
+ def load(hash)
18
+ klass.create_accessors hash
19
+ load_contents hash
20
+ end
21
+
22
+ def self.included(mod)
23
+ mod.send :extend, ClassMethods
24
+ end
25
+
26
+ module ClassMethods
27
+ def create_accessors(hash)
28
+ attr_accessor *hash.keys
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def load_contents(hash)
35
+ hash.map do |key, val|
36
+ if array_of_hashes? val
37
+ val = replace_contents val
38
+ end
39
+
40
+ instance_variable_set "@#{key}", val
41
+ end
42
+ end
43
+
44
+ def array_of_hashes?(val)
45
+ val.is_a?(Array) && val.first.is_a?(Hash)
46
+ end
47
+
48
+ def replace_contents(array)
49
+ array.map do |hash|
50
+ klass = Object.const_get hash[:type]
51
+ Blizz.load klass, hash
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blizz
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Francesco 'makevoid' Canessa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! ' Blizz let''s you easily load hashes into objects
15
+
16
+ '
17
+ email: makevoid@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - Readme.md
23
+ - lib/blizz.rb
24
+ homepage: http://github.com/makevoid/blizz
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.23
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Easily load hashes into objects
48
+ test_files: []