ure 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +109 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/ure.rb +85 -0
- data/lib/ure/version.rb +3 -0
- data/ure-0.0.1.gem +0 -0
- data/ure.gemspec +31 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6aee97285756be61b5d50c642d983b6fcc513b47
|
4
|
+
data.tar.gz: fd53cee9caf6a3afa0d824d89bcc2b5c6bd436af
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1637dd78f9d42d2aab243a61bf287e8f4ae1eb9e0387de4af3b745b612e60197198af9e4e1c789119cc78cfc4d3ac1f2a2e87112034479a9944a133ae690c348
|
7
|
+
data.tar.gz: 87a9c366245af1dc09e236615825ae0495acd331d8e377805c682614152d9d70f25f51b8c3c888d6ba5a58f600a4da1b766fef9d8630ed51e9398ec0859d0af4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Calvyn82
|
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,109 @@
|
|
1
|
+
# Ure
|
2
|
+
|
3
|
+
Ure is a Ruby Gem that fixes the biggest problems with Struct, namely the lack of immutability and required keyword arguments.
|
4
|
+
|
5
|
+
For example, you can do this with Struct:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
Car = Struct.new(:paint, :year)
|
9
|
+
=> Car
|
10
|
+
scooby_van = Car.new
|
11
|
+
=> #<struct Car paint=nil, year=nil>
|
12
|
+
```
|
13
|
+
|
14
|
+
Even worse, what if we include one argument and forget the other?
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
myster_machine = Car.new(:mural)
|
18
|
+
=> #<struct Car paint=:mural, year=nil>
|
19
|
+
```
|
20
|
+
Fixing it with Struct's built-in accessors after the fact isn't great, either. What we want is a Struct that has required arguments, and then throws a useful error if we give it the wrong thing.
|
21
|
+
|
22
|
+
That's where Ure comes in.
|
23
|
+
|
24
|
+
First, let's show a successful implementation of the same thing in Ure.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'ure'
|
28
|
+
=> true
|
29
|
+
Car = Ure.new(:paint, :year)
|
30
|
+
=> Car
|
31
|
+
scooby_van = Car.new(year: 1965, paint: :mural)
|
32
|
+
=> #ure {:year=>1968, :paint=>:mural}
|
33
|
+
```
|
34
|
+
Notice that it no longer matters what order `:year` and `:paint` are given to `Car`, because we have keyword arguments.
|
35
|
+
|
36
|
+
But that's not all.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
mystery_machine = Car.new
|
40
|
+
NameError: uninitialized constant Ure::ArgumentError
|
41
|
+
```
|
42
|
+
|
43
|
+
We get a useful error message, instead of a data object populated with `nil`'s.
|
44
|
+
|
45
|
+
Also, what happens if we try to change the values in an existing Ure data object?
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
scooby_van = Car.new(year: 1965, paint: :mural)
|
49
|
+
=> #ure {:year=>1968, :paint=>:mural}
|
50
|
+
scooby_van.year = 2000
|
51
|
+
NoMethodError: undefined method `year=' for #<ure {:year=>1965, :paint=>:mural}
|
52
|
+
```
|
53
|
+
## Installation
|
54
|
+
|
55
|
+
Add this line to your application's Gemfile:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
gem 'ure'
|
59
|
+
```
|
60
|
+
|
61
|
+
And then execute:
|
62
|
+
|
63
|
+
$ bundle
|
64
|
+
|
65
|
+
Or install it yourself as:
|
66
|
+
|
67
|
+
$ gem install ure
|
68
|
+
|
69
|
+
## Usage
|
70
|
+
|
71
|
+
To use Ure, just require it in whatever file is implementing it and treat it like a Struct.
|
72
|
+
|
73
|
+
Ure will try to implement as public methods whatever keys you pass in as a hash. For speed purposes, this means that if you pass in an existing method that Ure recognizes, it'll override it. Ure inherits directly from `BasicObject`, so it has very few methods other than its public facing instance methods. Still, this is something to watch out for. If you're passing in ':instance_eval` or a lone `!` as keys, you'll have problems.
|
74
|
+
|
75
|
+
Any methods on `Struct` that require indexing or care about the position of arguments have been depricated or changed to only care about the name of the argument passed.
|
76
|
+
|
77
|
+
Ure's current public methods are:
|
78
|
+
|
79
|
+
`#[]` - Because Ure doesn't care about indexing, this allows users to treat instances of Ure as a hash.
|
80
|
+
|
81
|
+
`#each(&block) - Converts the fields into a hash and calls each on them.
|
82
|
+
|
83
|
+
`#to_s` - Returns a string describing the object and its fields.
|
84
|
+
|
85
|
+
`#inspect`- Alians for `.to_s`
|
86
|
+
|
87
|
+
`#to_a` - Alias for `.values`.
|
88
|
+
|
89
|
+
`#to_h` - Returns a hash of the fields.
|
90
|
+
|
91
|
+
`#values` - Returns an array of the values in the fields.
|
92
|
+
|
93
|
+
`#values_at` - Takes one or more keys as arguments, and returns an array of the corresponding values.
|
94
|
+
|
95
|
+
## Development
|
96
|
+
|
97
|
+
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.
|
98
|
+
|
99
|
+
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).
|
100
|
+
|
101
|
+
## Contributing
|
102
|
+
|
103
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Calvyn82/ure. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
104
|
+
|
105
|
+
|
106
|
+
## License
|
107
|
+
|
108
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
109
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ure"
|
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
|
data/bin/setup
ADDED
data/lib/ure.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require "ure/version"
|
2
|
+
class Ure < BasicObject
|
3
|
+
def self.members
|
4
|
+
@members
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.new(*members, &body)
|
8
|
+
::Class.new(self) do
|
9
|
+
instance_variable_set(:@members, members)
|
10
|
+
|
11
|
+
def self.new(*args, &block)
|
12
|
+
object = allocate
|
13
|
+
object.__send__(:initialize, *args, &block) if respond_to?(:initialize, true)
|
14
|
+
object
|
15
|
+
end
|
16
|
+
|
17
|
+
define_method(:members) do
|
18
|
+
@members ||= members
|
19
|
+
end
|
20
|
+
|
21
|
+
class_eval(&body) if body
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(fields = {})
|
26
|
+
fail "'fields' must be a 'Hash'" unless fields.is_a?(::Hash)
|
27
|
+
|
28
|
+
members.each do |member|
|
29
|
+
fail ArgumentError, "missing keyword: #{member}" unless fields.include?(member)
|
30
|
+
instance_eval <<-END_RUBY
|
31
|
+
def #{member}
|
32
|
+
i = members.index(#{member.inspect})
|
33
|
+
values[i]
|
34
|
+
end
|
35
|
+
END_RUBY
|
36
|
+
end
|
37
|
+
|
38
|
+
unless (extra = fields.keys - members).empty?
|
39
|
+
fail ArgumentError,
|
40
|
+
"unknown keyword#{'s' if extra.size > 1}: #{extra.join(', ')}"
|
41
|
+
end
|
42
|
+
|
43
|
+
@values = fields
|
44
|
+
@fields = fields
|
45
|
+
end
|
46
|
+
|
47
|
+
attr_reader :fields
|
48
|
+
|
49
|
+
def [](key)
|
50
|
+
fields[key]
|
51
|
+
end
|
52
|
+
|
53
|
+
def each(&block)
|
54
|
+
to_h.each(&block)
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_s
|
58
|
+
"#<ure #{fields.to_s}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def inspect
|
62
|
+
to_s
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_a
|
66
|
+
fields.values
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_h
|
70
|
+
fields.to_h
|
71
|
+
end
|
72
|
+
|
73
|
+
def values
|
74
|
+
fields.values
|
75
|
+
end
|
76
|
+
|
77
|
+
def values_at(name, *args)
|
78
|
+
list = []
|
79
|
+
list << fields[name]
|
80
|
+
args.each do |arg|
|
81
|
+
list << fields[arg]
|
82
|
+
end
|
83
|
+
list
|
84
|
+
end
|
85
|
+
end
|
data/lib/ure/version.rb
ADDED
data/ure-0.0.1.gem
ADDED
Binary file
|
data/ure.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ure/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ure"
|
8
|
+
spec.version = Ure::VERSION
|
9
|
+
spec.authors = ["Clayton Flesher", "James Edward Grey II"]
|
10
|
+
spec.email = ["claytonflesher@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Immutable value objects for ruby that include required keyword arguments.}
|
13
|
+
spec.description = %q{A replacement for Struct that is immutable and has required keyword arguments.}
|
14
|
+
spec.homepage = "https://github.com/Calvyn82/ure"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
else
|
21
|
+
end
|
22
|
+
|
23
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.3.0"
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ure
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Clayton Flesher
|
8
|
+
- James Edward Grey II
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-11-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.10'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.10'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 3.3.0
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 3.3.0
|
56
|
+
description: A replacement for Struct that is immutable and has required keyword arguments.
|
57
|
+
email:
|
58
|
+
- claytonflesher@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/ure.rb
|
74
|
+
- lib/ure/version.rb
|
75
|
+
- ure-0.0.1.gem
|
76
|
+
- ure.gemspec
|
77
|
+
homepage: https://github.com/Calvyn82/ure
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.4.5.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Immutable value objects for ruby that include required keyword arguments.
|
101
|
+
test_files: []
|