constructable 0.1.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.
- data/.document +5 -0
 - data/.rvmrc +1 -0
 - data/.travis.yml +2 -0
 - data/Gemfile +13 -0
 - data/LICENSE.txt +20 -0
 - data/README.markdown +56 -0
 - data/Rakefile +40 -0
 - data/VERSION +1 -0
 - data/lib/constructable/attribute.rb +65 -0
 - data/lib/constructable/constructable.rb +40 -0
 - data/lib/constructable/constructor.rb +37 -0
 - data/lib/constructable/core_ext.rb +3 -0
 - data/lib/constructable/exceptions.rb +3 -0
 - data/lib/constructable.rb +7 -0
 - data/test/constructable/test_attribute.rb +97 -0
 - data/test/constructable/test_constructable.rb +68 -0
 - data/test/constructable/test_constructor.rb +39 -0
 - data/test/helper.rb +37 -0
 - metadata +124 -0
 
    
        data/.document
    ADDED
    
    
    
        data/.rvmrc
    ADDED
    
    | 
         @@ -0,0 +1 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            rvm ruby-1.9.2-p180@constructable
         
     | 
    
        data/.travis.yml
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    | 
         @@ -0,0 +1,13 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            source "http://rubygems.org"
         
     | 
| 
      
 2 
     | 
    
         
            +
            # Add dependencies required to use your gem here.
         
     | 
| 
      
 3 
     | 
    
         
            +
            # Example:
         
     | 
| 
      
 4 
     | 
    
         
            +
            #   gem "activesupport", ">= 2.3.5"
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            # Add dependencies to develop your gem here.
         
     | 
| 
      
 7 
     | 
    
         
            +
            # Include everything needed to run rake, tests, features, etc.
         
     | 
| 
      
 8 
     | 
    
         
            +
            group :development do
         
     | 
| 
      
 9 
     | 
    
         
            +
              gem "bundler", "~> 1.0.0"
         
     | 
| 
      
 10 
     | 
    
         
            +
              gem "jeweler", "~> 1.6.0"
         
     | 
| 
      
 11 
     | 
    
         
            +
              gem 'simplecov'
         
     | 
| 
      
 12 
     | 
    
         
            +
              gem 'yard'
         
     | 
| 
      
 13 
     | 
    
         
            +
            end
         
     | 
    
        data/LICENSE.txt
    ADDED
    
    | 
         @@ -0,0 +1,20 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            Copyright (c) 2011 Manuel Korfmann
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            Permission is hereby granted, free of charge, to any person obtaining
         
     | 
| 
      
 4 
     | 
    
         
            +
            a copy of this software and associated documentation files (the
         
     | 
| 
      
 5 
     | 
    
         
            +
            "Software"), to deal in the Software without restriction, including
         
     | 
| 
      
 6 
     | 
    
         
            +
            without limitation the rights to use, copy, modify, merge, publish,
         
     | 
| 
      
 7 
     | 
    
         
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         
     | 
| 
      
 8 
     | 
    
         
            +
            permit persons to whom the Software is furnished to do so, subject to
         
     | 
| 
      
 9 
     | 
    
         
            +
            the following conditions:
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            The above copyright notice and this permission notice shall be
         
     | 
| 
      
 12 
     | 
    
         
            +
            included in all copies or substantial portions of the Software.
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         
     | 
| 
      
 15 
     | 
    
         
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         
     | 
| 
      
 16 
     | 
    
         
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         
     | 
| 
      
 17 
     | 
    
         
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         
     | 
| 
      
 18 
     | 
    
         
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         
     | 
| 
      
 19 
     | 
    
         
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         
     | 
| 
      
 20 
     | 
    
         
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         
     | 
    
        data/README.markdown
    ADDED
    
    | 
         @@ -0,0 +1,56 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # Constructable [](http://travis-ci.org/mkorfmann/constructable)
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            Provides a powerful class macro for defining and configuring constructable attributes of a class.
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            ## Facts
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            * Doesn't break default initialize behaviour(see test/constructable/test_constructable 'should not break the initialize behaviour')
         
     | 
| 
      
 8 
     | 
    
         
            +
            * Only does what you expect it to do (no real magic involved)
         
     | 
| 
      
 9 
     | 
    
         
            +
            * Validates your attributes
         
     | 
| 
      
 10 
     | 
    
         
            +
            * Provides a granular control on how accessible your attributes are(no need to define attr_* yourself)
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            ## Usage
         
     | 
| 
      
 13 
     | 
    
         
            +
            <pre><code>
         
     | 
| 
      
 14 
     | 
    
         
            +
            class Foo
         
     | 
| 
      
 15 
     | 
    
         
            +
              constructable [:bar, :readable => true], [:baz, :required => true, :readable => true]
         
     | 
| 
      
 16 
     | 
    
         
            +
            end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
            foo = Foo.new(bar: 5)
         
     | 
| 
      
 19 
     | 
    
         
            +
            \# raises AttributeError, ':baz is a required attribute'
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
            foo = Foo.new(baz: 7, bar: 5)
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            foo.bar
         
     | 
| 
      
 24 
     | 
    
         
            +
            \# => 5
         
     | 
| 
      
 25 
     | 
    
         
            +
            foo.baz
         
     | 
| 
      
 26 
     | 
    
         
            +
            \# => 7
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
            class ProgrammingLanguage
         
     | 
| 
      
 29 
     | 
    
         
            +
              constructable [:paradigms,
         
     | 
| 
      
 30 
     | 
    
         
            +
                readable: true,
         
     | 
| 
      
 31 
     | 
    
         
            +
                required: true,
         
     | 
| 
      
 32 
     | 
    
         
            +
                validate_type: Array]
         
     | 
| 
      
 33 
     | 
    
         
            +
            end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
            c = ProgrammingLanguage.new(paradigms: :functional)
         
     | 
| 
      
 36 
     | 
    
         
            +
            \#  raises AttributeError, ':paradigms needs to be of type Array'
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
            ruby = ProgrammingLanguage.new(paradigms: [:object_oriented, :functional])
         
     | 
| 
      
 39 
     | 
    
         
            +
            ruby.paradigms
         
     | 
