property-accessor 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
+ SHA256:
3
+ metadata.gz: ee7d889a50003bc95523507b5f890335757113e753a1a0eab8fa7af4a09894e3
4
+ data.tar.gz: ad0dbc3ff7db1c41692759a1348e811287ae513b6e17db8c33e11ed9aceccb7c
5
+ SHA512:
6
+ metadata.gz: dd60658a1e4330f1d387d27f7a5ca2805213750529344b22c4683c4dc432de8f3a4567fcf22f2b094c3d7d9118cc38b57901b63db300b12ac3ab4006ee900275
7
+ data.tar.gz: 042660f427218fabd8cbc9c77b7fb1e80dabcdee9b5529512e762bccd7b3ade2a4776aaabd4353c3cb81147909dc4b8c0cf8b0099135d8bdff1b715869bfbe73
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.history
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
@@ -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 evgeny.stepanov@jetruby.com. 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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in property-accessor.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ property-accessor (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (12.3.3)
11
+ rspec (3.9.0)
12
+ rspec-core (~> 3.9.0)
13
+ rspec-expectations (~> 3.9.0)
14
+ rspec-mocks (~> 3.9.0)
15
+ rspec-core (3.9.1)
16
+ rspec-support (~> 3.9.1)
17
+ rspec-expectations (3.9.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.9.0)
20
+ rspec-mocks (3.9.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-support (3.9.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ property-accessor!
30
+ rake (~> 12.0)
31
+ rspec (~> 3.0)
32
+
33
+ BUNDLED WITH
34
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Evgeny Stepanov
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,81 @@
1
+ # property_accessor
2
+
3
+ Ruby syntax sugar for C# style property accessor definition
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'property-accessor'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install property-accessor
20
+
21
+ ## Usage
22
+
23
+ There are two ways of using property accessor.
24
+
25
+ You can inject `property` singleton method directly to `Class` object during your app initialization.
26
+
27
+ ```ruby
28
+ PropertyAccessor::Mixin.inject!
29
+ ```
30
+
31
+ Or you can manually extend your class with `PropertyAccessor` module
32
+
33
+ ```ruby
34
+ require 'date'
35
+
36
+ class Person
37
+ extend PropertyAccessor
38
+
39
+ property(:first_name) do
40
+ get { @name.upcase }
41
+ set { |val| @name = val }
42
+ end
43
+
44
+ property(:last_name) { get; set }
45
+ property(:full_name) { get { "#{first_name} #{last_name}" } }
46
+ property(:birth_date) { get; set { |val| @birth_date = Date.parse(val.to_s) } }
47
+ property(:age) { get { (Time.now - birth_date.to_time).to_i / 60 / 60 / 24 / 365 } }
48
+ property(:meta) { default { {} }; get }
49
+ end
50
+
51
+ p = Person.new
52
+ p.first_name = 'john'
53
+ p.last_name = 'doe'
54
+ p.full_name
55
+ # => 'JOHN doe'
56
+ p.birth_date = "1992-04-12"
57
+ p.age
58
+ # => 27
59
+ p.meta
60
+ # => {}
61
+ ```
62
+
63
+
64
+ ## Development
65
+
66
+ 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.
67
+
68
+ 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).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/estepnv/property-accessor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/estepnv/property-accessor/blob/master/CODE_OF_CONDUCT.md).
73
+
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
78
+
79
+ ## Code of Conduct
80
+
81
+ Everyone interacting in the property_accessor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/estepnv/property-accessor/blob/master/CODE_OF_CONDUCT.md).
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 "property_accessor"
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(__FILE__)
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,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PropertyAccessor
4
+ UndefinedPropertyError = Class.new(StandardError)
5
+
6
+ module ClassMethods
7
+ def property(property_name, &block)
8
+ property = Property.new(property_name)
9
+ builder = PropertyBuilder.new(property)
10
+
11
+ if block.nil?
12
+ raise UndefinedPropertyError, 'cannot define a proprty without specifing at least getter or setter'
13
+ end
14
+
15
+ builder.instance_exec(&block)
16
+
17
+ define_method("initialize_#{property.name}") do
18
+ if !instance_variable_defined?("@#{property.name}")
19
+ instance_variable_set(
20
+ "@#{property.name}",
21
+ property.default_value_proc ?
22
+ instance_exec(&property.default_value_proc) :
23
+ property.default_value
24
+ )
25
+ end
26
+ end
27
+
28
+ private "initialize_#{property.name}"
29
+
30
+ if property.getter_defined?
31
+ define_method(property.name) do
32
+ send("initialize_#{property.name}")
33
+ instance_exec(&property.getter_proc)
34
+ end
35
+ end
36
+
37
+ if property.setter_defined?
38
+ define_method("#{property.name}=", property.setter_proc)
39
+ end
40
+
41
+ if property.default_getter
42
+ define_method(property.name) do
43
+ send("initialize_#{property.name}")
44
+ instance_variable_get("@#{property.name}")
45
+ end
46
+ end
47
+
48
+ if property.default_setter
49
+ attr_writer property.name
50
+ end
51
+
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PropertyAccessor
4
+ class Property
5
+
6
+ attr_accessor :name,
7
+ :value,
8
+ :setter_proc,
9
+ :getter_proc,
10
+ :default_getter,
11
+ :default_setter,
12
+ :default_value,
13
+ :default_value_proc
14
+
15
+ def initialize(name)
16
+ @name = name
17
+ @setter_proc = nil
18
+ @getter_proc = nil
19
+ @default_getter = false
20
+ @default_setter = false
21
+ @default_value = nil
22
+ @default_value_proc = nil
23
+ @value = nil
24
+ end
25
+
26
+ def setter_defined?
27
+ !@setter_proc.nil?
28
+ end
29
+
30
+ def getter_defined?
31
+ !@getter_proc.nil?
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PropertyAccessor
4
+ class PropertyBuilder
5
+
6
+ # @param property [PropertyAccessor::Property]
7
+ def initialize(property)
8
+ @property = property
9
+ end
10
+
11
+ def default(value = nil, &block)
12
+ if value
13
+ @property.default_value = value
14
+ else
15
+ @property.default_value_proc = block
16
+ end
17
+ end
18
+
19
+ def get(&block)
20
+ if block
21
+ @property.getter_proc = block
22
+ else
23
+ @property.default_getter = true
24
+ end
25
+ end
26
+
27
+ def set(&block)
28
+ if block
29
+ @property.setter_proc = block
30
+ else
31
+ @property.default_setter = true
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module PropertyAccessor
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'property_accessor/class_methods.rb'
2
+ require 'property_accessor/property_builder.rb'
3
+ require 'property_accessor/property.rb'
4
+
5
+ module PropertyAccessor
6
+ include ClassMethods
7
+
8
+ module Mixin
9
+ def self.inject!
10
+ Class.include(ClassMethods)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'lib/property_accessor/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "property-accessor"
5
+ spec.version = PropertyAccessor::VERSION
6
+ spec.authors = ["Evgeny Stepanov"]
7
+ spec.email = ["estepnv@icloud.com"]
8
+
9
+ spec.summary = "Ruby syntax sugar for C# style property accessor definition"
10
+ spec.homepage = "https://github.com/estepnv/property-accessor"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/estepnv/property-accessor"
16
+ spec.metadata["changelog_uri"] = "https://github.com/estepnv/property-accessor"
17
+
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{(test|spec|features|git|rspec|travis)/}) }
20
+ end
21
+
22
+ spec.bindir = "bin"
23
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: property-accessor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Evgeny Stepanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - estepnv@icloud.com
16
+ executables:
17
+ - console
18
+ - setup
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".gitignore"
23
+ - ".rspec"
24
+ - ".travis.yml"
25
+ - CODE_OF_CONDUCT.md
26
+ - Gemfile
27
+ - Gemfile.lock
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - bin/console
32
+ - bin/setup
33
+ - lib/property_accessor.rb
34
+ - lib/property_accessor/class_methods.rb
35
+ - lib/property_accessor/property.rb
36
+ - lib/property_accessor/property_builder.rb
37
+ - lib/property_accessor/version.rb
38
+ - property-accessor.gemspec
39
+ homepage: https://github.com/estepnv/property-accessor
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://github.com/estepnv/property-accessor
44
+ source_code_uri: https://github.com/estepnv/property-accessor
45
+ changelog_uri: https://github.com/estepnv/property-accessor
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.1.2
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Ruby syntax sugar for C# style property accessor definition
65
+ test_files: []