flex_struct 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: caf46503e9a8152fa8f0e263643c8be5900ac8db
4
+ data.tar.gz: 1a5aefee1703e619e88c2fb4f0e618fcb832b98d
5
+ SHA512:
6
+ metadata.gz: 8b0662189bbffdf683097d768121da164e5cbea58ad73f6866e7678a17d4dc0897fa94ee5df8039d645e1f2d59bea633ee7b0c366b95251bd2b34a8e6302e494
7
+ data.tar.gz: 1414dbe98a5a8e7fd03851ae85dbbc683e264bb0bf1757be5e3731877198f657850dcc5bf71ed04a3ab6e9a09dc94a0dc1327c7ed7f4b73ca15d886345af863d
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,70 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+
5
+ # Relates to the indentation of if/when/(etc.) blocks when the initial keyword
6
+ # is indented as part of a variable assignment.
7
+ #
8
+ # Our preference is that the rubocop default conflicts with our 80-character
9
+ # line restriction, leaving less space for code inside nested blocks. We make
10
+ # this change at the cost of a little bit of readability.
11
+ #
12
+ # # bad (although the Rubocop default):
13
+ # thing = if x > 42
14
+ # thing!
15
+ # else
16
+ # other_thing!
17
+ # end
18
+ #
19
+ # # good (preferred):
20
+ # thing = if x > 42
21
+ # thing!
22
+ # else
23
+ # other_thing!
24
+ # end
25
+ #
26
+ # # also acceptable
27
+ # thing =
28
+ # if x > 42
29
+ # thing!
30
+ # else
31
+ # other_thing!
32
+ # end
33
+ Lint/EndAlignment:
34
+ EnforcedStyleAlignWith: variable
35
+ StyleGuide: https://github.com/BridgeU/bridgeu/commit/d19e558ec0246452a2962dcd6e6e5a915c1a8a6b
36
+
37
+ Metrics/BlockLength:
38
+ Exclude:
39
+ - 'spec/**/*.rb'
40
+
41
+ # Passing multiple hashes to a function is messy, and clarity is good
42
+ # This is an extension of https://github.com/bbatsov/ruby-style-guide#no-braces-opts-hash
43
+ Style/BracesAroundHashParameters:
44
+ EnforcedStyle: context_dependent
45
+ StyleGuide: https://github.com/bbatsov/rubocop/issues/801
46
+
47
+ # This is one part of the style guide that doesn't express a preference, it just
48
+ # suggests you make a choice
49
+ Style/DotPosition:
50
+ EnforcedStyle: trailing
51
+
52
+ Style/PercentLiteralDelimiters:
53
+ # Enforce Ruby style guide percent-delimiters
54
+ # Redundant with Rubocop 0.48.1
55
+ PreferredDelimiters:
56
+ default: ()
57
+ '%i': '[]'
58
+ '%I': '[]'
59
+ '%r': '{}'
60
+ '%w': '[]'
61
+ '%W': '[]'
62
+
63
+ Style/StringLiterals:
64
+ EnforcedStyle: double_quotes
65
+ StyleGuide: https://www.viget.com/articles/just-use-double-quoted-ruby-strings
66
+
67
+ Style/PredicateName:
68
+ NamePrefixBlacklist:
69
+ - is_
70
+ - have_
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.15.3
@@ -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 TODO: Write your email address. 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
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ # Gems which aren't development dependencies, but are useful for development
6
+ gem "guard-bundler"
7
+ gem "guard-rspec"
data/Guardfile ADDED
@@ -0,0 +1,26 @@
1
+ guard :bundler do
2
+ require "guard/bundler"
3
+ require "guard/bundler/verify"
4
+ helper = Guard::Bundler::Verify.new
5
+
6
+ files = ["Gemfile"]
7
+ files += Dir["*.gemspec"] if files.any? { |f| helper.uses_gemspec?(f) }
8
+
9
+ # Assume files are symlinked from somewhere
10
+ files.each { |file| watch(helper.real_path(file)) }
11
+ end
12
+
13
+ guard :rspec, cmd: "bundle exec rspec" do
14
+ require "guard/rspec/dsl"
15
+ dsl = Guard::RSpec::Dsl.new(self)
16
+
17
+ # RSpec files
18
+ rspec = dsl.rspec
19
+ watch(rspec.spec_helper) { rspec.spec_dir }
20
+ watch(rspec.spec_support) { rspec.spec_dir }
21
+ watch(rspec.spec_files)
22
+
23
+ # Ruby files
24
+ ruby = dsl.ruby
25
+ dsl.watch_spec_files_for(ruby.lib_files)
26
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Gareth Adams
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,121 @@
1
+ # FlexStruct
2
+
3
+ A drop-in replacement for Struct which adds a more flexible initialize method
4
+
5
+ ## Usage
6
+
7
+ Create a `FlexStruct` very similarly to how you'd use any other `Struct`:
8
+
9
+ Pet = FlexStruct.new(:species, :name, :colour)
10
+
11
+ However, `FlexStruct`-created classes can be initialized in more readable ways than
12
+ default `Struct`s:
13
+
14
+ # Your only option with a Struct, positional arguments that require you to
15
+ # know which order the parameters were defined
16
+ floppy = Pet.new(:rabbit, "Floppy", :brown)
17
+
18
+ # Use hash/keyword arguments
19
+ fido = Pet.new(name: "Fido", species: :dog)
20
+
21
+ # Use block initialization
22
+ marmaduke = Pet.new do |pet|
23
+ pet.name = "Marmaduke"
24
+ pet.species = :cat
25
+ pet.colour = :tortoiseshell
26
+ end
27
+
28
+ ## Why?
29
+
30
+ `Hash`es are great. You can use them to store arbitrary key/value pairs using
31
+ any object as a key (although commonly people use `Symbol`s).
32
+
33
+ However, `Hash`es are (by design) quite limited. Data is accessed using a small
34
+ subset of method calls, so if you're passing data around your Ruby program in a
35
+ `Hash`, every method using that data needs to know it's in a `Hash`(like) object.
36
+
37
+ `OpenStruct`s are great. They behave a lot like a `Hash` that only has `Symbol`
38
+ keys - you can set arbitrary values against any named property. You also get to
39
+ get and set those values using regular Ruby methods, which means an `OpenStruct`
40
+ can be often used as a substitute for any other Ruby object, thanks to duck
41
+ typing. And to show how like-a-`Hash` it is, you can pass a `Hash` into its
42
+ initializer and have all of the corresponding properties set.
43
+
44
+ However, an `OpenStruct` will (by design) cheerfully respond `nil` to any
45
+ property it doesn't explicitly know about, and let you set values for any
46
+ property you like. While this is good in a lot of cases, you sometimes want
47
+ data structures that (like most Ruby objects) have a fixed API. `OpenStruct`
48
+ also has performance issues if you're going to create large numbers of them.
49
+
50
+ `Struct`s are great. They're the most lightweight way to create a class with
51
+ fixed attributes - you can create a data structure without having to worry about
52
+ a lot of the `nil` issues that can crop up with the dynamic structures above.
53
+
54
+ However, once you've created a `Struct` class, the initialize method can only be
55
+ called using positional arguments - to initialize a `Struct` class with data, you
56
+ need to know the order its properties were initialized with. That's not
57
+ convenient or readable.
58
+
59
+ ## Yes, but why?
60
+
61
+ `Struct`s are really useful classes that often make more sense than `Hash`es or
62
+ `OpenStruct`s for a lot of their common use cases. But their inflexible
63
+ initializers make them a pain to use.
64
+
65
+ It's really easy to extend a Ruby `Struct` with new methods, including a better
66
+ initializer, but the amount of boilerplate required turns what would otherwise
67
+ be a one-liner class definition into a much heavier ugly addition.
68
+
69
+ `FlexStruct` just creates regular `Struct`-based classes with this smarter
70
+ initializer. The entire useful code of this gem is 14 lines, significantly
71
+ smaller than even this section of the README!
72
+
73
+ But to get people using data structures that are more useful, descriptive, and
74
+ lightweight, it should be as easy as possible to create them. Hence:
75
+ `FlexStruct`!
76
+
77
+ ## Installation
78
+
79
+ Add this line to your application's Gemfile:
80
+
81
+ ```ruby
82
+ gem 'flex_struct'
83
+ ```
84
+
85
+ And then execute:
86
+
87
+ $ bundle
88
+
89
+ Or install it yourself as:
90
+
91
+ $ gem install flex_struct
92
+
93
+ ## Development
94
+
95
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
96
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
97
+ prompt that will allow you to experiment.
98
+
99
+ To install this gem onto your local machine, run `bundle exec rake install`. To
100
+ release a new version, update the version number in `version.rb`, and then run
101
+ `bundle exec rake release`, which will create a git tag for the version, push
102
+ git commits and tags, and push the `.gem` file to
103
+ [rubygems.org](https://rubygems.org).
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at
108
+ https://github.com/gareth/flex_struct. This project is intended to be a safe,
109
+ welcoming space for collaboration, and contributors are expected to adhere to
110
+ the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
111
+
112
+ ## License
113
+
114
+ The gem is available as open source under the terms of the [MIT
115
+ License](http://opensource.org/licenses/MIT).
116
+
117
+ ## Code of Conduct
118
+
119
+ Everyone interacting in the FlexStruct project’s codebases, issue trackers, chat
120
+ rooms and mailing lists is expected to follow the [code of
121
+ conduct](https://github.com/gareth/flex_struct/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require "reek/rake/task"
3
+ require "rspec/core/rake_task"
4
+ require "rubocop/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ RuboCop::RakeTask.new
8
+ Reek::Rake::Task.new do |t|
9
+ t.fail_on_error = false
10
+ end
11
+
12
+ task default: %i[spec rubocop reek]
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "flex_struct"
5
+
6
+ require "pry"
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "flex_struct"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "flex_struct"
7
+ spec.version = FlexStruct::VERSION
8
+ spec.authors = ["Gareth Adams"]
9
+ spec.email = ["gareth@bridge-u.com"]
10
+
11
+ spec.summary = %(An extension to Struct with a more flexible initializer)
12
+ spec.homepage = "https://github.com/bridgeu/flex_struct"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.15"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rubocop", "~> 0.48.1"
26
+ spec.add_development_dependency "reek", "~> 4.0"
27
+ end
@@ -0,0 +1,3 @@
1
+ class FlexStruct
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,14 @@
1
+ # A drop-in replacement for Struct which adds a more flexible initialize method
2
+ class FlexStruct
3
+ autoload :VERSION, "flex_struct/version"
4
+
5
+ def self.new(*args)
6
+ Struct.new(*args) do
7
+ def initialize(*args, **kwargs)
8
+ super(*args)
9
+ kwargs.each { |key, val| self[key] = val }
10
+ yield self if block_given?
11
+ end
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flex_struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gareth Adams
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-11 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.48.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.48.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: reek
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ description:
70
+ email:
71
+ - gareth@bridge-u.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - Guardfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - flex_struct.gemspec
89
+ - lib/flex_struct.rb
90
+ - lib/flex_struct/version.rb
91
+ homepage: https://github.com/bridgeu/flex_struct
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: An extension to Struct with a more flexible initializer
115
+ test_files: []