| 
      
 40 
     | 
    
         
            +
            \# => [:object_oriented, :functional]
         
     | 
| 
      
 41 
     | 
    
         
            +
            </pre></code>
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
            ## Contributing to constructable
         
     | 
| 
      
 45 
     | 
    
         
            +
            * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
         
     | 
| 
      
 46 
     | 
    
         
            +
            * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
         
     | 
| 
      
 47 
     | 
    
         
            +
            * Fork the project
         
     | 
| 
      
 48 
     | 
    
         
            +
            * Start a feature/bugfix branch
         
     | 
| 
      
 49 
     | 
    
         
            +
            * Commit and push until you are happy with your contribution
         
     | 
| 
      
 50 
     | 
    
         
            +
            * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
         
     | 
| 
      
 51 
     | 
    
         
            +
            * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
            ## Copyright
         
     | 
| 
      
 54 
     | 
    
         
            +
            Copyright (c) 2011 Manuel Korfmann. See LICENSE.txt for
         
     | 
| 
      
 55 
     | 
    
         
            +
            further details.
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
    
        data/Rakefile
    ADDED
    
    | 
         @@ -0,0 +1,40 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # encoding: utf-8
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'rubygems'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'bundler'
         
     | 
| 
      
 5 
     | 
    
         
            +
            begin
         
     | 
| 
      
 6 
     | 
    
         
            +
              Bundler.setup(:default, :development)
         
     | 
| 
      
 7 
     | 
    
         
            +
            rescue Bundler::BundlerError => e
         
     | 
| 
      
 8 
     | 
    
         
            +
              $stderr.puts e.message
         
     | 
| 
      
 9 
     | 
    
         
            +
              $stderr.puts "Run `bundle install` to install missing gems"
         
     | 
| 
      
 10 
     | 
    
         
            +
              exit e.status_code
         
     | 
| 
      
 11 
     | 
    
         
            +
            end
         
     | 
| 
      
 12 
     | 
    
         
            +
            require 'rake'
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
            require 'jeweler'
         
     | 
| 
      
 15 
     | 
    
         
            +
            Jeweler::Tasks.new do |gem|
         
     | 
| 
      
 16 
     | 
    
         
            +
              # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
         
     | 
| 
      
 17 
     | 
    
         
            +
              gem.name = "constructable"
         
     | 
| 
      
 18 
     | 
    
         
            +
              gem.homepage = "http://github.com/mkorfmann/constructable"
         
     | 
| 
      
 19 
     | 
    
         
            +
              gem.license = "MIT"
         
     | 
| 
      
 20 
     | 
    
         
            +
              gem.summary = %Q{Makes constructing objects through an attributes hash easier}
         
     | 
| 
      
 21 
     | 
    
         
            +
              gem.description = %Q{
         
     | 
| 
      
 22 
     | 
    
         
            +
            Adds the class macro Class#constructable to easily define what attributes a Class accepts provided as a hash to Class#new.
         
     | 
| 
      
 23 
     | 
    
         
            +
            Attributes can be configured in their behaviour in case they are not provided or are in the wrong format.
         
     | 
| 
      
 24 
     | 
    
         
            +
            Their default value can also be defined and you have granular control on how accessible your attribute is.
         
     | 
| 
      
 25 
     | 
    
         
            +
            See the documentation for Constructable::Constructable#constructable or the README for more information.
         
     | 
| 
      
 26 
     | 
    
         
            +
              }
         
     | 
| 
      
 27 
     | 
    
         
            +
              gem.email = "manu@korfmann.info"
         
     | 
| 
      
 28 
     | 
    
         
            +
              gem.authors = ["Manuel Korfmann"]
         
     | 
| 
      
 29 
     | 
    
         
            +
              # dependencies defined in Gemfile
         
     | 
| 
      
 30 
     | 
    
         
            +
            end
         
     | 
| 
      
 31 
     | 
    
         
            +
            Jeweler::RubygemsDotOrgTasks.new
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
            require 'rake/testtask'
         
     | 
| 
      
 34 
     | 
    
         
            +
            Rake::TestTask.new(:test) do |test|
         
     | 
| 
      
 35 
     | 
    
         
            +
              test.libs << 'lib' << 'test'
         
     | 
| 
      
 36 
     | 
    
         
            +
              test.pattern = 'test/**/test_*.rb'
         
     | 
| 
      
 37 
     | 
    
         
            +
              test.verbose = true
         
     | 
| 
      
 38 
     | 
    
         
            +
            end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
            task :default => :test
         
     | 
    
        data/VERSION
    ADDED
    
    | 
         @@ -0,0 +1 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            0.1.0
         
     | 
| 
         @@ -0,0 +1,65 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Constructable
         
     | 
| 
      
 2 
     | 
    
         
            +
              class Attribute
         
     | 
| 
      
 3 
     | 
    
         
            +
                ATTRIBUTES = [:writable, :readable, :accessible, :required, :validate, :default, :validate_type, :converter]
         
     | 
| 
      
 4 
     | 
    
         
            +
                attr_accessor *ATTRIBUTES, :name
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                REQUIRED_REQUIREMENT = {
         
     | 
| 
      
 7 
     | 
    
         
            +
                  name: :required,
         
     | 
| 
      
 8 
     | 
    
         
            +
                  message: proc {":#{self.name} is a required attribute"},
         
     | 
| 
      
 9 
     | 
    
         
            +
                  check: ->(hash) { hash.has_key?(self.name) }
         
     | 
| 
      
 10 
     | 
    
         
            +
                }
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                REQUIREMENTS = [
         
     | 
| 
      
 13 
     | 
    
         
            +
                  {
         
     | 
| 
      
 14 
     | 
    
         
            +
                    name: :validate,
         
     | 
| 
      
 15 
     | 
    
         
            +
                    message: proc {":#{self.name} did not pass validation"},
         
     | 
| 
      
 16 
     | 
    
         
            +
                    check: ->(hash) { self.validate.call(hash[self.name])}
         
     | 
| 
      
 17 
     | 
    
         
            +
                  },
         
     | 
| 
      
 18 
     | 
    
         
            +
                  {
         
     | 
| 
      
 19 
     | 
    
         
            +
                    name: :validate_type,
         
     | 
| 
      
 20 
     | 
    
         
            +
                    message: proc {":#{self.name} needs to be of type #{self.validate_type}"},
         
     | 
| 
      
 21 
     | 
    
         
            +
                    check: ->(hash) { hash[self.name].is_a? self.validate_type }
         
     | 
| 
      
 22 
     | 
    
         
            +
                  }
         
     | 
| 
      
 23 
     | 
    
         
            +
                ]
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                def initialize(name, options = {})
         
     | 
