attrs 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 831e360a852a5219f76a80451787e234b1d2945c
4
+ data.tar.gz: c19032b5e3cf6888752e6068efb0770422d77a27
5
+ SHA512:
6
+ metadata.gz: 793ca776c058b0577bec1fc744f4283087b5350371c39d5fd94ec892a385c4820f3c7a4b686ac613e7bf8d6733ab59576f8261a6f14b567ca32c460470816bec
7
+ data.tar.gz: 00dc67d08c02a0a3aad66d5adb916dca0115eaf773991035acb2d1bac0c0ad73e6d0fa62c40d436f75e1ec5c0fe50e6b723162d7f51bc619fc8fbdbf2343b22c
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in attrs.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Wojciech Mach
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,93 @@
1
+ # Attrs
2
+
3
+ [![Build Status](https://secure.travis-ci.org/wojtekmach/attrs.png?branch=master)](http://travis-ci.org/wojtekmach/attrs)
4
+
5
+ Yet another attributes on steroids gem.
6
+
7
+ Heavily inspired by [Virtus](https://github.com/solnic/virtus) and
8
+ [Values](https://github.com/tcrayford/Values) which both are great projects.
9
+
10
+ The purpose of this project is to have a small & simple codebase and minimum number of features.
11
+ For a more complex solution I strongly recommend **Virtus** which I'm happily using in production.
12
+
13
+ ## Features
14
+
15
+ * immutability
16
+ * all attributes must be specified on initialisation. Hopefully less `nil`'s flying around
17
+ * uses `attr_reader` and `attr_writer`. Can be easily overwritten
18
+ * just ~50 LOC (not including coercion support)
19
+ * no external dependencies (again, not counting coercion which requires coercible gem)
20
+
21
+ ## Usage
22
+
23
+ Let's write a `Person` class:
24
+
25
+ ```ruby
26
+ require 'attrs'
27
+
28
+ class Person < Attrs(:name, :age)
29
+ private
30
+
31
+ def age=(new_age)
32
+ super(new_age.to_i)
33
+ end
34
+ end
35
+
36
+ person = Person.new(name: 'John Doe', age: '26')
37
+ ```
38
+
39
+ with this code we can:
40
+
41
+ * get the attributes hash: `person.attributes # => {:name => "John Doe", :age => 26}`
42
+ * get the attribute names: `Person.attribute_names # => [:name, :age]`
43
+ * compare with other objects: `person == {:name => "John Doe", :age => 26} # => true`
44
+
45
+ and more! See: <https://github.com/wojtekmach/attrs/blob/master/test/attrs_test.rb>
46
+
47
+ ### Coercion
48
+
49
+ Notice in previous example we added custom `age=` writer to coerce argument to integer.
50
+
51
+ One of my favourite features of **Virtus** is attribute coercion, and you can use it here too.
52
+ In fact it's using the same library that was extracted out from **Virtus**: <https://github.com/solnic/coercible>
53
+
54
+ ```
55
+ gem install coercible
56
+ ```
57
+
58
+ ```ruby
59
+ require 'attrs/coercible'
60
+
61
+ class Person < Attrs(name: String, age: Integer)
62
+ end
63
+ ```
64
+
65
+ or, simply:
66
+
67
+ ```ruby
68
+ Person = Attrs(name: String, age: Integer)
69
+ ```
70
+
71
+ (note, using 2nd style you won't be able to use `super` when overwriting methods)
72
+
73
+ ## Installation
74
+
75
+ Add this line to your application's Gemfile:
76
+
77
+ gem 'attrs'
78
+
79
+ And then execute:
80
+
81
+ $ bundle
82
+
83
+ Or install it yourself as:
84
+
85
+ $ gem install attrs
86
+
87
+ ## Contributing
88
+
89
+ 1. Fork it
90
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
91
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
92
+ 4. Push to the branch (`git push origin my-new-feature`)
93
+ 5. Create new Pull Request
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'lib'
6
+ t.libs << 'test'
7
+ t.pattern = 'test/**/*_test.rb'
8
+ t.verbose = false
9
+ end
10
+
11
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'attrs/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "attrs"
8
+ spec.version = Attrs::VERSION
9
+ spec.authors = ["Wojciech Mach"]
10
+ spec.email = ["wojtek@wojtekmach.pl"]
11
+ spec.description = "Yet another attributes on steroids gem"
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/wojtekmach/attrs"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest", "~> 5.0.0"
24
+ spec.add_development_dependency "coercible", "~> 0.2.0"
25
+ end
@@ -0,0 +1,48 @@
1
+ require 'attrs/version'
2
+
3
+ def Attrs(*args, &block)
4
+ Attrs.new(*args, &block)
5
+ end
6
+
7
+ module Attrs
8
+ def self.new(*attribute_names, &block)
9
+ Class.new do
10
+ const_set(:ATTRIBUTE_NAMES, attribute_names)
11
+
12
+ def self.attribute_names
13
+ self::ATTRIBUTE_NAMES
14
+ end
15
+
16
+ def self.attr(name)
17
+ attr_accessor name
18
+ private :"#{name}="
19
+ end
20
+
21
+ attribute_names.each { |name| attr name }
22
+
23
+ def initialize(attributes)
24
+ self.class.attribute_names.each do |name|
25
+ self.send("#{name}=", attributes.fetch(name))
26
+ end
27
+ end
28
+
29
+ def inspect
30
+ "#<#{self.class.name} #{attributes.inspect}>"
31
+ end
32
+
33
+ def ==(other)
34
+ to_hash == other.to_hash
35
+ end
36
+
37
+ def attributes
38
+ self.class.attribute_names.each_with_object({}) do |name, hash|
39
+ hash[name] = send(name)
40
+ end
41
+ end
42
+
43
+ alias_method :to_hash, :attributes
44
+
45
+ class_eval(&block) if block_given?
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'coercible'
3
+ rescue LoadError => e
4
+ puts "Coercion support requires coercible: gem install coercible"
5
+ raise e
6
+ end
7
+
8
+ def Attrs(*attributes, &block)
9
+ if attributes.size == 1 && attributes.first.is_a?(Hash)
10
+ CoercibleAttrs.new(attributes.first, &block)
11
+ else
12
+ Attrs.new(*attributes, &block)
13
+ end
14
+ end
15
+
16
+ module CoercibleAttrs
17
+ def self.new(attributes, &block)
18
+ Attrs.new(*attributes.keys) do
19
+ const_set(:ATTRIBUTES, attributes)
20
+ const_set(:COERCER, Coercible::Coercer.new)
21
+
22
+ def self.attr(name, type)
23
+ define_method :"#{name}=" do |val|
24
+ coercion = "to_" + self.class::ATTRIBUTES[name].name.downcase
25
+ val = self.class::COERCER[val.class].send(coercion, val)
26
+ instance_variable_set(:"@#{name}", val)
27
+ end
28
+
29
+ private :"#{name}="
30
+ end
31
+
32
+ attributes.each { |name, type| attr(name, type) }
33
+
34
+ class_eval(&block) if block_given?
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Attrs
2
+ VERSION = File.read(File.expand_path('../../../VERSION', __FILE__)).chomp
3
+ end
@@ -0,0 +1,73 @@
1
+ require 'minitest/autorun'
2
+ require 'attrs'
3
+
4
+ class Person < Attrs(:name, :age)
5
+ private
6
+
7
+ def age=(new_age)
8
+ super(new_age.to_i)
9
+ end
10
+ end
11
+
12
+ class PersonTest < Minitest::Spec
13
+ let(:valid_attributes) { {name: "John Doe", age: 26} }
14
+ let(:klass) { Person }
15
+
16
+ it "responds to .attribute_names" do
17
+ klass.attribute_names.must_equal [:name, :age]
18
+ end
19
+
20
+ it "can be instantiated" do
21
+ klass.new(valid_attributes).must_be_kind_of klass
22
+ end
23
+
24
+ it "has string representation" do
25
+ str = klass.new(valid_attributes).inspect
26
+ str.must_equal "#<#{klass} #{valid_attributes.inspect}>"
27
+ end
28
+
29
+ it "must be instantiated with all attributes" do
30
+ proc {
31
+ klass.new(age: 26)
32
+ }.must_raise KeyError
33
+ end
34
+
35
+ it "can access individual attributes" do
36
+ klass.new(valid_attributes).name.must_equal "John Doe"
37
+ end
38
+
39
+ it "can return all attributes" do
40
+ klass.new(valid_attributes).attributes.must_equal valid_attributes
41
+ end
42
+
43
+ it "can overwrite setting of an attribute" do
44
+ klass.new(valid_attributes.merge(age: '26')).age.must_equal 26
45
+ end
46
+
47
+ it "cannot mutate attributes" do
48
+ person = klass.new(valid_attributes)
49
+
50
+ proc {
51
+ person.name = 'Joe'
52
+ }.must_raise NoMethodError
53
+ end
54
+
55
+ it "is equal to an object with the same attributes" do
56
+ klass.new(valid_attributes).must_equal klass.new(valid_attributes)
57
+
58
+ klass.new(valid_attributes).must_equal valid_attributes
59
+ end
60
+
61
+ it "is equal to hash of the same attributes" do
62
+ klass.new(valid_attributes).must_equal valid_attributes
63
+ end
64
+ end
65
+
66
+ require 'attrs/coercible'
67
+
68
+ class Person2 < Attrs(name: String, age: Integer)
69
+ end
70
+
71
+ class Person2Test < PersonTest
72
+ let(:klass) { Person2 }
73
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attrs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Wojciech Mach
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 5.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 5.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: coercible
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.2.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.2.0
69
+ description: Yet another attributes on steroids gem
70
+ email:
71
+ - wojtek@wojtekmach.pl
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - VERSION
83
+ - attrs.gemspec
84
+ - lib/attrs.rb
85
+ - lib/attrs/coercible.rb
86
+ - lib/attrs/version.rb
87
+ - test/attrs_test.rb
88
+ homepage: https://github.com/wojtekmach/attrs
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Yet another attributes on steroids gem
112
+ test_files:
113
+ - test/attrs_test.rb