ostend 0.0.2.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ostend.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,88 @@
1
+ (From: http://www.eclipse.org/legal/epl-v10.html)
2
+
3
+ Eclipse Public License - v 1.0
4
+
5
+ THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
6
+
7
+ 1. DEFINITIONS
8
+
9
+ "Contribution" means:
10
+
11
+ a) in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and
12
+ b) in the case of each subsequent Contributor:
13
+
14
+ i) changes to the Program, and
15
+
16
+ ii) additions to the Program;
17
+
18
+ where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include additions to the Program which: (i) are separate modules of software distributed in conjunction with the Program under their own license agreement, and (ii) are not derivative works of the Program.
19
+
20
+ "Contributor" means any person or entity that distributes the Program.
21
+
22
+ "Licensed Patents " mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program.
23
+
24
+ "Program" means the Contributions distributed in accordance with this Agreement.
25
+
26
+ "Recipient" means anyone who receives the Program under this Agreement, including all Contributors.
27
+
28
+ 2. GRANT OF RIGHTS
29
+
30
+ a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form.
31
+
32
+ b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder.
33
+
34
+ c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program.
35
+
36
+ d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement.
37
+
38
+ 3. REQUIREMENTS
39
+
40
+ A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that:
41
+
42
+ a) it complies with the terms and conditions of this Agreement; and
43
+
44
+ b) its license agreement:
45
+
46
+ i) effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose;
47
+
48
+ ii) effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits;
49
+
50
+ iii) states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and
51
+
52
+ iv) states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange.
53
+
54
+ When the Program is made available in source code form:
55
+
56
+ a) it must be made available under this Agreement; and
57
+
58
+ b) a copy of this Agreement must be included with each copy of the Program.
59
+
60
+ Contributors may not remove or alter any copyright notices contained within the Program.
61
+
62
+ Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution.
63
+
64
+ 4. COMMERCIAL DISTRIBUTION
65
+
66
+ Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense.
67
+
68
+ For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages.
69
+
70
+ 5. NO WARRANTY
71
+
72
+ EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement , including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations.
73
+
74
+ 6. DISCLAIMER OF LIABILITY
75
+
76
+ EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
77
+
78
+ 7. GENERAL
79
+
80
+ If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.
81
+
82
+ If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed.
83
+
84
+ All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive.
85
+
86
+ Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved.
87
+
88
+ This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ Using this as a mix in we are able to maintain a meaningful class structure while exposing the hash keys as viable and visable methods.
2
+ I had a lot more I wanted to do in my FileCopyTask, and I didn't feel that the objectification logic was correctly placed in my class.
3
+
4
+ The really neat part about this is that you could extend an individual instance of an object and objectify it in place.
5
+ You could do all sorts of other crazy things with definition of reader/writer/accessor permissions in the objectify call.
6
+
7
+ This may be entirely useless and silly but I think it is a slightly better option than returning an object of the class Hashit/ObjectifyFromHash
8
+
9
+ OpenStruct is a viable alternative. However, I was unable to find a way to use the Plain class as a mixin.
10
+
11
+ Thoughts? Comments? Need more of the code around what I'm doing? Is their an easier, clearer way (ActiveModel if working in rails)?
12
+
13
+ .jpg
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,49 @@
1
+ module Ostend
2
+ def self.included(base)
3
+ base.send(:attr_writer, :ostend_attr_type)
4
+ base.send(:attr_writer, :ostend_attr_filter)
5
+ base.send(:attr_writer, :ostend_strict)
6
+ end
7
+
8
+ def ostendify( hash )
9
+ @ostend_attr_type ||= :accessor
10
+ @ostend_attr_filter ||= nil
11
+ @ostend_strict ||= false
12
+
13
+ ostend_raise_disallowed_attrs( hash ) if @ostend_attr_filter && @ostend_strict
14
+ ostend_create_attributes( hash )
15
+ end
16
+
17
+ private
18
+
19
+ def ostend_create_attributes( hash )
20
+ ostend_filterd_hash( hash ).each do |key,value|
21
+ temp_type = @ostend_attr_type
22
+ self.instance_variable_set("@#{key}", value)
23
+ # self.class.send("attr_#{@ostend_attr_type}", key)
24
+ # I'm basically trying to do the above.
25
+ # However, there is an issue with the above send in that it modifies the core class
26
+ # We really want to access the instance's single class like below.
27
+ # I really want this to be simple but it end up being rediculous to modify the singleton class while honoring scoped variables
28
+ # To get everything I needed into scope I have to retrieve the singleton class
29
+ # and then class_eval on it.
30
+ # Also, instance vars don't evaluate so I had to create a temporary value which will properly scope in the class_eval block.
31
+ # Hlurg
32
+ (class << self; self; end).class_eval do
33
+ send("attr_#{temp_type}", key)
34
+ end
35
+ end
36
+ end
37
+
38
+ def ostend_filterd_hash( hash )
39
+ return hash unless @ostend_attr_filter.respond_to?(:include?)
40
+ hash.keep_if{|key, val| @ostend_attr_filter.include?( key ) }
41
+ end
42
+
43
+ def ostend_raise_disallowed_attrs( hash )
44
+ disallowed_attrs = hash.delete_if{|key, val| @ostend_attr_filter.include?( key ) }
45
+ return if disallowed_attrs.empty?
46
+ raise "The following are not allowed attributes: #{disallowed_attrs}"
47
+ end
48
+
49
+ end
@@ -0,0 +1,3 @@
1
+ module Ostend
2
+ VERSION = "0.0.2.alpha.1"
3
+ end
data/lib/ostend.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'ostend/core.rb'
2
+ require 'ostend/version.rb'
data/ostend.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ostend/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "ostend"
8
+ s.version = Ostend::VERSION
9
+ s.authors = ["Jeff Gandt"]
10
+ s.email = ["jeff.gandt@gmail.com"]
11
+ s.description = "This is a set of tools to help instantiating objects with a hash"
12
+ s.summary = "Objectify From a Hash"
13
+ s.license = 'EPL'
14
+ s.homepage = 'https://github.com/jgandt/ostend'
15
+
16
+
17
+ s.files = `git ls-files`.split($/)
18
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,145 @@
1
+ require 'spec_helper'
2
+
3
+ class OstendTester
4
+ include Ostend
5
+ end
6
+
7
+ describe Ostend do
8
+
9
+ HASH = {:test_attr_1 => 'test attr 1', :test_attr_2 => 'test attr 2'}
10
+
11
+ let(:hash){ {:test_attr_1 => 'test attr 1', :test_attr_2 => 'test attr 2'} }
12
+ let(:ostend_tester){ OstendTester.new }
13
+
14
+ shared_examples 'instance variable setter' do |test_hash|
15
+ it 'assigns the value to the instance variable' do
16
+ test_hash.each do |key, value|
17
+ subject.instance_variable_get( "@#{key}" ).should == value
18
+ end
19
+ end
20
+ end
21
+
22
+ shared_examples 'writer creator' do |test_hash|
23
+ it 'creates writer methods' do
24
+ test_hash.each do |key, value|
25
+ subject.public_methods.should include "#{key}=".to_sym
26
+ end
27
+ end
28
+ end
29
+
30
+ shared_examples 'NON writer creator' do |test_hash|
31
+ it 'does not create writer methods' do
32
+ test_hash.each do |key, value|
33
+ subject.public_methods.should_not include "#{key}=".to_sym
34
+ end
35
+ end
36
+ end
37
+
38
+ shared_examples 'reader creator' do |test_hash|
39
+ it 'creates reader methods' do
40
+ test_hash.each do |key, value|
41
+ subject.public_methods.should include "#{key}".to_sym
42
+ end
43
+ end
44
+ end
45
+
46
+ shared_examples 'NON reader creator' do |test_hash|
47
+ it 'creates reader methods' do
48
+ test_hash.each do |key, value|
49
+ subject.public_methods.should_not include "#{key}".to_sym
50
+ end
51
+ end
52
+ end
53
+
54
+ describe '#ostendify' do
55
+ subject{ ostend_tester }
56
+
57
+ context 'when @ostend_attr_type is not set' do
58
+ it 'defaults @ostend_attr_type to :accessor' do
59
+ subject.instance_variable_get('@ostend_attr_type').should be_nil
60
+ subject.ostendify(hash)
61
+ subject.instance_variable_get('@ostend_attr_type').should be :accessor
62
+ end
63
+ context 'it creates attr_accessors' do
64
+ before{ subject.ostendify(hash) }
65
+ it_should_behave_like 'writer creator', HASH
66
+ it_should_behave_like 'reader creator', HASH
67
+ it_should_behave_like 'instance variable setter', HASH
68
+ end
69
+ end
70
+
71
+ context 'when @ostend_attr_type is :accessor it creates attr_accessors' do
72
+ before do
73
+ subject.ostend_attr_type = :accessor
74
+ subject.ostendify(hash)
75
+ end
76
+ it_should_behave_like 'writer creator', HASH
77
+ it_should_behave_like 'reader creator', HASH
78
+ it_should_behave_like 'instance variable setter', HASH
79
+ end
80
+
81
+ context 'when @ostend_attr_type is :writer it creates attr_accessors' do
82
+ before do
83
+ subject.ostend_attr_type = :writer
84
+ subject.ostendify(hash)
85
+ end
86
+ it_should_behave_like 'writer creator', HASH
87
+ # it_should_behave_like 'NON reader creator', HASH
88
+ it_should_behave_like 'instance variable setter', HASH
89
+ end
90
+
91
+ context 'when @ostend_attr_type is :reader it creates attr_accessors' do
92
+ before do
93
+ subject.ostend_attr_type = :writer
94
+ subject.ostendify(hash)
95
+ end
96
+ it_should_behave_like 'writer creator', HASH
97
+ it_should_behave_like 'NON reader creator', HASH
98
+ it_should_behave_like 'instance variable setter', HASH
99
+ end
100
+
101
+ end
102
+
103
+ describe '#ostend_create_attributes' do
104
+ subject{ ostend_tester }
105
+
106
+ context 'when @ostend_attr_type is :accessor' do
107
+ before do
108
+ subject.ostend_attr_type = :accessor
109
+ subject.send(:ostend_create_attributes, hash)
110
+ end
111
+ it_should_behave_like 'writer creator', HASH
112
+ it_should_behave_like 'reader creator', HASH
113
+ it_should_behave_like 'instance variable setter', HASH
114
+ end
115
+
116
+ context 'when @ostend_attr_type is :writer' do
117
+ before do
118
+ subject.ostend_attr_type = :writer
119
+ subject.send(:ostend_create_attributes, hash)
120
+ end
121
+ it_should_behave_like 'writer creator', HASH
122
+ it_should_behave_like 'NON reader creator', HASH
123
+ it_should_behave_like 'instance variable setter', HASH
124
+ end
125
+
126
+ context 'when @ostend_attr_type is :reader' do
127
+ before do
128
+ subject.ostend_attr_type = :reader
129
+ subject.send(:ostend_create_attributes, hash)
130
+ end
131
+ it_should_behave_like 'NON writer creator', HASH
132
+ it_should_behave_like 'reader creator', HASH
133
+ it_should_behave_like 'instance variable setter', HASH
134
+ end
135
+
136
+ it 'creates attr_* only for the singleton class, not the core class' do
137
+ subject{ OstendTester.new }
138
+ ostend_tester.ostend_attr_type = :accessor
139
+ ostend_tester.send('ostend_create_attributes', hash )
140
+
141
+ OstendTester.new.should_not respond_to( hash.keys.first )
142
+ end
143
+ end
144
+
145
+ end
@@ -0,0 +1,12 @@
1
+ SPEC_ROOT_DIR = File.dirname(__FILE__)
2
+
3
+ require 'simplecov'
4
+
5
+ SimpleCov.start
6
+
7
+ # require_relative File.expand_path(File.join(SPEC_ROOT_DIR, '../lib'))
8
+ Dir.glob( SPEC_ROOT_DIR + '../lib/**/*') do |load_file|
9
+ require_relative load_file
10
+ end
11
+ require 'ostend'
12
+
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ostend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2.alpha.1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Jeff Gandt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-03 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: This is a set of tools to help instantiating objects with a hash
15
+ email:
16
+ - jeff.gandt@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README
24
+ - Rakefile
25
+ - lib/ostend.rb
26
+ - lib/ostend/core.rb
27
+ - lib/ostend/version.rb
28
+ - ostend.gemspec
29
+ - spec/lib/ostend_spec.rb
30
+ - spec/spec_helper.rb
31
+ homepage: https://github.com/jgandt/ostend
32
+ licenses:
33
+ - EPL
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>'
48
+ - !ruby/object:Gem::Version
49
+ version: 1.3.1
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.10
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Objectify From a Hash
56
+ test_files:
57
+ - spec/lib/ostend_spec.rb
58
+ - spec/spec_helper.rb