koine-attributes 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.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +10 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +183 -0
- data/Rakefile +21 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/koine-attributes.gemspec +39 -0
- data/lib/koine/attributes/adapter/base.rb +32 -0
- data/lib/koine/attributes/adapter/boolean.rb +39 -0
- data/lib/koine/attributes/adapter/date.rb +15 -0
- data/lib/koine/attributes/adapter/float.rb +12 -0
- data/lib/koine/attributes/adapter/integer.rb +11 -0
- data/lib/koine/attributes/adapter/string.rb +13 -0
- data/lib/koine/attributes/adapter/time.rb +14 -0
- data/lib/koine/attributes/builder.rb +121 -0
- data/lib/koine/attributes/hash_helper.rb +13 -0
- data/lib/koine/attributes/version.rb +5 -0
- data/lib/koine/attributes.rb +152 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 688f7b4338c71beed07a114dd6db4a590a4d66dd
|
4
|
+
data.tar.gz: 3800bfd795ebe7e49e565a192b2bfec7bcdcb13b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 865663b76ada1c397c68b1059a6e28acbccda9465a8fb5caebe3458fd4e2065b289cbb5052ee8dcd40c8886c1f08374e6350efa20ad5be39d33a73522acf5ac3
|
7
|
+
data.tar.gz: 9f421ed25a4de3640922463dd0075e968c2a89164b46ac81bc8eb0d731ba9681eddee9b98c4616f20e54b9b0d544c365aa628cf4f2f4254c06f5c525a91c5a58
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at marcelo.jacobus@xing.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Marcelo Jacobus
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
# Koine::Attributes
|
2
|
+
|
3
|
+
Strong attributes for ruby ruby objects.
|
4
|
+
|
5
|
+
[](https://travis-ci.org/mjacobus/koine-attributes)
|
6
|
+
[](https://coveralls.io/github/mjacobus/koine-attributes?branch=master)
|
7
|
+
[](https://scrutinizer-ci.com/g/mjacobus/koine-attributes/?branch=master)
|
8
|
+
[](https://codeclimate.com/github/mjacobus/koine-attributes)
|
9
|
+
[](https://codeclimate.com/github/mjacobus/koine-attributes)
|
10
|
+
[](https://gemnasium.com/github.com/mjacobus/koine-attributes)
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'koine-attributes'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install koine-attributes
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class Person
|
32
|
+
include Koine::Attributes
|
33
|
+
|
34
|
+
attributes do
|
35
|
+
attribute :name, :string
|
36
|
+
attribute :birthday, :date
|
37
|
+
|
38
|
+
# or
|
39
|
+
attribute :birthday, Koine::Attributes::Adapter::Date.new
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
peson = Person.new
|
44
|
+
person.name = 'John Doe'
|
45
|
+
person.birtday = '2001-02-31' # Date Object can also be given
|
46
|
+
|
47
|
+
person.name # => 'John Doe'
|
48
|
+
person.birtday # => #<Date 2001-02-31>
|
49
|
+
```
|
50
|
+
|
51
|
+
Options:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
attributes do
|
55
|
+
attribute :name, Koine::Attributes::Adapters::Date.new.with_default_value('guest')
|
56
|
+
|
57
|
+
# or
|
58
|
+
attribute :name, :string, ->(adapter) { adapter.with_default_value('guest') }
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
Also, a constructor can be created by the API
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class Person
|
66
|
+
include Koine::Attributes
|
67
|
+
|
68
|
+
attributes initializer: true do
|
69
|
+
attribute :name, :string
|
70
|
+
attribute :birthday, :date
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
person = Person.new(name: 'John Doe', birthday: '2001-01-31')
|
75
|
+
|
76
|
+
foo: attribute will raise error
|
77
|
+
person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
|
78
|
+
```
|
79
|
+
|
80
|
+
You can disable strict mode
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class Person
|
84
|
+
include Koine::Attributes
|
85
|
+
|
86
|
+
attributes initializer: { strict: false } do
|
87
|
+
attribute :name, :string
|
88
|
+
attribute :birthday, :date
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# foo will be ignored
|
93
|
+
person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
|
94
|
+
```
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
class Product
|
98
|
+
include Koine::Attributes
|
99
|
+
|
100
|
+
attributes do
|
101
|
+
attribute :price, MyCustom::Money.new
|
102
|
+
attribute :available, :boolean, ->(attribues){ attributes.with_default_value(true) }
|
103
|
+
attribute :available, Koine::Attributes::Drivers::Boolean.new.with_default_value(true)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
product = Product.new
|
108
|
+
|
109
|
+
product.available # => true
|
110
|
+
product.available = 0
|
111
|
+
product.available # => false
|
112
|
+
|
113
|
+
product.price = { currency: 'USD', value: 100 }
|
114
|
+
|
115
|
+
# or
|
116
|
+
product.price = "100 USD"
|
117
|
+
```
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
class MyCustom::Money
|
121
|
+
def default_value
|
122
|
+
return 'some default_value'
|
123
|
+
end
|
124
|
+
|
125
|
+
def coerce(*values)
|
126
|
+
Money.new(values.first, values.last)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
product = Product.new
|
131
|
+
|
132
|
+
product.available # => true
|
133
|
+
product.available = 0
|
134
|
+
product.available # => false
|
135
|
+
|
136
|
+
product.price = { currency: 'USD', value: 100 }
|
137
|
+
|
138
|
+
# or
|
139
|
+
product.price = "100 USD"
|
140
|
+
```
|
141
|
+
|
142
|
+
### Value objects
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
class Location
|
146
|
+
include Koine::Attributes
|
147
|
+
|
148
|
+
attributes initializer: { freeze: true } do
|
149
|
+
attribute :lat, :float
|
150
|
+
attribute :lon, :float
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
location = Location.new(lat: 1, lon: 2)
|
155
|
+
new_location = location.with_lon(3)
|
156
|
+
```
|
157
|
+
|
158
|
+
### Standard types
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
:boolean, ->(adapter) { adapter.append_true_value('yes').append_false_value('no') }
|
162
|
+
:date
|
163
|
+
:float
|
164
|
+
:integer
|
165
|
+
:string
|
166
|
+
:time
|
167
|
+
```
|
168
|
+
|
169
|
+
## Development
|
170
|
+
|
171
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
172
|
+
|
173
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
174
|
+
|
175
|
+
## Contributing
|
176
|
+
|
177
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mjacobus/koine-attributes. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
178
|
+
|
179
|
+
|
180
|
+
## License
|
181
|
+
|
182
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
183
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
namespace :test do
|
9
|
+
|
10
|
+
desc "run tests and generate invoke coveralls"
|
11
|
+
task :coveralls do
|
12
|
+
ENV['COVERALLS'] = 'true'
|
13
|
+
Rake::Task['test:coverage'].invoke
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "run tests and generate code coverage"
|
17
|
+
task :coverage do
|
18
|
+
ENV['COVERAGE'] = 'true'
|
19
|
+
Rake::Task[:spec].invoke
|
20
|
+
end
|
21
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "koine/attributes"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'koine/attributes/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'koine-attributes'
|
9
|
+
spec.version = Koine::Attributes::VERSION
|
10
|
+
spec.authors = ['Marcelo Jacobus']
|
11
|
+
spec.email = ['marcelo.jacobus@xing.com']
|
12
|
+
|
13
|
+
spec.summary = 'Stronger setters and getters'
|
14
|
+
spec.description = 'Stronger getter and setters for your ruby classes'
|
15
|
+
spec.homepage = 'https://github.com/mjacobus/koine-attributes'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
22
|
+
else
|
23
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
24
|
+
'public gem pushes.'
|
25
|
+
end
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
28
|
+
f.match(%r{^(test|spec|features)/})
|
29
|
+
end
|
30
|
+
spec.bindir = 'exe'
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ['lib']
|
33
|
+
|
34
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
35
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
36
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
37
|
+
spec.add_development_dependency 'coveralls'
|
38
|
+
spec.add_development_dependency 'simplecov'
|
39
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Koine
|
2
|
+
module Attributes
|
3
|
+
module Adapter
|
4
|
+
class Base
|
5
|
+
def coerce(*_values)
|
6
|
+
raise NotImplementedError
|
7
|
+
end
|
8
|
+
|
9
|
+
def default_value
|
10
|
+
@default_value.respond_to?(:call) &&
|
11
|
+
@default_value.call ||
|
12
|
+
@default_value
|
13
|
+
end
|
14
|
+
|
15
|
+
def with_default_value(value = nil, &block)
|
16
|
+
@default_value = value
|
17
|
+
@default_value = block if block
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
protected
|
22
|
+
|
23
|
+
# duplicates if possible and freezes object
|
24
|
+
def secure
|
25
|
+
value = yield
|
26
|
+
value = value.dup if value.respond_to?(:dup)
|
27
|
+
value.freeze
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Koine
|
2
|
+
module Attributes
|
3
|
+
module Adapter
|
4
|
+
class Boolean < Base
|
5
|
+
DEFAULT_TRUE_VALUES = [1, '1', true, 'true'].freeze
|
6
|
+
DEFAULT_FALSE_VALUES = [0, '0', false, 'false'].freeze
|
7
|
+
|
8
|
+
def initialize(
|
9
|
+
true_values: DEFAULT_TRUE_VALUES.dup,
|
10
|
+
false_values: DEFAULT_FALSE_VALUES.dup
|
11
|
+
)
|
12
|
+
@true_values = true_values
|
13
|
+
@false_values = false_values
|
14
|
+
end
|
15
|
+
|
16
|
+
def coerce(value)
|
17
|
+
return true if true_values.include?(value)
|
18
|
+
return false if false_values.include?(value)
|
19
|
+
raise ArgumentError, "Invalid argument '#{value}'"
|
20
|
+
end
|
21
|
+
|
22
|
+
def append_true_value(value)
|
23
|
+
true_values << value
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def append_false_value(value)
|
28
|
+
false_values << value
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
attr_reader :true_values
|
35
|
+
attr_reader :false_values
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'koine/attributes/hash_helper'
|
2
|
+
|
3
|
+
module Koine
|
4
|
+
module Attributes
|
5
|
+
class Builder
|
6
|
+
attr_reader :attributes
|
7
|
+
attr_reader :target
|
8
|
+
|
9
|
+
def initialize(target:)
|
10
|
+
@target = target
|
11
|
+
@attributes = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def build(name, driver)
|
15
|
+
attributes.push(name.to_sym)
|
16
|
+
build_getter(name, driver)
|
17
|
+
build_setter(name, driver)
|
18
|
+
end
|
19
|
+
|
20
|
+
def build_getter(name, driver)
|
21
|
+
target.class_eval do
|
22
|
+
define_method(name) do
|
23
|
+
instance_variable_get("@#{name}") || driver.default_value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def build_setter(name, driver)
|
29
|
+
setter = "#{name}="
|
30
|
+
|
31
|
+
target.class_eval do
|
32
|
+
define_method(setter) do |*args|
|
33
|
+
instance_variable_set("@#{name}", driver.coerce(*args))
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def build_constructor(strict: true, freeze: false)
|
39
|
+
valid_attributes = attributes
|
40
|
+
|
41
|
+
target.class_eval do
|
42
|
+
def initialize(args = {})
|
43
|
+
initialize_attributes(args)
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
define_method(:initialize_attributes) do |constructor_args|
|
49
|
+
invalid_attributes = []
|
50
|
+
constructor_args = HashHelper.new.symbolize_keys(constructor_args)
|
51
|
+
|
52
|
+
constructor_args.each do |name, value|
|
53
|
+
if valid_attributes.include?(name)
|
54
|
+
setter = "#{name}=".to_sym
|
55
|
+
send(setter, value)
|
56
|
+
next
|
57
|
+
end
|
58
|
+
|
59
|
+
next unless strict
|
60
|
+
invalid_attributes << name
|
61
|
+
end
|
62
|
+
|
63
|
+
unless invalid_attributes.empty?
|
64
|
+
attributes = invalid_attributes.join(', ')
|
65
|
+
raise ArgumentError, "Invalid attributes (#{attributes})"
|
66
|
+
end
|
67
|
+
|
68
|
+
self.freeze if freeze
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
if strict
|
73
|
+
make_setters_private
|
74
|
+
create_with_methods
|
75
|
+
create_comparator
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
|
81
|
+
def make_setters_private
|
82
|
+
attrs = attributes
|
83
|
+
|
84
|
+
target.instance_eval do
|
85
|
+
attrs.each do |attr|
|
86
|
+
protected "#{attr}="
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def create_with_methods
|
92
|
+
attributes.each do |attr|
|
93
|
+
create_with_method(attr)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def create_with_method(attr)
|
98
|
+
target.class_eval do
|
99
|
+
define_method "with_#{attr}" do |*args|
|
100
|
+
dup.tap do |new_object|
|
101
|
+
new_object.send("#{attr}=", *args)
|
102
|
+
new_object.freeze
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def create_comparator
|
109
|
+
attrs = attributes
|
110
|
+
|
111
|
+
target.class_eval do
|
112
|
+
define_method :== do |that|
|
113
|
+
a = attrs.map { |attr| send(attr) }
|
114
|
+
b = attrs.map { |attr| that.send(attr) }
|
115
|
+
a == b
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'koine/attributes/version'
|
2
|
+
require 'koine/attributes/builder'
|
3
|
+
require 'koine/attributes/adapter/base'
|
4
|
+
|
5
|
+
# provides the following API
|
6
|
+
#
|
7
|
+
# @example using attributes
|
8
|
+
# class Person
|
9
|
+
# include Koine::Attributes
|
10
|
+
#
|
11
|
+
# attributes do
|
12
|
+
# attribute :name, :string
|
13
|
+
# attribute :birthday, :date
|
14
|
+
#
|
15
|
+
# # or
|
16
|
+
# attribute :birthday, Koine::Attributes::Adapter::Date.new
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# peson = Person.new
|
21
|
+
# person.name = 'John Doe'
|
22
|
+
# person.birtday = '2001-02-31' # Date Object can also be given
|
23
|
+
#
|
24
|
+
# person.name # => 'John Doe'
|
25
|
+
# person.birtday # => #<Date 2001-02-31>
|
26
|
+
#
|
27
|
+
# @example custom attribute options
|
28
|
+
#
|
29
|
+
# attributes do
|
30
|
+
# attribute :name, Koine::Attributes::Adapters::Date.new.with_default('guest')
|
31
|
+
#
|
32
|
+
# # or
|
33
|
+
# attribute :name, :string, ->(adapter) { adapter.with_default('guest') }
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# @example Constructor for attributes
|
37
|
+
#
|
38
|
+
# class Person
|
39
|
+
# include Koine::Attributes
|
40
|
+
#
|
41
|
+
# attributes initializer: true do
|
42
|
+
# attribute :name, :string
|
43
|
+
# attribute :birthday, :date
|
44
|
+
# end
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# person = Person.new(name: 'John Doe', birthday: '2001-01-31')
|
48
|
+
#
|
49
|
+
# # foo: attribute will raise error with strict mode
|
50
|
+
# person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
|
51
|
+
#
|
52
|
+
# @example Constructor for attributes withouth strict mode
|
53
|
+
#
|
54
|
+
# class Person
|
55
|
+
# include Koine::Attributes
|
56
|
+
#
|
57
|
+
# attributes initializer: { strict: false } do
|
58
|
+
# attribute :name, :string
|
59
|
+
# attribute :birthday, :date
|
60
|
+
# end
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# # foo will be ignored
|
64
|
+
# person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
|
65
|
+
#
|
66
|
+
# @example Override constructor
|
67
|
+
#
|
68
|
+
# class Person
|
69
|
+
# include Koine::Attributes
|
70
|
+
#
|
71
|
+
# attr_reader :foo
|
72
|
+
#
|
73
|
+
# attributes initializer: true do
|
74
|
+
# attribute :name, :string
|
75
|
+
# attribute :birthday, :date
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# def initializ(attributes = {})
|
79
|
+
# @foo = attributes.delete(:foo)
|
80
|
+
#
|
81
|
+
# initialize_attributes(attributes) # protected
|
82
|
+
# end
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
|
86
|
+
# person.foo # => :bar
|
87
|
+
#
|
88
|
+
# @example
|
89
|
+
# class Location
|
90
|
+
# include Koine::Attributes
|
91
|
+
#
|
92
|
+
# attributes initializer: { freeze: true } do
|
93
|
+
# attribute :lat, :flaot
|
94
|
+
# attribute :lon, :flaot
|
95
|
+
# end
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# location = Location.new(lat: 1, lon: 2)
|
99
|
+
# new_location = location.with_lon(3)
|
100
|
+
#
|
101
|
+
module Koine
|
102
|
+
module Attributes
|
103
|
+
module Adapter
|
104
|
+
autoload :Boolean, 'koine/attributes/adapter/boolean'
|
105
|
+
autoload :Date, 'koine/attributes/adapter/date'
|
106
|
+
autoload :Time, 'koine/attributes/adapter/time'
|
107
|
+
autoload :Float, 'koine/attributes/adapter/float'
|
108
|
+
autoload :Integer, 'koine/attributes/adapter/integer'
|
109
|
+
autoload :String, 'koine/attributes/adapter/string'
|
110
|
+
end
|
111
|
+
|
112
|
+
Error = Class.new(StandardError)
|
113
|
+
|
114
|
+
def self.included(base)
|
115
|
+
base.extend(ClassMethods)
|
116
|
+
end
|
117
|
+
|
118
|
+
module ClassMethods
|
119
|
+
def attributes(options = {}, &block)
|
120
|
+
@builder = Builder.new(target: self)
|
121
|
+
class_eval(&block)
|
122
|
+
|
123
|
+
if options[:initializer]
|
124
|
+
initializer_options = options[:initializer]
|
125
|
+
|
126
|
+
initializer_options = {} unless initializer_options.is_a?(Hash)
|
127
|
+
|
128
|
+
@builder.build_constructor(initializer_options)
|
129
|
+
end
|
130
|
+
|
131
|
+
@builder = nil
|
132
|
+
end
|
133
|
+
|
134
|
+
def attribute(name, adapter, lambda_constructor = nil, &block)
|
135
|
+
unless @builder
|
136
|
+
raise Error, 'You must call .attribute inside the .attributes block'
|
137
|
+
end
|
138
|
+
|
139
|
+
adapter = coerce_adapter(adapter, lambda_constructor, &block)
|
140
|
+
@builder.build(name, adapter)
|
141
|
+
end
|
142
|
+
|
143
|
+
private def coerce_adapter(adapter, lambda_constructor, &block)
|
144
|
+
return adapter unless adapter.instance_of?(::Symbol)
|
145
|
+
adapter = const_get("Koine::Attributes::Adapter::#{adapter.to_s.capitalize}").new
|
146
|
+
lambda_constructor.call(adapter) if lambda_constructor
|
147
|
+
yield(adapter) if block
|
148
|
+
adapter
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: koine-attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcelo Jacobus
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-03 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.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
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.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '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'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Stronger getter and setters for your ruby classes
|
84
|
+
email:
|
85
|
+
- marcelo.jacobus@xing.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- koine-attributes.gemspec
|
101
|
+
- lib/koine/attributes.rb
|
102
|
+
- lib/koine/attributes/adapter/base.rb
|
103
|
+
- lib/koine/attributes/adapter/boolean.rb
|
104
|
+
- lib/koine/attributes/adapter/date.rb
|
105
|
+
- lib/koine/attributes/adapter/float.rb
|
106
|
+
- lib/koine/attributes/adapter/integer.rb
|
107
|
+
- lib/koine/attributes/adapter/string.rb
|
108
|
+
- lib/koine/attributes/adapter/time.rb
|
109
|
+
- lib/koine/attributes/builder.rb
|
110
|
+
- lib/koine/attributes/hash_helper.rb
|
111
|
+
- lib/koine/attributes/version.rb
|
112
|
+
homepage: https://github.com/mjacobus/koine-attributes
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata:
|
116
|
+
allowed_push_host: https://rubygems.org
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.6.8
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: Stronger setters and getters
|
137
|
+
test_files: []
|