| 
      
 26 
     | 
    
         
            +
                  @name = name
         
     | 
| 
      
 27 
     | 
    
         
            +
                  options.each do |option, value|
         
     | 
| 
      
 28 
     | 
    
         
            +
                    self.send(:"#{option}=", value )
         
     | 
| 
      
 29 
     | 
    
         
            +
                  end
         
     | 
| 
      
 30 
     | 
    
         
            +
                end
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                def accessible=(boolean)
         
     | 
| 
      
 33 
     | 
    
         
            +
                  if boolean
         
     | 
| 
      
 34 
     | 
    
         
            +
                    self.readable = true
         
     | 
| 
      
 35 
     | 
    
         
            +
                    self.writable = true
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                def ivar_symbol
         
     | 
| 
      
 40 
     | 
    
         
            +
                  ('@' + self.name.to_s).to_sym
         
     | 
| 
      
 41 
     | 
    
         
            +
                end
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                def check_for_requirement(requirement, constructor_hash)
         
     | 
| 
      
 44 
     | 
    
         
            +
                  if self.send requirement[:name]
         
     | 
| 
      
 45 
     | 
    
         
            +
                    unless self.instance_exec(constructor_hash,&requirement[:check])
         
     | 
| 
      
 46 
     | 
    
         
            +
                      raise AttributeError, instance_eval(&requirement[:message])
         
     | 
| 
      
 47 
     | 
    
         
            +
                    end
         
     | 
| 
      
 48 
     | 
    
         
            +
                  end
         
     | 
| 
      
 49 
     | 
    
         
            +
                end
         
     | 
| 
      
 50 
     | 
    
         
            +
                private :check_for_requirement
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                def process(constructor_hash)
         
     | 
| 
      
 53 
     | 
    
         
            +
                  if constructor_hash.has_key?(self.name)
         
     | 
| 
      
 54 
     | 
    
         
            +
                    REQUIREMENTS.each do |requirement|
         
     | 
| 
      
 55 
     | 
    
         
            +
                      check_for_requirement(requirement, constructor_hash)
         
     | 
| 
      
 56 
     | 
    
         
            +
                    end
         
     | 
| 
      
 57 
     | 
    
         
            +
                    value = constructor_hash[self.name]
         
     | 
| 
      
 58 
     | 
    
         
            +
                    self.converter ? converter.(value) : value
         
     | 
| 
      
 59 
     | 
    
         
            +
                  else
         
     | 
| 
      
 60 
     | 
    
         
            +
                    check_for_requirement(REQUIRED_REQUIREMENT, constructor_hash)
         
     | 
| 
      
 61 
     | 
    
         
            +
                    self.default
         
     | 
| 
      
 62 
     | 
    
         
            +
                  end
         
     | 
| 
      
 63 
     | 
    
         
            +
                end
         
     | 
| 
      
 64 
     | 
    
         
            +
              end
         
     | 
| 
      
 65 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,40 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Constructable
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Constructable
         
     | 
| 
      
 3 
     | 
    
         
            +
                # @example
         
     | 
| 
      
 4 
     | 
    
         
            +
                #
         
     | 
| 
      
 5 
     | 
    
         
            +
                #  class Foo
         
     | 
| 
      
 6 
     | 
    
         
            +
                #    constructable [:bar, :readable => true], [:baz, :required => true, :readable => true]
         
     | 
| 
      
 7 
     | 
    
         
            +
                #  end
         
     | 
| 
      
 8 
     | 
    
         
            +
                #
         
     | 
| 
      
 9 
     | 
    
         
            +
                #  foo = Foo.new(bar: 5)
         
     | 
| 
      
 10 
     | 
    
         
            +
                #  # raises AttributeError, ':baz is a required attribute'
         
     | 
| 
      
 11 
     | 
    
         
            +
                #
         
     | 
| 
      
 12 
     | 
    
         
            +
                #  foo = Foo.new(baz: 7, bar: 5)
         
     | 
| 
      
 13 
     | 
    
         
            +
                #
         
     | 
| 
      
 14 
     | 
    
         
            +
                #  foo.bar
         
     | 
| 
      
 15 
     | 
    
         
            +
                #  # => 5
         
     | 
| 
      
 16 
     | 
    
         
            +
                #  foo.baz
         
     | 
| 
      
 17 
     | 
    
         
            +
                #  # => 7
         
     | 
| 
      
 18 
     | 
    
         
            +
                #
         
     | 
| 
      
 19 
     | 
    
         
            +
                #  class ProgrammingLanguage
         
     | 
| 
      
 20 
     | 
    
         
            +
                #    constructable [:paradigms,
         
     | 
| 
      
 21 
     | 
    
         
            +
                #      readable: true,
         
     | 
| 
      
 22 
     | 
    
         
            +
                #      required: true,
         
     | 
| 
      
 23 
     | 
    
         
            +
                #      validate: ->(value) { value.is_a?(Array) }]
         
     | 
| 
      
 24 
     | 
    
         
            +
                #  end
         
     | 
| 
      
 25 
     | 
    
         
            +
                #
         
     | 
| 
      
 26 
     | 
    
         
            +
                #  c = ProgrammingLanguage.new(paradigms: :functional)
         
     | 
| 
      
 27 
     | 
    
         
            +
                #  #  raises AttributeError, ':paradigms did not pass validation'
         
     | 
| 
      
 28 
     | 
    
         
            +
                #
         
     | 
| 
      
 29 
     | 
    
         
            +
                #  ruby = ProgrammingLanguage.new(paradigms: [:object_oriented, :functional])
         
     | 
