molar 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: 64364e74a1242e5e31acda34cdc36c986ec6d4b2
4
+ data.tar.gz: cc83c2322e936e6a1e60aeb5539db49b39fae194
5
+ SHA512:
6
+ metadata.gz: 3203840e7521100be050bcfbd0b9cd18e4084041031c9eb1fbbf5949e05c5b776630b846dfe7e6f05a5f3b70fa60346ecb6671e537edd2474d74106e4c83f111
7
+ data.tar.gz: 5701f1d82e36a823cead2aa9a736c39367ac4fa20555556b7ac857f055054fc53d88647e6dd34e02799668e0d6be5a0e802bc270dcfaf5f9f31f9222295ca592
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/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in molar.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Connor Jacobsen
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,64 @@
1
+ # Molar
2
+
3
+ Molar dynamically defines attribute hash getters and setters for your classes.
4
+
5
+ *Work in progress*
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'molar'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install molar
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ class Jedi
27
+ include Molar
28
+
29
+ def initialize(attributes = {})
30
+ @attributes = attributes
31
+ end
32
+ end
33
+
34
+ jedi = Jedi.new(name: 'Luke Skywalker', master: 'Yoda')
35
+
36
+ jedi.name
37
+ # => 'Luke Skywalker'
38
+
39
+ jedi.master
40
+ # => 'Yoda'
41
+
42
+ jedi.name = 'Anakin'
43
+ jedi.name
44
+ # => 'Anakin'
45
+ ```
46
+
47
+ Molar defines accessors and setters on the fly so as not to incur the `:method_missing` penalty on each access. Currently, only lookups in the `@attributes` instance variable are supported.
48
+
49
+ Currently, mutation coverage is `~77%`, but I haven't yet had a chance to determine how meaningful that is, as this gem uses a couple of items listed under the limitations section for [mutant](https://github.com/mbj/mutant) (which is a great tool, by the way).
50
+
51
+ ## Development
52
+
53
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+
55
+ 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).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/connorjacobsen/molar.
60
+
61
+
62
+ ## License
63
+
64
+ 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,8 @@
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task test: :spec
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'molar'
5
+
6
+ require 'pry'
7
+ Pry.start
8
+
9
+ # Simple debugging class configured with Molar
10
+ class Jedi
11
+ include Molar
12
+
13
+ def initialize(attributes = {})
14
+ @attributes = attributes
15
+ end
16
+ end
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
data/lib/.rspec ADDED
@@ -0,0 +1,5 @@
1
+ --color
2
+ --format progress
3
+ --profile
4
+ --warnings
5
+ --order random
data/lib/.rubocop.yml ADDED
@@ -0,0 +1,3 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'Gemfile'
@@ -0,0 +1,3 @@
1
+ module Molar
2
+ VERSION = '0.1.0'
3
+ end
data/lib/molar.rb ADDED
@@ -0,0 +1,92 @@
1
+ require 'molar/version'
2
+
3
+ # Mixin providing method_missing attribute lookup. Defines appropriate
4
+ # getters and setters on the fly so as not to incur method_missing penalty
5
+ # on each access.
6
+ #
7
+ # Example:
8
+ #
9
+ # class Jedi
10
+ # include Molar
11
+ #
12
+ # def initialize(attributes)
13
+ # @attributes = attributes
14
+ # end
15
+ # end
16
+ #
17
+ # jedi = Jedi.new(name: 'Yoda', title: 'Master')
18
+ # jedi.name
19
+ # # => 'Yoda'
20
+ #
21
+ # jedi.name = 'Luke'
22
+ # jedi.name
23
+ # # => 'Luke'
24
+ #
25
+ # Note:
26
+ # Currently, Molar only performs the lookup in @attributes.
27
+ module Molar
28
+ def self.included(descendant)
29
+ descendant.class_eval do
30
+ alias_method :__method_missing, :method_missing
31
+
32
+ attr_reader :attributes
33
+ private :attributes
34
+
35
+ def method_missing(method, *args, &block)
36
+ action = __attribute?(method) ? :__register : :__method_missing
37
+ send(action, method, *args, &block)
38
+ end
39
+ end
40
+ end
41
+ private_class_method :included
42
+
43
+ def __register(method, *args)
44
+ if __setter?(method)
45
+ attr = __attr_name(method)
46
+ __add_setter(attr)
47
+ send(method, *args)
48
+ else
49
+ __add_getter(method)
50
+ send(method)
51
+ end
52
+ end
53
+ private :__register
54
+
55
+ def __attribute?(name)
56
+ attributes.key?(__attr_name(name))
57
+ end
58
+ private :__attribute?
59
+
60
+ def __setter?(method)
61
+ __attribute?(method) && method.to_s.end_with?('=')
62
+ end
63
+ private :__setter?
64
+
65
+ def __attribute(attribute)
66
+ attributes[attribute]
67
+ end
68
+ private :__attribute
69
+
70
+ def __add_getter(attribute)
71
+ singleton_class.instance_eval do
72
+ define_method(attribute) { __attribute(attribute) }
73
+ end
74
+ end
75
+ private :__add_getter
76
+
77
+ def __add_setter(attribute)
78
+ singleton_class.instance_eval do
79
+ define_method("#{attribute}=") do |value|
80
+ attributes[attribute] = value
81
+ end
82
+ end
83
+ end
84
+ private :__add_setter
85
+
86
+ def __attr_name(attribute)
87
+ str_attr = attribute.to_s
88
+
89
+ str_attr.end_with?('=') ? str_attr[0..-2].to_sym : str_attr.to_sym
90
+ end
91
+ private :__attr_name
92
+ end
data/molar.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'molar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'molar'
8
+ spec.version = Molar::VERSION
9
+ spec.authors = ['Connor Jacobsen']
10
+ spec.email = ['jacobsen.connor@gmail.com']
11
+
12
+ spec.summary = 'Mixin for attribute hash lookups.'
13
+ spec.description = 'Dynamically defines attribute hash accessors and mutators.'
14
+ spec.homepage = 'https://github.com/connorjacobsen/molar'
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', '~> 3.4'
25
+ spec.add_development_dependency 'rspec-its', '~> 1.2'
26
+ spec.add_development_dependency 'pry', '~> 0.10'
27
+ spec.add_development_dependency 'simplecov', '~> 0.10'
28
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: molar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Connor Jacobsen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-14 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: '3.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-its
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ description: Dynamically defines attribute hash accessors and mutators.
98
+ email:
99
+ - jacobsen.connor@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - LICENSE
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - lib/.rspec
113
+ - lib/.rubocop.yml
114
+ - lib/molar.rb
115
+ - lib/molar/version.rb
116
+ - molar.gemspec
117
+ homepage: https://github.com/connorjacobsen/molar
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.4.7
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Mixin for attribute hash lookups.
141
+ test_files: []
142
+ has_rdoc: