ocioso 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d2498705c550427b0f715e3d5ddfa349e32d875c
4
+ data.tar.gz: 70f9242b356988243d5ecc808bf18507872f4aa3
5
+ SHA512:
6
+ metadata.gz: a09486e7eb9968baa8139ad2b82f36fb7c826519c21927e8dc1fe1cfa2c423767fd8e4061892b79e8811a22f4d394e7e0e618d40b0865b3c1d5778dc2454f79b
7
+ data.tar.gz: 2f82056475cfe684ca1e21f57d0b6fab9284fc71983b546748a28cf33d6837cd25b07964c5ebc1604679014c3842f1987304147eb215edce6bbc49ff0032dda3
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Julio Lopez
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,110 @@
1
+ # Ocioso
2
+
3
+ Micro Library for initialize objects
4
+
5
+ ## Installation
6
+
7
+ Installing Ocioso is as simple as running:
8
+
9
+ ```
10
+ $ gem install ocioso
11
+ ```
12
+
13
+ Include Ocioso in your Gemfile with gem 'ocioso' or require it with require 'ocioso'.
14
+
15
+ Usage
16
+ -----
17
+
18
+ You can automatically instance variables without write an initialize method, just send a hash with your name variables and its values when you instantiate a class.
19
+
20
+ ```ruby
21
+ class User
22
+ include Ocioso
23
+ end
24
+
25
+ user = User.new name: "Julio", email: "julio@email.com"
26
+
27
+ # This is the same thing of do:
28
+ # class User
29
+ # def initialize(name, email)
30
+ # @name = name
31
+ # @email = email
32
+ # end
33
+ # end
34
+
35
+ puts user.inspect
36
+ #=> <User @name = "Julio", @email = "julio@email.com">
37
+ ```
38
+
39
+ If you define your writer methods you can initialize your variables using a block:
40
+
41
+ ```ruby
42
+ class User
43
+ include Ocioso
44
+ attr_writer :name, :email
45
+ end
46
+
47
+ user = User.new do |user|
48
+ user.name = "Julio"
49
+ user.email = "julio@email.com"
50
+ end
51
+
52
+ puts user.inspect
53
+ #=> <User @name = "Julio", @email = "julio@email.com">
54
+ ```
55
+
56
+ ## Allow certain Values
57
+
58
+ If you want to only allow some certain variables, you can use `initialize_only_with` method. Other sent variables that haven't been specified won't be initializated.
59
+
60
+ ```ruby
61
+ class User
62
+ include Ocioso
63
+ initialize_only_with :name
64
+ end
65
+
66
+ user = User.new name: "Julio", age: 25
67
+ puts user.inspect
68
+ #=> <User @name = "Julio">
69
+ ```
70
+
71
+ ## Default Values
72
+
73
+ You can define your default values for your instance variables using the `initialize_defaults` method.
74
+
75
+ ```ruby
76
+ class User
77
+ include Ocioso
78
+ initialize_defaults name: "Julio", email: "my_email@email.com"
79
+ end
80
+
81
+ # This is the same thing of do:
82
+ # class User
83
+ # def initialize(name = "Julio", email = "my_email@email.com")
84
+ # @name = name
85
+ # @email = email
86
+ # end
87
+ # end
88
+
89
+ user = User.new
90
+ puts user.inspect
91
+ #=> <User @name = "Julio", @email = "my_email@email.com">
92
+
93
+ other_user = User.new name: "Piero"
94
+ puts other_user.inspect
95
+ #=> <User @name = "Piero", @email = "my_email@email.com">
96
+ ```
97
+
98
+ ## Open to Initialize
99
+
100
+ You can still use your initialize method to do whatever you need, just not forget to send the `super` method to handle the initialize of Ocioso.
101
+
102
+ ```ruby
103
+ class User
104
+ include Ocioso
105
+ def initialize(*args)
106
+ super(*args)
107
+ #doing whatever
108
+ end
109
+ end
110
+ ```
data/lib/ocioso.rb ADDED
@@ -0,0 +1,61 @@
1
+ # Copyright (c) 2015 Julio Lopez
2
+
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+ module Ocioso
21
+ VERSION = "1.0.0"
22
+ class << self
23
+ attr_reader :defaults, :values_allowed
24
+
25
+ def included(base)
26
+ @defaults ||= {}; @values_allowed ||= {}
27
+ base.extend ClassMethods
28
+ end
29
+
30
+ def get_values(klass)
31
+ [values_allowed[klass], defaults[klass]]
32
+ end
33
+ end
34
+
35
+ def initialize(vars = {}, &block)
36
+ super()
37
+ _a, _d = Ocioso.get_values(self.class)
38
+ vars = _d.attributes.merge(vars) if _d
39
+ vars.each do |k, v|
40
+ instance_variable_set "@#{k}", v if (_a && _a.include?(k) || _a.nil? )
41
+ end
42
+ yield self if block_given?
43
+ end
44
+
45
+ module ClassMethods
46
+ def initialize_defaults(defaults={})
47
+ Ocioso.defaults[self] = DefaultParameters.new defaults
48
+ end
49
+
50
+ def initialize_only_with(*args)
51
+ Ocioso.values_allowed[self] = args
52
+ end
53
+ end
54
+
55
+ class DefaultParameters
56
+ attr_reader :attributes
57
+ def initialize(attributes={})
58
+ @attributes = attributes
59
+ end
60
+ end
61
+ end
data/ocioso.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ require "./lib/ocioso"
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "ocioso"
5
+ s.version = Ocioso::VERSION
6
+ s.summary = "Micro Library for initialize objects"
7
+ s.description = "Micro Library for initialize objects"
8
+ s.authors = ["Julio Lopez"]
9
+ s.email = ["ljuliom@gmail.com"]
10
+ s.homepage = "http://github.com/TheBlasfem/ocioso"
11
+ s.files = Dir[
12
+ "LICENSE",
13
+ "README.md",
14
+ "lib/**/*.rb",
15
+ "*.gemspec",
16
+ "test/**/*.rb"
17
+ ]
18
+ s.license = "MIT"
19
+ s.add_development_dependency "cutest", "1.1.3"
20
+ end
@@ -0,0 +1,85 @@
1
+ require File.expand_path("../lib/ocioso", File.dirname(__FILE__))
2
+
3
+ scope do
4
+ class User
5
+ include Ocioso
6
+ attr_accessor :name, :email
7
+ end
8
+
9
+ test "init blank" do
10
+ user = User.new
11
+ assert_equal user.is_a?(User), true
12
+ assert_equal user.instance_variable_get("@name"), nil
13
+ end
14
+
15
+ test "instance variables" do
16
+ user = User.new name: "Julio", email: "julio@email.com"
17
+ assert_equal user.instance_variable_get("@name"), "Julio"
18
+ assert_equal user.instance_variable_get("@email"), "julio@email.com"
19
+ end
20
+
21
+ test "init with block" do
22
+ user = User.new do |user|
23
+ user.name = "Claudia"
24
+ user.email = "clau@email.com"
25
+ end
26
+ assert_equal user.name, "Claudia"
27
+ assert_equal user.email, "clau@email.com"
28
+ end
29
+
30
+ test "init mix, sending hash and block" do
31
+ user = User.new name: "Piero" do |user|
32
+ user.email = "piero@email.com"
33
+ end
34
+ assert_equal user.name, "Piero"
35
+ assert_equal user.email, "piero@email.com"
36
+ end
37
+
38
+ test "init with defaults" do
39
+ class User
40
+ initialize_defaults name: "Julio"
41
+ end
42
+ user = User.new
43
+ assert_equal user.instance_variable_get("@name"), "Julio"
44
+ end
45
+
46
+ test "customizing defaults" do
47
+ class User
48
+ initialize_defaults name: "Julio", email: "julio@gmail.com"
49
+ end
50
+ user = User.new name: "Piero"
51
+ assert_equal user.instance_variable_get("@name"), "Piero"
52
+ assert_equal user.instance_variable_get("@email"), "julio@gmail.com"
53
+ end
54
+
55
+ test "initialize_defaults dont work with inheritance" do
56
+ class User
57
+ initialize_defaults name: "Julio"
58
+ end
59
+ class Admin < User; end
60
+ admin = Admin.new
61
+ assert_equal admin.instance_variable_get("@name"), nil
62
+ end
63
+
64
+ test "initialize only with" do
65
+ class DemoUser
66
+ include Ocioso
67
+ initialize_only_with :name, :email
68
+ end
69
+ user = DemoUser.new name: "Julio", age: 23
70
+ assert_equal user.instance_variable_get("@name"), "Julio"
71
+ assert_equal user.instance_variable_get("@age"), nil
72
+ end
73
+
74
+ test "respecting the initialize method" do
75
+ class User
76
+ def initialize(*args)
77
+ super(*args)
78
+ @ivar = "i'm alive!"
79
+ end
80
+ end
81
+ user = User.new surname: "Lopez"
82
+ assert_equal user.instance_variable_get("@surname"), "Lopez"
83
+ assert_equal user.instance_variable_get("@ivar"), "i'm alive!"
84
+ end
85
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ocioso
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Julio Lopez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.3
27
+ description: Micro Library for initialize objects
28
+ email:
29
+ - ljuliom@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - lib/ocioso.rb
37
+ - ocioso.gemspec
38
+ - test/test_ocioso.rb
39
+ homepage: http://github.com/TheBlasfem/ocioso
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.2.2
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Micro Library for initialize objects
63
+ test_files: []