| 
      
 30 
     | 
    
         
            +
                #  ruby.paradigms
         
     | 
| 
      
 31 
     | 
    
         
            +
                #  # => [:object_oriented, :functional]
         
     | 
| 
      
 32 
     | 
    
         
            +
                #
         
     | 
| 
      
 33 
     | 
    
         
            +
                # @param [Array<[Array<Symbol, Hash>]>] args an array of symbols or arrays: the name of the attribute and it's configuration
         
     | 
| 
      
 34 
     | 
    
         
            +
                def constructable(*args)
         
     | 
| 
      
 35 
     | 
    
         
            +
                  @__constructor ||= Constructor.new(self)
         
     | 
| 
      
 36 
     | 
    
         
            +
                  @__constructor.define_attributes(args)
         
     | 
| 
      
 37 
     | 
    
         
            +
                  return nil
         
     | 
| 
      
 38 
     | 
    
         
            +
                end
         
     | 
| 
      
 39 
     | 
    
         
            +
              end
         
     | 
| 
      
 40 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,37 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Constructable
         
     | 
| 
      
 2 
     | 
    
         
            +
              class Constructor
         
     | 
| 
      
 3 
     | 
    
         
            +
                def initialize(klass)
         
     | 
| 
      
 4 
     | 
    
         
            +
                  @attributes = []
         
     | 
| 
      
 5 
     | 
    
         
            +
                  @klass = klass
         
     | 
| 
      
 6 
     | 
    
         
            +
                  constructor = self
         
     | 
| 
      
 7 
     | 
    
         
            +
                  @klass.define_singleton_method(:new) do |*args, &block|
         
     | 
| 
      
 8 
     | 
    
         
            +
                    obj = self.allocate
         
     | 
| 
      
 9 
     | 
    
         
            +
                    constructor_hash = Hash === args.last ? args.pop : {}
         
     | 
| 
      
 10 
     | 
    
         
            +
                    constructor.construct(constructor_hash, obj)
         
     | 
| 
      
 11 
     | 
    
         
            +
                    obj.send :initialize, *args, &block
         
     | 
| 
      
 12 
     | 
    
         
            +
                    obj
         
     | 
| 
      
 13 
     | 
    
         
            +
                  end
         
     | 
| 
      
 14 
     | 
    
         
            +
                end
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                def define_attributes(attributes)
         
     | 
| 
      
 18 
     | 
    
         
            +
                  @attributes.concat attributes.map! { |c| Attribute.new(*c) }
         
     | 
| 
      
 19 
     | 
    
         
            +
                  attributes.each do |attribute|
         
     | 
| 
      
 20 
     | 
    
         
            +
                    @klass.class_eval do
         
     | 
| 
      
 21 
     | 
    
         
            +
                      setter = :"#{attribute.name}="
         
     | 
| 
      
 22 
     | 
    
         
            +
                      define_method(attribute.name) { instance_variable_get attribute.ivar_symbol }
         
     | 
| 
      
 23 
     | 
    
         
            +
                      private attribute.name unless attribute.readable
         
     | 
| 
      
 24 
     | 
    
         
            +
                      define_method(setter) { |value| instance_variable_set attribute.ivar_symbol, attribute.process({ attribute.name => value}) }
         
     | 
| 
      
 25 
     | 
    
         
            +
                      private setter unless attribute.writable
         
     | 
| 
      
 26 
     | 
    
         
            +
                    end
         
     | 
| 
      
 27 
     | 
    
         
            +
                  end
         
     | 
| 
      
 28 
     | 
    
         
            +
                end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
                def construct(constructor_hash, obj)
         
     | 
| 
      
 31 
     | 
    
         
            +
                  constructor_hash ||= {}
         
     | 
| 
      
 32 
     | 
    
         
            +
                  @attributes.each do |attributes|
         
     | 
| 
      
 33 
     | 
    
         
            +
                    obj.instance_variable_set(attributes.ivar_symbol, attributes.process(constructor_hash))
         
     | 
| 
      
 34 
     | 
    
         
            +
                  end
         
     | 
| 
      
 35 
     | 
    
         
            +
                end
         
     | 
| 
      
 36 
     | 
    
         
            +
              end
         
     | 
| 
      
 37 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,97 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
            include Constructable
         
     | 
| 
      
 3 
     | 
    
         
            +
            describe 'Attribute' do
         
     | 
| 
      
 4 
     | 
    
         
            +
              describe 'name' do
         
     | 
| 
      
 5 
     | 
    
         
            +
                it 'returns the name' do
         
     | 
| 
      
 6 
     | 
    
         
            +
                  attribute = Attribute.new(:attribute)
         
     | 
| 
      
 7 
     | 
    
         
            +
                  assert_equal :attribute, attribute.name
         
     | 
| 
      
 8 
     | 
    
         
            +
                end
         
     | 
| 
      
 9 
     | 
    
         
            +
              end
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
              describe 'ivar_symbol' do
         
     | 
| 
      
 12 
     | 
    
         
            +
                it 'should return @<name>' do
         
     | 
| 
      
 13 
     | 
    
         
            +
                  attribute = Attribute.new(:foo)
         
     | 
| 
      
 14 
     | 
    
         
            +
                  assert_equal :@foo, attribute.ivar_symbol
         
     | 
| 
      
 15 
     | 
    
         
            +
                end
         
     | 
| 
      
 16 
     | 
    
         
            +
              end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
              describe 'process' do
         
     | 
| 
      
 19 
     | 
    
         
            +
                it 'should raise nothing if no attributes are specified' do
         
     | 
| 
      
 20 
     | 
    
         
            +
                  attribute = Attribute.new(:foo)
         
     | 
| 
      
 21 
     | 
    
         
            +
                  assert_equal 'bar', attribute.process({foo: 'bar'})
         
     | 
| 
      
 22 
     | 
    
         
            +
                end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                describe 'required attribute' do
         
     | 
| 
      
 25 
     | 
    
         
            +
                  it 'should raise an AttributeError if required is set to true' do
         
     | 
| 
      
 26 
     | 
    
         
            +
                    attribute = Attribute.new(:foo, required: true)
         
     | 
