simple_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.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +2 -0
- data/lib/simple_attrs.rb +39 -0
- data/lib/simple_attrs/version.rb +3 -0
- data/simple_attrs.gemspec +25 -0
- data/spec/lib/simple_attrs_spec.rb +80 -0
- data/spec/spec_helper.rb +8 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c511452b534540bc99df27d27f5ee2817910a860
|
4
|
+
data.tar.gz: e106a056b408c021f4f7e63d44368d883adb4d8c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5f9a70e3875a36b2f5783aec75d45c82c28410c4e6bb60957eae7e9f535f8ad8d5e72163192fe613487409c49272a05ae2117f935828a1b400e260ce1e23281d
|
7
|
+
data.tar.gz: 4dd8a3b9c51db099363c6c0efc21adef0613c89ccbeb20e5e7b7b6627ef46765316b8d1e45c0c58ae39e090a061f5962a382810e25ae665523981bcbfcd51276
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 TODO: Write your name
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# SimpleAttrs
|
2
|
+
|
3
|
+
The goal is to provide a simple DSL to define attributes for your
|
4
|
+
models.
|
5
|
+
|
6
|
+
## Features
|
7
|
+
- Adds a reader/write for each attribute using `attr_accessor`.
|
8
|
+
- Adds an initializer that takes in an options hash to define intial
|
9
|
+
values. The default initializer will ensure that the option hash only
|
10
|
+
includes values defined as a simple_attr.
|
11
|
+
|
12
|
+
SimpleAttrs does not support type checking, coercion or default
|
13
|
+
values. For a more complex solution, look at these other great
|
14
|
+
libraries: [Attrs](https://github.com/wojtekmach/attrs),
|
15
|
+
[Attrio](https://github.com/jetrockets/attrio), [Virtus](https://github.com/solnic/virtus)
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'simple_attrs'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install simple_attrs
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'simple_attrs'
|
37
|
+
|
38
|
+
class SimpleExample
|
39
|
+
include SimpleAttrs
|
40
|
+
|
41
|
+
has_simple_attr :attr1
|
42
|
+
has_simple_attr :attr2
|
43
|
+
has_simple_attr :attr3
|
44
|
+
end
|
45
|
+
|
46
|
+
ex = SimpleExample.new(attr1: 1, attr2: 2)
|
47
|
+
ex.attributes
|
48
|
+
=> { attr1: 1, attr2: 2, attr3: nil }
|
49
|
+
|
50
|
+
ex = SimpleExample.new(invalid_attr: 1)
|
51
|
+
ArgumentError: Cannot set invalid_attr through initializer
|
52
|
+
```
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it ( https://github.com/codeshoppe/simple_attrs/fork )
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/simple_attrs.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "simple_attrs/version"
|
2
|
+
|
3
|
+
module SimpleAttrs
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
|
7
|
+
base.class_eval do
|
8
|
+
@simple_attrs ||= {}
|
9
|
+
|
10
|
+
def initialize(options = {})
|
11
|
+
options.each do |key, value|
|
12
|
+
if self.class.simple_attrs.has_key?(key.to_sym)
|
13
|
+
instance_variable_set("@#{key}", value)
|
14
|
+
else
|
15
|
+
raise ArgumentError, "Cannot set #{key} through initializer"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def attributes
|
21
|
+
self.class.simple_attrs.inject({}) do |result, attr_pair|
|
22
|
+
key, _value = attr_pair
|
23
|
+
result.merge(key => self.public_send(key))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def has_simple_attr(attr_name)
|
31
|
+
@simple_attrs[attr_name.to_sym] = nil
|
32
|
+
attr_accessor attr_name
|
33
|
+
end
|
34
|
+
|
35
|
+
def simple_attrs
|
36
|
+
@simple_attrs
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -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 'simple_attrs/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple_attrs"
|
8
|
+
spec.version = SimpleAttrs::VERSION
|
9
|
+
spec.authors = ["Jennifer Reyes"]
|
10
|
+
spec.email = ["jgdreyes@gmail.com"]
|
11
|
+
spec.summary = %q{Simple attrs DSL}
|
12
|
+
spec.description = %q{DSL for adding attributes to any plain old ruby object.}
|
13
|
+
spec.homepage = "http://codeshoppe.github.io/simple_attrs"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
24
|
+
spec.add_development_dependency "byebug", "~> 3.5"
|
25
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SimpleAttrs do
|
4
|
+
context 'setting an attribute' do
|
5
|
+
let(:klass) { Class.new { include SimpleAttrs } }
|
6
|
+
|
7
|
+
it 'defines a reader' do
|
8
|
+
klass.has_simple_attr(:valid_attr)
|
9
|
+
instance = klass.new
|
10
|
+
expect(instance).to respond_to("valid_attr")
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'defines a writer' do
|
14
|
+
klass.has_simple_attr(:valid_attr)
|
15
|
+
instance = klass.new
|
16
|
+
expect(instance).to respond_to("valid_attr=")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context '#intialize' do
|
21
|
+
let(:klass) { Class.new { include SimpleAttrs } }
|
22
|
+
let(:klass_with_custom_initialize) do
|
23
|
+
Class.new {
|
24
|
+
include SimpleAttrs
|
25
|
+
has_simple_attr :valid_attr
|
26
|
+
def initialize(some_value)
|
27
|
+
@some_value = some_value
|
28
|
+
end
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'defines an instance variable' do
|
33
|
+
klass.has_simple_attr(:valid_attr)
|
34
|
+
instance = klass.new(valid_attr: 3)
|
35
|
+
expect(instance).to be_instance_variable_defined(:@valid_attr)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'sets the value of the attribute' do
|
39
|
+
klass.has_simple_attr(:valid_attr)
|
40
|
+
instance = klass.new(valid_attr: 3)
|
41
|
+
expect(instance.valid_attr).to eq(3)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'raises error when trying to initialize non simple attr' do
|
45
|
+
klass.has_simple_attr(:valid_attr)
|
46
|
+
expect {
|
47
|
+
klass.new(valid_attr: 3, invalid_attr: 4)
|
48
|
+
}.to raise_error(ArgumentError, "Cannot set invalid_attr through initializer")
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'allows for initialize to be overwritten' do
|
52
|
+
expect {
|
53
|
+
klass_with_custom_initialize.new(3)
|
54
|
+
}.not_to raise_error
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'allows for initialize to be overwritten' do
|
58
|
+
expect {
|
59
|
+
klass_with_custom_initialize.new(3)
|
60
|
+
}.not_to raise_error
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#attributes' do
|
65
|
+
let(:klass) do
|
66
|
+
Class.new {
|
67
|
+
include SimpleAttrs
|
68
|
+
attr_accessor :some_other_attr
|
69
|
+
}
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'returns all simple_attr attributes' do
|
73
|
+
klass.has_simple_attr(:valid_attr1)
|
74
|
+
klass.has_simple_attr(:valid_attr2)
|
75
|
+
|
76
|
+
instance = klass.new(valid_attr1: 'test')
|
77
|
+
expect(instance.attributes).to eq({valid_attr1: 'test', valid_attr2: nil})
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_attrs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jennifer Reyes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-24 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.5'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.5'
|
69
|
+
description: DSL for adding attributes to any plain old ruby object.
|
70
|
+
email:
|
71
|
+
- jgdreyes@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/simple_attrs.rb
|
83
|
+
- lib/simple_attrs/version.rb
|
84
|
+
- simple_attrs.gemspec
|
85
|
+
- spec/lib/simple_attrs_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
homepage: http://codeshoppe.github.io/simple_attrs
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.2.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Simple attrs DSL
|
111
|
+
test_files:
|
112
|
+
- spec/lib/simple_attrs_spec.rb
|
113
|
+
- spec/spec_helper.rb
|