contextuable 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f01ddee462e52b3629e919653e26fbc1dbd92464
4
+ data.tar.gz: 96f2d268eaade637bb222cd96e873c870a1d0223
5
+ SHA512:
6
+ metadata.gz: 66e68fc9f7ce8471999c194c89eb199b716eb0decd0e4ad1f1481e23079a28622a7266ff87692445160b11cd1deb2c831dfdac5e143552202d3d54e8eace98e5
7
+ data.tar.gz: 647bcd46a5b40ce789c849189e5ad5906813fe1fcc14cabf2d067ce841042552630206947405f6128beb7ed2e89ec209debd14d7f513d52971be8d942087804a
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
4
+ before_install: gem install bundler -v 1.10.4
@@ -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
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in contextuable.gemspec
4
+ gemspec
5
+ gem 'pry'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Artur Pañach
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,145 @@
1
+ # Contextuable
2
+
3
+ Better Structs for many applications.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'contextuable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install contextuable
20
+
21
+ ## Usage
22
+
23
+ __Extended OpenStruct:__
24
+ ```ruby
25
+ context = Contextuable.new(name: 'John', surname: 'Doe')
26
+ context.name # => 'John'
27
+ context.name? # => true
28
+ context.surname # => 'Doe'
29
+ context.foo? # => false
30
+ context.foo = :bar
31
+ context.foo? # => true
32
+ context.foo # => :bar
33
+ context.to_h # => {:name=>"John", :surname=>"Doe", :foo=>:bar}
34
+ ```
35
+ _more complex example_
36
+ ```ruby
37
+ class Input < Contextuable
38
+ permit :name, :city, :address, :phone_number, :free_text, :country_code,
39
+ :country, :zip, :types
40
+ defaults types: ['lodging']
41
+ aliases :name, :hotel_name
42
+ aliases :phone_number, :telephone
43
+
44
+ def long_name
45
+ [name, address, city].join(', ')
46
+ end
47
+
48
+ def types
49
+ Array.wrap(args[:types])
50
+ end
51
+ end
52
+
53
+ i = Input.new(name: 'Hotel', city: 'Barcelona', address: 'Happy street', not_permitted: 'dangerous')
54
+ i.types
55
+ # => ["lodging"]
56
+ i.long_name
57
+ # => "Hotel,Happy street,Barcelona"
58
+ i.hotel_name
59
+ # => "Hotel"
60
+ i.phone_number?
61
+ # => false
62
+ i.not_permitted
63
+ # => nil
64
+ ```
65
+
66
+ ### Building better Structs
67
+
68
+ **required**
69
+ ```ruby
70
+ class Example < Contextuable
71
+ required :required_arg
72
+ end
73
+
74
+ Example.new(foo: :bar)
75
+ #=> Error Contextuable::RequiredFieldNotPresent
76
+ ```
77
+
78
+ **aliases**
79
+ ```ruby
80
+ class Example < Contextuable
81
+ aliases :hello, :greeting, :welcome
82
+ end
83
+ ex = Example.new(hello: 'Hey!')
84
+ # => #<Example:0x007fd88ba30398 @args={:hello=>"Hey!"}>
85
+ ex.hello
86
+ # => "Hey!"
87
+ ex.greeting
88
+ # => "Hey!"
89
+ ex.welcome
90
+ # => "Hey!"
91
+ ```
92
+
93
+ **defaults**
94
+ ```ruby
95
+ class Example2 < Contextuable
96
+ defaults foo: :bar, bar: :foo
97
+ end
98
+ ex = Example2.new
99
+ ex.foo
100
+ => :bar
101
+ ex.bar
102
+ => :foo
103
+ ex2 = Example2.new(foo: 'something', bar: true)
104
+ ex2.foo
105
+ => 'something'
106
+ ex2.bar
107
+ => true
108
+ ```
109
+
110
+ **ensure_presence**
111
+ ```ruby
112
+ class EnsurePresence < Contextuable
113
+ ensure_presence :foo
114
+ end
115
+ EnsurePresence.new(hello: 'asdf')
116
+ #=> Error: Contextuable::PresenceRequired
117
+
118
+ EnsurePresence.new(foo: nil)
119
+ #=> Error: Contextuable::PresenceRequired
120
+
121
+ EnsurePresence.new(foo: '').foo #=> ""
122
+ ```
123
+
124
+ **permit**
125
+ ```ruby
126
+ per = Permit.new(foo: :bar, hello: 'Hey!', bar: 'bla', yuju: 'dangerous')
127
+ => #<Permit:0x007fd88b9dd878 @args={:foo=>:bar, :hello=>"Hey!"}>
128
+ per.foo #=> :bar
129
+ per.yuju #=> nil
130
+ ```
131
+
132
+ ## Development
133
+
134
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
135
+
136
+ 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).
137
+
138
+ ## Contributing
139
+
140
+ Bug reports and pull requests are welcome on GitHub at https://github.com/arturictus/contextuable. 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.
141
+
142
+
143
+ ## License
144
+
145
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "contextuable"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -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 'contextuable'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "contextuable"
8
+ spec.version = Contextuable::VERSION
9
+ spec.authors = ["Artur Pañach"]
10
+ spec.email = ["arturictus@gmail.com"]
11
+
12
+ spec.summary = %q{Structs with steroids.}
13
+ spec.description = %q{Better way to improve your data structs.}
14
+ spec.homepage = "https://www.github.com/arturictus/contextuable"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,124 @@
1
+ # require 'forwardable'
2
+ class Contextuable
3
+ # extend Forwardable
4
+ # delegate :[], to: :args
5
+
6
+ VERSION = "0.1.0"
7
+ class RequiredFieldNotPresent < ArgumentError; end
8
+ class PresenceRequired < ArgumentError; end
9
+ class << self
10
+ def required(*names)
11
+ @_required = names.map(&:to_sym)
12
+ end
13
+
14
+ def ensure_presence(*names)
15
+ @_presence_required = names.map(&:to_sym)
16
+ end
17
+
18
+ def aliases(*names)
19
+ @_equivalents ||= []
20
+ @_equivalents << names.map(&:to_sym)
21
+ end
22
+
23
+ def defaults(hash)
24
+ @_defaults = hash
25
+ end
26
+
27
+ def permit(*names)
28
+ @_permitted = names.map(&:to_sym)
29
+ end
30
+ end
31
+
32
+ attr_reader :args
33
+ alias_method :to_h, :args
34
+ alias_method :to_hash, :args
35
+
36
+ def initialize(hash = {})
37
+ fail ArgumentError unless hash.respond_to?(:fetch)
38
+ fail RequiredFieldNotPresent unless _required_args.map(&:to_sym).all? { |r| hash.keys.map(&:to_sym).include?(r) }
39
+ fail PresenceRequired if _presence_required.map(&:to_sym).any? { |r| hash[r].nil? }
40
+ hash = hash.select{|k, v| _permitted.include?(k.to_sym) } if _only_permitted?
41
+ @args = _defaults.merge(hash)
42
+ args.each do |k, v|
43
+ define_special_method(k, v)
44
+ end
45
+ end
46
+
47
+ def [](key)
48
+ args[key]
49
+ end
50
+
51
+ def []=(key, value)
52
+ set_attribute(key, value)
53
+ end
54
+
55
+ def method_missing(name, *args, &block)
56
+ if ary = find_in_equivalents(name)
57
+ _from_equivalents(ary)
58
+ elsif name =~ /\A\w+=\z/
59
+ value = args.first || block
60
+ key = name.to_s.gsub('=', '').to_sym
61
+ set_attribute(key, value)
62
+ else
63
+ # if name.to_s =~ /\anot_.?\z/
64
+ name.to_s.include?('?') ? false : nil
65
+ # else
66
+ # end
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def set_attribute(key, value)
73
+ args[key] = value
74
+ define_special_method(key, value)
75
+ end
76
+
77
+ def define_special_method(key, value)
78
+ define_singleton_method(key) { args.fetch(key) }
79
+ define_singleton_method("#{key}?") { true }
80
+ define_singleton_method("not_#{key}?") { false }
81
+ end
82
+
83
+ def find_in_equivalents(name)
84
+ found = nil
85
+ _equivalents.each do |ary|
86
+ found = ary if ary.include?(name.to_sym)
87
+ break if found
88
+ end
89
+ found
90
+ end
91
+
92
+ def _from_equivalents(ary)
93
+ out = nil
94
+ ary.each do |method|
95
+ out = args[method.to_sym]
96
+ break if out
97
+ end
98
+ out
99
+ end
100
+
101
+ def _only_permitted?
102
+ _permitted.any?
103
+ end
104
+
105
+ def _permitted
106
+ self.class.instance_variable_get(:@_permitted) || []
107
+ end
108
+
109
+ def _equivalents
110
+ self.class.instance_variable_get(:@_equivalents) || []
111
+ end
112
+
113
+ def _presence_required
114
+ self.class.instance_variable_get(:@_presence_required) || []
115
+ end
116
+
117
+ def _defaults
118
+ self.class.instance_variable_get(:@_defaults) || {}
119
+ end
120
+
121
+ def _required_args
122
+ self.class.instance_variable_get(:@_required) || []
123
+ end
124
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: contextuable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Artur Pañach
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Better way to improve your data structs.
56
+ email:
57
+ - arturictus@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - contextuable.gemspec
73
+ - lib/contextuable.rb
74
+ homepage: https://www.github.com/arturictus/contextuable
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.6
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Structs with steroids.
98
+ test_files: []