| 
      
 27 
     | 
    
         
            +
                    begin
         
     | 
| 
      
 28 
     | 
    
         
            +
                      attribute.process(bar: 'blab')
         
     | 
| 
      
 29 
     | 
    
         
            +
                    rescue Exception => e
         
     | 
| 
      
 30 
     | 
    
         
            +
                      assert AttributeError === e
         
     | 
| 
      
 31 
     | 
    
         
            +
                      assert_equal ':foo is a required attribute', e.message
         
     | 
| 
      
 32 
     | 
    
         
            +
                    else
         
     | 
| 
      
 33 
     | 
    
         
            +
                      assert false, 'AttributeError was not raised'
         
     | 
| 
      
 34 
     | 
    
         
            +
                    end
         
     | 
| 
      
 35 
     | 
    
         
            +
                  end
         
     | 
| 
      
 36 
     | 
    
         
            +
                end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                describe 'attribute is neither required nor provided' do
         
     | 
| 
      
 39 
     | 
    
         
            +
                  it 'does not check for further requirements' do
         
     | 
| 
      
 40 
     | 
    
         
            +
                    attribute = Attribute.new(:foo, validate_type: Integer)
         
     | 
| 
      
 41 
     | 
    
         
            +
                    refute_raises do
         
     | 
| 
      
 42 
     | 
    
         
            +
                      attribute.process({})
         
     | 
| 
      
 43 
     | 
    
         
            +
                    end
         
     | 
| 
      
 44 
     | 
    
         
            +
                  end
         
     | 
| 
      
 45 
     | 
    
         
            +
                end
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                describe 'validator' do
         
     | 
| 
      
 48 
     | 
    
         
            +
                  it 'should raise an AttributeError if the validator doesn\'t pass' do
         
     | 
| 
      
 49 
     | 
    
         
            +
                    attribute = Attribute.new(:foo, validate: ->(number) { number < 5 })
         
     | 
| 
      
 50 
     | 
    
         
            +
                    begin
         
     | 
| 
      
 51 
     | 
    
         
            +
                      attribute.process(foo: 6)
         
     | 
| 
      
 52 
     | 
    
         
            +
                    rescue Exception => e
         
     | 
| 
      
 53 
     | 
    
         
            +
                      assert AttributeError === e, "[#{e.class},#{e.message}] was not expected"
         
     | 
| 
      
 54 
     | 
    
         
            +
                      assert_equal ':foo did not pass validation', e.message
         
     | 
| 
      
 55 
     | 
    
         
            +
                    else
         
     | 
| 
      
 56 
     | 
    
         
            +
                      assert false, 'AttributeError was not raised'
         
     | 
| 
      
 57 
     | 
    
         
            +
                    end
         
     | 
| 
      
 58 
     | 
    
         
            +
                  end
         
     | 
| 
      
 59 
     | 
    
         
            +
                end
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                describe 'validate_type check' do
         
     | 
| 
      
 62 
     | 
    
         
            +
                  it 'should raise an AttributeError if the value has not the wanted validate_type' do
         
     | 
| 
      
 63 
     | 
    
         
            +
                    attribute = Attribute.new(:foo, validate_type: Integer)
         
     | 
| 
      
 64 
     | 
    
         
            +
                    begin
         
     | 
| 
      
 65 
     | 
    
         
            +
                      attribute.process(foo: 'notanumber')
         
     | 
| 
      
 66 
     | 
    
         
            +
                    rescue Exception => e
         
     | 
| 
      
 67 
     | 
    
         
            +
                      assert AttributeError === e, "[#{e.class},#{e.message}] was not expected"
         
     | 
| 
      
 68 
     | 
    
         
            +
                      assert_equal ':foo needs to be of type Integer', e.message
         
     | 
| 
      
 69 
     | 
    
         
            +
                    else
         
     | 
| 
      
 70 
     | 
    
         
            +
                      assert false, 'AttributeError was not raised'
         
     | 
| 
      
 71 
     | 
    
         
            +
                    end
         
     | 
| 
      
 72 
     | 
    
         
            +
                  end
         
     | 
| 
      
 73 
     | 
    
         
            +
                end
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
                describe 'default value' do
         
     | 
| 
      
 76 
     | 
    
         
            +
                  it 'should be possible to provide a default value' do
         
     | 
| 
      
 77 
     | 
    
         
            +
                    attribute = Attribute.new(:foo, default: :bar)
         
     | 
| 
      
 78 
     | 
    
         
            +
                    assert_equal :bar, attribute.process({})
         
     | 
| 
      
 79 
     | 
    
         
            +
                  end
         
     | 
| 
      
 80 
     | 
    
         
            +
                end
         
     | 
| 
      
 81 
     | 
    
         
            +
             
     | 
| 
      
 82 
     | 
    
         
            +
                describe 'convert value' do
         
     | 
| 
      
 83 
     | 
    
         
            +
                  it 'is possible to define a converter(proc) which converts attribute values' do
         
     | 
| 
      
 84 
     | 
    
         
            +
                    attribute = Attribute.new(:number, converter: ->(value) { value.to_i })
         
     | 
| 
      
 85 
     | 
    
         
            +
                    assert_equal 5, attribute.process({number: '5'})
         
     | 
| 
      
 86 
     | 
    
         
            +
                  end
         
     | 
| 
      
 87 
     | 
    
         
            +
                end
         
     | 
| 
      
 88 
     | 
    
         
            +
              end
         
     | 
| 
      
 89 
     | 
    
         
            +
             
     | 
| 
      
 90 
     | 
    
         
            +
              describe 'permission' do
         
     | 
| 
      
 91 
     | 
    
         
            +
                it 'should detect accessible attributes' do
         
     | 
| 
      
 92 
     | 
    
         
            +
                  attribute = Attribute.new( :readable_and_writable, accessible: true)
         
     | 
| 
      
 93 
     | 
    
         
            +
                  assert_equal true, attribute.readable
         
     | 
| 
      
 94 
     | 
    
         
            +
                  assert_equal true, attribute.writable
         
     | 
| 
      
 95 
     | 
    
         
            +
                end
         
     | 
