bb_openstruct 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5be3f4893038ef571505c8c6ee7f5bcff485d035
4
+ data.tar.gz: ddb388360d954e595e16bd044d6fd404a440dc2d
5
+ SHA512:
6
+ metadata.gz: c2f8f8f40f0c99e1e77fac47b2e08794b4d44a59b80fb58683e97621617f23b06c383797fb1b131e6b35c289c745f7e2c80028cfa7ffc2a7b942129889a999ee
7
+ data.tar.gz: dd0ac4417e4ac79b84b858c9b68c24ae7231953a246b1847040224630bda29c9a3f1d65edf664c154660b8140094f8940ebf3abfcee19b32e9f6b90211ec00df
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/
@@ -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 p.szmielew@ava.waw.pl. 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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bb_openstruct.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Piotr Szmielew
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,30 @@
1
+ # BBOpenstruct
2
+
3
+ This gem contains BbOpenStruct class - reimplementation of OpenStruct that uses binding to store your data internally. Why? For science. You monster.
4
+
5
+ Test file is actually taken from original openstruct implementation, so API is nearly identical.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bb_openstruct'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bb_openstruct
22
+
23
+ ## Should I use it on production
24
+
25
+ Well... I don't really know what can go wrong, however I'm not sure it's very good idea.
26
+
27
+ ## License
28
+
29
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
30
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :test
@@ -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 'bb_openstruct/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bb_openstruct"
8
+ spec.version = BBOpenstruct::VERSION
9
+ spec.authors = ["Piotr Szmielew"]
10
+ spec.email = ["p.szmielew@ava.waw.pl"]
11
+
12
+ spec.summary = %q{Reimplementation of OpenStruct that uses binding object as data store}
13
+ spec.homepage = "http://piotr.szmielew.pl"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
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.13"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bb_openstruct"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,166 @@
1
+ # encoding: UTF-8
2
+ require "bb_openstruct/version"
3
+
4
+ class BBOpenStruct
5
+ class << self # :nodoc:
6
+ alias allocate new
7
+ end
8
+
9
+ def initialize(hash={})
10
+ @binding = BBOpenStruct.pure_binding
11
+ hash.each_pair do |k,v|
12
+ @binding.local_variable_set(k.to_sym, v)
13
+ end
14
+ @binding.local_variables.each do |name|
15
+ get = ->() { @binding.local_variable_get(name) }
16
+ set = ->(new_var) { @binding.local_variable_set(name, new_var) }
17
+ define_singleton_method(name, get)
18
+ define_singleton_method("#{name}=".to_sym, set)
19
+ end
20
+ end
21
+
22
+ def self.pure_binding
23
+ binding
24
+ end
25
+
26
+ def delete_field(name)
27
+ name = name.to_sym
28
+ binding_old = @binding.dup
29
+ @binding = BBOpenStruct.pure_binding
30
+ binding_old.local_variables.each do |var|
31
+ next if var == name
32
+ @binding.local_variable_set(var, binding_old.local_variable_get(var))
33
+ end
34
+
35
+ instance_eval("undef #{name}=")
36
+ instance_eval("undef #{name}")
37
+
38
+ binding_old.local_variable_get(name)
39
+ end
40
+
41
+ def freeze
42
+ singleton_methods.each do |method|
43
+ instance_eval("undef #{method}") if method.to_s[-1] == '='
44
+ end
45
+ eval("def self.frozen?; true end")
46
+ super
47
+ end
48
+
49
+ def method_missing(name, arg=nil)
50
+ name = name.to_sym
51
+ if @binding.local_variables.include?(name)
52
+ @binding.local_variable_get(name)
53
+ elsif name.to_s[-1] == '=' && frozen?
54
+ raise RuntimeError.new "can't modify frozen object"
55
+ elsif name.to_s[-1] == '='
56
+ pure_name = name.to_s.delete('=').to_sym
57
+ @binding.local_variable_set(pure_name, arg)
58
+ unless methods.include?(pure_name)
59
+ set = ->(new_var) { @binding.local_variable_set(pure_name, new_var) }
60
+ get = ->() { @binding.local_variable_get(pure_name) }
61
+ define_singleton_method(name, set)
62
+ define_singleton_method(pure_name, get)
63
+ end
64
+ else
65
+ err = NoMethodError.new "undefined method `#{name}' for #{self}", name, [arg]
66
+ err.set_backtrace caller(1)
67
+ raise err
68
+ end
69
+ end
70
+
71
+ def [](name)
72
+ method_missing(name)
73
+ end
74
+
75
+ def []=(name, val)
76
+ method_missing("#{name.to_sym}=", val)
77
+ end
78
+
79
+ def respond_to_missing?(method_name, include_private = false)
80
+ last_char_name = method_name.to_s[-1]
81
+ if last_char_name == '=' && frozen?
82
+ false
83
+ elsif last_char_name == '=' && @binding.local_variable_defined?(method_name.to_s.tr('=', ''))
84
+ true
85
+ else
86
+ (@binding.local_variables.include?(method_name) && !@binding.local_variable_get(method_name)) || super
87
+ end
88
+ end
89
+
90
+ InspectKey = :__inspect_key__ # :nodoc:
91
+ def inspect
92
+ str = "#<#{self.class}"
93
+
94
+ ids = (Thread.current[InspectKey] ||= [])
95
+ if ids.include?(object_id)
96
+ return str << ' ...>'
97
+ end
98
+
99
+ ids << object_id
100
+ begin
101
+ first = true
102
+ @binding.local_variables.sort.each do |k|
103
+ v = @binding.local_variable_get(k)
104
+ str << "," unless first
105
+ first = false
106
+ str << " #{k}=#{v.inspect}"
107
+ end
108
+ return str << '>'
109
+ ensure
110
+ ids.pop
111
+ end
112
+ end
113
+ alias :to_s :inspect
114
+
115
+ def each_pair
116
+ table = {}
117
+ @binding.local_variables.each do |k|
118
+ table[k] = @binding.local_variable_get(k)
119
+ end
120
+ return to_enum(__method__) { table.size } unless block_given?
121
+ table.each_pair{|p| yield p}
122
+ end
123
+
124
+ def to_h
125
+ output = {}
126
+ @binding.local_variables.each do |var|
127
+ next if var == :hash
128
+ output[var] = @binding.local_variable_get(var)
129
+ end
130
+ output
131
+ end
132
+
133
+ def ==(other)
134
+ return false unless other.kind_of?(BBOpenStruct)
135
+ to_h == other.to_h
136
+ end
137
+
138
+ def eql?(other)
139
+ return false unless other.kind_of?(BBOpenStruct)
140
+ to_h.eql?(other.to_h)
141
+ end
142
+
143
+ # Compute a hash-code for this OpenStruct.
144
+ # Two hashes with the same content will have the same hash code
145
+ # (and will be eql?).
146
+ def hash
147
+ to_h.hash
148
+ end
149
+
150
+ # Retrieves the value object corresponding to the each +name+
151
+ # objects repeatedly.
152
+ #
153
+ # address = BBOpenStruct.new('city' => "Anytown NC", 'zip' => 12345)
154
+ # person = BBOpenStruct.new('name' => 'John Smith', 'address' => address)
155
+ # person.dig(:address, 'zip') # => 12345
156
+ # person.dig(:business_address, 'zip') # => nil
157
+ #
158
+ def dig(name, *names)
159
+ begin
160
+ name = name.to_sym
161
+ rescue NoMethodError
162
+ raise TypeError, "#{name} is not a symbol nor a string"
163
+ end
164
+ to_h.dig(name, *names)
165
+ end
166
+ end
@@ -0,0 +1,3 @@
1
+ module BBOpenstruct
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bb_openstruct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Szmielew
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-17 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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
+ description:
42
+ email:
43
+ - p.szmielew@ava.waw.pl
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - CODE_OF_CONDUCT.md
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bb_openstruct.gemspec
55
+ - bin/console
56
+ - bin/setup
57
+ - lib/bb_openstruct.rb
58
+ - lib/bb_openstruct/version.rb
59
+ homepage: http://piotr.szmielew.pl
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.6.8
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Reimplementation of OpenStruct that uses binding object as data store
83
+ test_files: []