rstructural 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eaeeb6fd3715a08c65d789c7c7566bd01cdf85889c2f75e3680c147899d1f35c
4
+ data.tar.gz: 77219c7fdcba9a170bb8e89150ea15b52fed139132c856503a2209cfb1356fd6
5
+ SHA512:
6
+ metadata.gz: 5a81b38f7fa790f0a11b96c8d5dd0b85d8a30a812c34948c46027c75b5a8109b45411a636a708d2b36b8bb19e9271e48c923134199ca02776bd2eb5248d43435
7
+ data.tar.gz: 77fe704bffc450ec21394ad2f098a5f6911b5132546c55d60e390bd803764930485679bf743bef918464981efa133e16f7c8fc473d578a1e225bcba00629b6ab
@@ -0,0 +1 @@
1
+ 2.7.0
@@ -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.2
@@ -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 violethero0820@gmail.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 rstructural.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rstructural (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (11.0.1)
10
+ minitest (5.13.0)
11
+ rake (12.3.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ byebug
18
+ minitest (~> 5.0)
19
+ rake (~> 12.0)
20
+ rstructural!
21
+
22
+ BUNDLED WITH
23
+ 2.1.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 petitviolet
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.
@@ -0,0 +1,141 @@
1
+ # Ruby structual types
2
+
3
+ - Rstruct
4
+ - Struct implemented with Ruby
5
+ - Enum
6
+ - A set of constants
7
+ - Algebraic Data Type(ADT)
8
+ - A set of objects
9
+ - A object is a constant or a set of objects
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'rstruct'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install rstruct
26
+
27
+ ## Usage
28
+
29
+ ### Rstruct
30
+
31
+ ```ruby
32
+ module RstructSample
33
+ # define a struct type
34
+ Value = Rstruct.new(:value)
35
+
36
+ puts Value.name #=> 'RstructSample::Value'
37
+ puts value = Value.new(100) #=> 'RstructSample::Value(value: 100)
38
+ puts value == Value.new(100) #=> true
39
+ puts value.value == 100 #=> true
40
+ case value
41
+ in Value[n] # pattern match (Ruby 2.7~)
42
+ puts "here! value: #{n}" #=> 'here! value: 100'
43
+ else
44
+ raise
45
+ end
46
+ end
47
+ ```
48
+
49
+ ### Enum
50
+
51
+ ```ruby
52
+ module EnumSample
53
+ # define enum
54
+ module Status
55
+ extend Enum
56
+
57
+ # method(:enum) creates a enum value
58
+ OK = enum 200
59
+ NotFound = enum 404
60
+ InternalServerError = enum 500 do
61
+ # add block to define custom methods
62
+ def message
63
+ "Something wrong"
64
+ end
65
+ end
66
+ end
67
+
68
+ puts Status::OK #=> EnumSample::Status::OK(value: 200)
69
+ puts Status::InternalServerError.message #=> Something wrong
70
+
71
+ # find enum by value
72
+ case Status.of(404)
73
+ in Status::NotFound
74
+ puts "NotFound!!!" #=> NotFound!!!
75
+ else
76
+ raise
77
+ end
78
+ end
79
+ ```
80
+
81
+ ### ADT
82
+
83
+ ```ruby
84
+ module AdtSample
85
+ # define ADT
86
+ module Shape
87
+ extend ADT
88
+
89
+ # define a constant object
90
+ Point = const
91
+
92
+ # define a object with 1 attribute
93
+ Circle = data :radius do |mod|
94
+ def scale(i)
95
+ Circle.new(radius * i)
96
+ end
97
+ def area
98
+ 3.14 * radius * radius
99
+ end
100
+ end
101
+ # define a object with 2 attributes
102
+ Rectangle = data :width, :height do |mod|
103
+ def area
104
+ width * height
105
+ end
106
+ end
107
+
108
+ end
109
+
110
+ puts Shape::Point #=> AdtSample::Shape::Point
111
+ puts Shape::Rectangle.new(3, 4) #=> AdtSample::Shape::Rectangle(width: 3, height: 4)
112
+ puts Shape::Rectangle.new(3, 4).area #=> 12
113
+ puts Shape::Circle.new(5).scale(2).area #=> 314.0
114
+
115
+ case Shape::Rectangle.new(1, 2)
116
+ in Shape::Rectangle[Integer => i, Integer => j] if j % 2 == 0
117
+ puts "here! rectangle #{i}, #{j}" #=> here! rectangle 1, 2
118
+ else
119
+ raise
120
+ end
121
+ end
122
+ ```
123
+
124
+ ## Development
125
+
126
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
127
+
128
+ 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).
129
+
130
+ ## Contributing
131
+
132
+ Bug reports and pull requests are welcome on GitHub at https://github.com/petitviolet/rstruct. 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/petitviolet/rstruct/blob/master/CODE_OF_CONDUCT.md).
133
+
134
+
135
+ ## License
136
+
137
+ The gem is available as open source under the terms of the [MIT License](https://petitviolet.mit-license.org/).
138
+
139
+ ## Code of Conduct
140
+
141
+ Everyone interacting in the Rstruct project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/petitviolet/rstruct/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rstruct"
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__)
@@ -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,11 @@
1
+ require_relative './rstructural/struct'
2
+ require_relative './rstructural/enum'
3
+ require_relative './rstructural/adt'
4
+
5
+ module Rstructural
6
+ end
7
+
8
+ # alias
9
+ Rstruct = Rstructural::Struct
10
+ Enum = Rstructural::Enum
11
+ ADT = Rstructural::ADT
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './struct'
4
+
5
+ module Rstructural::ADT
6
+ def self.extended(klass)
7
+ @@adt_types ||= []
8
+ end
9
+
10
+ def const(value = nil, &block)
11
+ if value
12
+ Rstructural::Struct.new(:value, __caller: caller, &block).new(value)
13
+ else
14
+ Rstructural::Struct.new(__caller: caller, &block).new
15
+ end.tap do |k|
16
+ @@adt_types << k
17
+ def k.name
18
+ self.class.name
19
+ end
20
+ end
21
+ end
22
+
23
+ def data(*fields, &block)
24
+ Rstructural::Struct.new(*fields, __caller: caller, &block).tap { |k| @@adt_types << k.name }
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './struct'
4
+
5
+ module Rstructural::Enum
6
+ def self.extended(mod)
7
+ @@enum_values ||= []
8
+ end
9
+
10
+ def enum(value, &block)
11
+ if (type = of(value))
12
+ raise ArgumentError, "Enum '#{value}' already defined in #{type.name}"
13
+ end
14
+ Rstructural::Struct.new(:value, __caller: caller, &block).new(value).tap do |k|
15
+ @@enum_values << k
16
+ def k.name
17
+ self.class.name
18
+ end
19
+ end
20
+ end
21
+
22
+ def of(value)
23
+ @@enum_values.find { |v| v.value == value }
24
+ end
25
+ end
26
+
@@ -0,0 +1,56 @@
1
+ module Rstructural
2
+ module Struct
3
+ def self.new(*attributes, __caller: nil, &block)
4
+ begin
5
+ kaller = __caller || caller
6
+ names = kaller.map do |stack|
7
+ # ".../hoge.rb:7:in `<module:Hoge>'"
8
+ if (m = stack.match(/\A.+in `<(module|class):(.+)>.+/))
9
+ m[2]
10
+ end
11
+ end.reject(&:nil?).reverse
12
+ file_name, line_num = kaller[0].split(':')
13
+ line_executed = File.readlines(file_name)[line_num.to_i - 1]
14
+ names << line_executed.match(/\A\s*(\S+)\s*=/)[1] # " Point = Struct.new(:x, :y)\n"
15
+ class_name = names.join('::')
16
+ rescue StandardError
17
+ class_name = 'Struct'
18
+ end
19
+ Class.new.tap do |k|
20
+ k.class_eval <<~RUBY
21
+ def initialize(#{attributes.join(', ')})
22
+ #{attributes.map { |attr| "@#{attr} = #{attr}" }.join("\n")}
23
+ end
24
+
25
+ #{attributes.map { |attr| "attr_reader(:#{attr})" }.join("\n")}
26
+
27
+ def self.name
28
+ "#{class_name}"
29
+ end
30
+
31
+ def ==(other)
32
+ return false if other.class != self.class
33
+ #{attributes.map { |attr| "other.#{attr} == self.#{attr}" }.join(" && ")}
34
+ end
35
+
36
+ def inspect
37
+ if #{attributes.empty?}
38
+ "#{class_name}"
39
+ else
40
+ __attrs = Array[#{attributes.map { |attr| "'#{attr}: ' + @#{attr}.to_s" }.join(', ')}].join(", ")
41
+ "#{class_name}(" + __attrs + ")"
42
+ end
43
+ end
44
+
45
+ alias :to_s :inspect
46
+
47
+ def deconstruct
48
+ [#{attributes.map { |attr| "@#{attr}" }.join(', ')}]
49
+ end
50
+ RUBY
51
+ k.class_eval(&block) if block
52
+ end
53
+ end
54
+ end
55
+ end
56
+
@@ -0,0 +1,27 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "rstructural"
3
+ spec.version = "0.1.1"
4
+ spec.authors = ["petitviolet"]
5
+ spec.email = ["violethero0820@gmail.com"]
6
+
7
+ spec.summary = %q{A kind of structural types for Ruby}
8
+ spec.description = %q{Structural types, Struct, Enum and ADT for Ruby implemented with Ruby}
9
+ spec.homepage = "https://github.com/petitviolet/rstructural"
10
+ spec.license = "MIT"
11
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
12
+
13
+ spec.metadata["homepage_uri"] = spec.homepage
14
+ spec.metadata["source_code_uri"] = spec.homepage
15
+ spec.metadata["changelog_uri"] = spec.homepage
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match("^(\.git*|sample|test|spec|features)") }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "byebug"
27
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rstructural
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - petitviolet
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Structural types, Struct, Enum and ADT for Ruby implemented with Ruby
28
+ email:
29
+ - violethero0820@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".ruby-version"
35
+ - ".travis.yml"
36
+ - CODE_OF_CONDUCT.md
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - bin/console
43
+ - bin/setup
44
+ - lib/rstructural.rb
45
+ - lib/rstructural/adt.rb
46
+ - lib/rstructural/enum.rb
47
+ - lib/rstructural/struct.rb
48
+ - rstructural.gemspec
49
+ homepage: https://github.com/petitviolet/rstructural
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://github.com/petitviolet/rstructural
54
+ source_code_uri: https://github.com/petitviolet/rstructural
55
+ changelog_uri: https://github.com/petitviolet/rstructural
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.3.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.1.2
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: A kind of structural types for Ruby
75
+ test_files: []