| 
      
 96 
     | 
    
         
            +
              end
         
     | 
| 
      
 97 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,68 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
            describe 'integration' do
         
     | 
| 
      
 3 
     | 
    
         
            +
              describe 'Class' do
         
     | 
| 
      
 4 
     | 
    
         
            +
                describe 'constructable' do
         
     | 
| 
      
 5 
     | 
    
         
            +
                  it 'should define attr_accessors' do
         
     | 
| 
      
 6 
     | 
    
         
            +
                    klass = Class.new
         
     | 
| 
      
 7 
     | 
    
         
            +
                    klass.constructable([:foo, accessible: true])
         
     | 
| 
      
 8 
     | 
    
         
            +
                    assert_respond_to klass.new, :foo
         
     | 
| 
      
 9 
     | 
    
         
            +
                    assert_respond_to klass.new, :foo=
         
     | 
| 
      
 10 
     | 
    
         
            +
                  end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                  it 'should assign values found in the constructer hash' do
         
     | 
| 
      
 13 
     | 
    
         
            +
                    klass = Class.new
         
     | 
| 
      
 14 
     | 
    
         
            +
                    klass.constructable([:foo, readable: true])
         
     | 
| 
      
 15 
     | 
    
         
            +
                    instance = klass.new(foo: :bar)
         
     | 
| 
      
 16 
     | 
    
         
            +
                    assert_equal :bar, instance.foo
         
     | 
| 
      
 17 
     | 
    
         
            +
                  end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                  it 'should work with inheritance' do
         
     | 
| 
      
 20 
     | 
    
         
            +
                    klass = Class.new
         
     | 
| 
      
 21 
     | 
    
         
            +
                    klass.constructable([:bar, readable: true])
         
     | 
| 
      
 22 
     | 
    
         
            +
                    inherited_klass = Class.new(klass)
         
     | 
| 
      
 23 
     | 
    
         
            +
                    instance = inherited_klass.new(bar: 1)
         
     | 
| 
      
 24 
     | 
    
         
            +
                    assert_equal 1, instance.bar
         
     | 
| 
      
 25 
     | 
    
         
            +
                  end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                  it 'should be possible to make attributes required' do
         
     | 
| 
      
 28 
     | 
    
         
            +
                    klass = Class.new
         
     | 
| 
      
 29 
     | 
    
         
            +
                    klass.constructable([:bar, required: true])
         
     | 
| 
      
 30 
     | 
    
         
            +
                    assert_raises AttributeError do
         
     | 
| 
      
 31 
     | 
    
         
            +
                      klass.new
         
     | 
| 
      
 32 
     | 
    
         
            +
                    end
         
     | 
| 
      
 33 
     | 
    
         
            +
                  end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                  describe 'should not break the initalize behaviour' do
         
     | 
| 
      
 36 
     | 
    
         
            +
                    it 'works for methods with arguments + options provided' do
         
     | 
| 
      
 37 
     | 
    
         
            +
                      klass = Class.new
         
     | 
| 
      
 38 
     | 
    
         
            +
                      klass.constructable [:bar, accessible: true]
         
     | 
| 
      
 39 
     | 
    
         
            +
                      klass.class_eval do
         
     | 
| 
      
 40 
     | 
    
         
            +
                        def initialize
         
     | 
| 
      
 41 
     | 
    
         
            +
                          self.bar = 20
         
     | 
| 
      
 42 
     | 
    
         
            +
                        end
         
     | 
| 
      
 43 
     | 
    
         
            +
                      end
         
     | 
| 
      
 44 
     | 
    
         
            +
                      instance = klass.new(bar: 5)
         
     | 
| 
      
 45 
     | 
    
         
            +
                      assert_equal 20, instance.bar
         
     | 
| 
      
 46 
     | 
    
         
            +
                    end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                    it 'works for methods with only arguments provided' do
         
     | 
| 
      
 49 
     | 
    
         
            +
                      klass = Class.new
         
     | 
| 
      
 50 
     | 
    
         
            +
                      klass.constructable [:bar, accessible: true]
         
     | 
| 
      
 51 
     | 
    
         
            +
                      klass.class_eval do
         
     | 
| 
      
 52 
     | 
    
         
            +
                        def initialize(bar)
         
     | 
| 
      
 53 
     | 
    
         
            +
                          self.bar = bar
         
     | 
| 
      
 54 
     | 
    
         
            +
                        end
         
     | 
| 
      
 55 
     | 
    
         
            +
                      end
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                      instance = klass.new(1)
         
     | 
| 
      
 58 
     | 
    
         
            +
                      assert_equal 1, instance.bar
         
     | 
| 
      
 59 
     | 
    
         
            +
                    end
         
     | 
| 
      
 60 
     | 
    
         
            +
                  end
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                  it 'should return nil' do
         
     | 
| 
      
 63 
     | 
    
         
            +
                    klass = Class.new
         
     | 
| 
      
 64 
     | 
    
         
            +
                    assert_equal nil, klass.constructable(:bar)
         
     | 
| 
      
 65 
     | 
    
         
            +
                  end
         
     | 
| 
      
 66 
     | 
    
         
            +
                end
         
     | 
| 
      
 67 
     | 
    
         
            +
              end
         
     | 
| 
      
 68 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,39 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
            describe 'Constructor' do
         
     | 
| 
      
 3 
     | 
    
         
            +
              before do
         
     | 
| 
      
 4 
     | 
    
         
            +
                @klass = Class.new
         
     | 
| 
      
 5 
     | 
    
         
            +
              end
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
              describe 'define_attributes' do
         
     | 
| 
      
 8 
     | 
    
         
            +
                it 'defines public setters validating like in the constructor' do
         
     | 
| 
      
 9 
     | 
    
         
            +
                  @klass.constructable [:integer, validate_type: Integer, writable: true]
         
     | 
| 
      
 10 
     | 
    
         
            +
                  instance = @klass.new
         
     | 
| 
      
 11 
     | 
    
         
            +
                  assert_raises AttributeError do
         
     | 
| 
      
 12 
     | 
    
         
            +
                    instance.integer = 6.6
         
     | 
| 
      
 13 
     | 
    
         
            +
                  end
         
     | 
| 
      
 14 
     | 
    
         
            +
                end
         
     | 
| 
      
 15 
     | 
    
         
            +
              end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
              describe 'permission' do
         
     | 
| 
      
 18 
     | 
    
         
            +
                it 'should allow writable attributes' do
         
     | 
| 
      
 19 
     | 
    
         
            +
                  @klass.constructable [:writable_attribute, writable: true]
         
     | 
| 
      
 20 
     | 
    
         
            +
                  instance = @klass.new
         
     | 
| 
      
 21 
     | 
    
         
            +
                  instance.writable_attribute = "hello"
         
     | 
| 
      
 22 
     | 
    
         
            +
                  assert_equal "hello", instance.instance_variable_get(:@writable_attribute)
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                it 'should allow readable attributes' do
         
     | 
| 
      
 26 
     | 
    
         
            +
                  @klass.constructable [:readable_attribute, readable: true]
         
     | 
| 
      
 27 
     | 
    
         
            +
                  instance = @klass.new
         
     | 
| 
      
 28 
     | 
    
         
            +
                  instance.instance_variable_set(:@readable_attribute, "hello")
         
     | 
| 
      
 29 
     | 
    
         
            +
                  assert_equal "hello", instance.readable_attribute
         
     | 
| 
      
 30 
     | 
    
         
            +
                end
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                it 'should allow accessible attributes' do
         
     | 
| 
      
 33 
     | 
    
         
            +
                  @klass.constructable [:accessible_attribute, accessible: true]
         
     | 
| 
      
 34 
     | 
    
         
            +
                  instance = @klass.new
         
     | 
| 
      
 35 
     | 
    
         
            +
                  instance.accessible_attribute = 'hello'
         
     | 
| 
      
 36 
     | 
    
         
            +
                  assert_equal 'hello', instance.accessible_attribute
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
              end
         
     | 
| 
      
 39 
     | 
    
         
            +
            end
         
     | 
    
        data/test/helper.rb
    ADDED
    
    | 
         @@ -0,0 +1,37 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'rubygems'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'bundler'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            begin
         
     | 
| 
      
 5 
     | 
    
         
            +
              Bundler.setup(:default, :development)
         
     | 
| 
      
 6 
     | 
    
         
            +
            rescue Bundler::BundlerError => e
         
     | 
| 
      
 7 
     | 
    
         
            +
              $stderr.puts e.message
         
     | 
| 
      
 8 
     | 
    
         
            +
              $stderr.puts "Run `bundle install` to install missing gems"
         
     | 
| 
      
 9 
     | 
    
         
            +
              exit e.status_code
         
     | 
| 
      
 10 
     | 
    
         
            +
            end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            require 'simplecov'
         
     | 
| 
      
 13 
     | 
    
         
            +
            SimpleCov.start do
         
     | 
| 
      
 14 
     | 
    
         
            +
              add_filter 'test'
         
     | 
| 
      
 15 
     | 
    
         
            +
            end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
            require 'minitest/autorun'
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
            $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
         
     | 
| 
      
 20 
     | 
    
         
            +
            $LOAD_PATH.unshift(File.dirname(__FILE__))
         
     | 
| 
      
 21 
     | 
    
         
            +
            require 'constructable'
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            class MiniTest::Unit::TestCase
         
     | 
| 
      
 24 
     | 
    
         
            +
            	def refute_raises
         
     | 
| 
      
 25 
     | 
    
         
            +
            		test = -> do
         
     | 
| 
      
 26 
     | 
    
         
            +
            			begin	
         
     | 
| 
      
 27 
     | 
    
         
            +
            				yield
         
     | 
| 
      
 28 
     | 
    
         
            +
            			rescue => e
         
     | 
| 
      
 29 
     | 
    
         
            +
            				return false, e
         
     | 
| 
      
 30 
     | 
    
         
            +
            			else
         
     | 
| 
      
 31 
     | 
    
         
            +
            				true
         
     | 
| 
      
 32 
     | 
    
         
            +
            			end
         
     | 
| 
      
 33 
     | 
    
         
            +
            		end
         
     | 
| 
      
 34 
     | 
    
         
            +
            		boolean, exception = test.call
         
     | 
| 
      
 35 
     | 
    
         
            +
            		assert boolean, "Expected no exception, but got #{exception}"
         
     | 
| 
      
 36 
     | 
    
         
            +
            	end
         
     | 
| 
      
 37 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,124 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification 
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: constructable
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version 
         
     | 
| 
      
 4 
     | 
    
         
            +
              prerelease: 
         
     | 
| 
      
 5 
     | 
    
         
            +
              version: 0.1.0
         
     | 
| 
      
 6 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 7 
     | 
    
         
            +
            authors: 
         
     | 
| 
      
 8 
     | 
    
         
            +
            - Manuel Korfmann
         
     | 
| 
      
 9 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 10 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 11 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            date: 2011-06-08 00:00:00 +02:00
         
     | 
| 
      
 14 
     | 
    
         
            +
            default_executable: 
         
     | 
| 
      
 15 
     | 
    
         
            +
            dependencies: 
         
     | 
| 
      
 16 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency 
         
     | 
| 
      
 17 
     | 
    
         
            +
              name: bundler
         
     | 
| 
      
 18 
     | 
    
         
            +
              requirement: &id001 !ruby/object:Gem::Requirement 
         
     | 
| 
      
 19 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 20 
     | 
    
         
            +
                requirements: 
         
     | 
| 
      
 21 
     | 
    
         
            +
                - - ~>
         
     | 
| 
      
 22 
     | 
    
         
            +
                  - !ruby/object:Gem::Version 
         
     | 
| 
      
 23 
     | 
    
         
            +
                    version: 1.0.0
         
     | 
| 
      
 24 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 25 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 26 
     | 
    
         
            +
              version_requirements: *id001
         
     | 
| 
      
 27 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency 
         
     | 
| 
      
 28 
     | 
    
         
            +
              name: jeweler
         
     | 
| 
      
 29 
     | 
    
         
            +
              requirement: &id002 !ruby/object:Gem::Requirement 
         
     | 
| 
      
 30 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 31 
     | 
    
         
            +
                requirements: 
         
     | 
| 
      
 32 
     | 
    
         
            +
                - - ~>
         
     | 
| 
      
 33 
     | 
    
         
            +
                  - !ruby/object:Gem::Version 
         
     | 
| 
      
 34 
     | 
    
         
            +
                    version: 1.6.0
         
     | 
| 
      
 35 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 36 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 37 
     | 
    
         
            +
              version_requirements: *id002
         
     | 
| 
      
 38 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency 
         
     | 
| 
      
 39 
     | 
    
         
            +
              name: simplecov
         
     | 
| 
      
 40 
     | 
    
         
            +
              requirement: &id003 !ruby/object:Gem::Requirement 
         
     | 
| 
      
 41 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 42 
     | 
    
         
            +
                requirements: 
         
     | 
| 
      
 43 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 44 
     | 
    
         
            +
                  - !ruby/object:Gem::Version 
         
     | 
| 
      
 45 
     | 
    
         
            +
                    version: "0"
         
     | 
| 
      
 46 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 47 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 48 
     | 
    
         
            +
              version_requirements: *id003
         
     | 
| 
      
 49 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency 
         
     | 
| 
      
 50 
     | 
    
         
            +
              name: yard
         
     | 
| 
      
 51 
     | 
    
         
            +
              requirement: &id004 !ruby/object:Gem::Requirement 
         
     | 
| 
      
 52 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 53 
     | 
    
         
            +
                requirements: 
         
     | 
| 
      
 54 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 55 
     | 
    
         
            +
                  - !ruby/object:Gem::Version 
         
     | 
| 
      
 56 
     | 
    
         
            +
                    version: "0"
         
     | 
| 
      
 57 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 58 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 59 
     | 
    
         
            +
              version_requirements: *id004
         
     | 
| 
      
 60 
     | 
    
         
            +
            description: "\n\
         
     | 
| 
      
 61 
     | 
    
         
            +
              Adds the class macro Class#constructable to easily define what attributes a Class accepts provided as a hash to Class#new.\n\
         
     | 
| 
      
 62 
     | 
    
         
            +
              Attributes can be configured in their behaviour in case they are not provided or are in the wrong format.\n\
         
     | 
| 
      
 63 
     | 
    
         
            +
              Their default value can also be defined and you have granular control on how accessible your attribute is.\n\
         
     | 
| 
      
 64 
     | 
    
         
            +
              See the documentation for Constructable::Constructable#constructable or the README for more information.\n  "
         
     | 
| 
      
 65 
     | 
    
         
            +
            email: manu@korfmann.info
         
     | 
| 
      
 66 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
            extra_rdoc_files: 
         
     | 
| 
      
 71 
     | 
    
         
            +
            - LICENSE.txt
         
     | 
| 
      
 72 
     | 
    
         
            +
            - README.markdown
         
     | 
| 
      
 73 
     | 
    
         
            +
            files: 
         
     | 
| 
      
 74 
     | 
    
         
            +
            - .document
         
     | 
| 
      
 75 
     | 
    
         
            +
            - .rvmrc
         
     | 
| 
      
 76 
     | 
    
         
            +
            - .travis.yml
         
     | 
| 
      
 77 
     | 
    
         
            +
            - Gemfile
         
     | 
| 
      
 78 
     | 
    
         
            +
            - LICENSE.txt
         
     | 
| 
      
 79 
     | 
    
         
            +
            - README.markdown
         
     | 
| 
      
 80 
     | 
    
         
            +
            - Rakefile
         
     | 
| 
      
 81 
     | 
    
         
            +
            - VERSION
         
     | 
| 
      
 82 
     | 
    
         
            +
            - lib/constructable.rb
         
     | 
| 
      
 83 
     | 
    
         
            +
            - lib/constructable/attribute.rb
         
     | 
| 
      
 84 
     | 
    
         
            +
            - lib/constructable/constructable.rb
         
     | 
| 
      
 85 
     | 
    
         
            +
            - lib/constructable/constructor.rb
         
     | 
| 
      
 86 
     | 
    
         
            +
            - lib/constructable/core_ext.rb
         
     | 
| 
      
 87 
     | 
    
         
            +
            - lib/constructable/exceptions.rb
         
     | 
| 
      
 88 
     | 
    
         
            +
            - test/constructable/test_attribute.rb
         
     | 
| 
      
 89 
     | 
    
         
            +
            - test/constructable/test_constructable.rb
         
     | 
| 
      
 90 
     | 
    
         
            +
            - test/constructable/test_constructor.rb
         
     | 
| 
      
 91 
     | 
    
         
            +
            - test/helper.rb
         
     | 
| 
      
 92 
     | 
    
         
            +
            has_rdoc: true
         
     | 
| 
      
 93 
     | 
    
         
            +
            homepage: http://github.com/mkorfmann/constructable
         
     | 
| 
      
 94 
     | 
    
         
            +
            licenses: 
         
     | 
| 
      
 95 
     | 
    
         
            +
            - MIT
         
     | 
| 
      
 96 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 97 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 98 
     | 
    
         
            +
             
     | 
| 
      
 99 
     | 
    
         
            +
            require_paths: 
         
     | 
| 
      
 100 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 101 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 102 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 103 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 104 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 105 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 106 
     | 
    
         
            +
                  hash: 2461341662544041366
         
     | 
| 
      
 107 
     | 
    
         
            +
                  segments: 
         
     | 
| 
      
 108 
     | 
    
         
            +
                  - 0
         
     | 
| 
      
 109 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 110 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 111 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 112 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 113 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 114 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 115 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 116 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 117 
     | 
    
         
            +
             
     | 
| 
      
 118 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 119 
     | 
    
         
            +
            rubygems_version: 1.6.2
         
     | 
| 
      
 120 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 121 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 122 
     | 
    
         
            +
            summary: Makes constructing objects through an attributes hash easier
         
     | 
| 
      
 123 
     | 
    
         
            +
            test_files: []
         
     | 
| 
      
 124 
     | 
    
         
            +
             
     |