hefted 0.3.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: 5a8b29e7dba7be574961c7808e210282116f3bda
4
+ data.tar.gz: 2ccc73b9ab5110132910841053166b8cf3ef3d65
5
+ SHA512:
6
+ metadata.gz: 38f865a2c7818644170f06400d850c4e29de74c86f8ba40b616b62a62f293d2499fa32b229371523fc66a65f071a1d153ce160d6e9e6038cf322cac854f3b7e4
7
+ data.tar.gz: 0f277061fca3371951e5f7398bc48af287a569e81ea6a5b7aa1cf47c31a02f0a94738a3679adc6c15b02f40e40225a87e6436523fb7039a7cf0400dc411a13ea
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,122 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.3
5
+ # RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
6
+ # to ignore them, so only the ones explicitly set in this file are enabled.
7
+ DisabledByDefault: true
8
+ Exclude:
9
+ - '**/templates/**/*'
10
+ - '**/vendor/**/*'
11
+
12
+ # Prefer &&/|| over and/or.
13
+ Style/AndOr:
14
+ Enabled: true
15
+
16
+ # Do not use braces for hash literals when they are the last argument of a
17
+ # method call.
18
+ Style/BracesAroundHashParameters:
19
+ Enabled: true
20
+
21
+ # Align `when` with `case`.
22
+ Style/CaseIndentation:
23
+ Enabled: true
24
+
25
+ # Align comments with method definitions.
26
+ Style/CommentIndentation:
27
+ Enabled: true
28
+
29
+ # No extra empty lines.
30
+ Style/EmptyLines:
31
+ Enabled: true
32
+
33
+ # In a regular class definition, no empty lines around the body.
34
+ Style/EmptyLinesAroundClassBody:
35
+ Enabled: true
36
+
37
+ # In a regular module definition, no empty lines around the body.
38
+ Style/EmptyLinesAroundModuleBody:
39
+ Enabled: true
40
+
41
+ # Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
42
+ Style/HashSyntax:
43
+ Enabled: true
44
+ EnforcedStyle: ruby19
45
+
46
+ # Method definitions after `private` or `protected` isolated calls need one
47
+ # extra level of indentation.
48
+ Style/IndentationConsistency:
49
+ Enabled: true
50
+ EnforcedStyle: rails
51
+
52
+ # Two spaces, no tabs (for indentation).
53
+ Style/IndentationWidth:
54
+ Enabled: true
55
+
56
+ Style/SpaceAfterColon:
57
+ Enabled: true
58
+
59
+ Style/SpaceAfterComma:
60
+ Enabled: true
61
+
62
+ Style/SpaceAroundEqualsInParameterDefault:
63
+ Enabled: true
64
+
65
+ Style/SpaceAroundKeyword:
66
+ Enabled: true
67
+
68
+ Style/SpaceAroundOperators:
69
+ Enabled: true
70
+
71
+ Style/SpaceBeforeFirstArg:
72
+ Enabled: true
73
+
74
+ # Defining a method with parameters needs parentheses.
75
+ Style/MethodDefParentheses:
76
+ Enabled: true
77
+
78
+ # Use `foo {}` not `foo{}`.
79
+ Style/SpaceBeforeBlockBraces:
80
+ Enabled: true
81
+
82
+ # Use `foo { bar }` not `foo {bar}`.
83
+ Style/SpaceInsideBlockBraces:
84
+ Enabled: true
85
+
86
+ # Use `{ a: 1 }` not `{a:1}`.
87
+ Style/SpaceInsideHashLiteralBraces:
88
+ Enabled: true
89
+
90
+ Style/SpaceInsideParens:
91
+ Enabled: true
92
+
93
+ # Check quotes usage according to lint rule below.
94
+ Style/StringLiterals:
95
+ Enabled: true
96
+ EnforcedStyle: double_quotes
97
+
98
+ # Detect hard tabs, no hard tabs.
99
+ Style/Tab:
100
+ Enabled: true
101
+
102
+ # Blank lines should not have any spaces.
103
+ Style/TrailingBlankLines:
104
+ Enabled: true
105
+
106
+ # No trailing whitespace.
107
+ Style/TrailingWhitespace:
108
+ Enabled: true
109
+
110
+ # Use quotes for string literals when they are enough.
111
+ Style/UnneededPercentQ:
112
+ Enabled: true
113
+
114
+ # Align `end` with the matching keyword or starting expression except for
115
+ # assignments, where it should be aligned with the LHS.
116
+ Lint/EndAlignment:
117
+ Enabled: true
118
+ EnforcedStyleAlignWith: variable
119
+
120
+ # Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
121
+ Lint/RequireParentheses:
122
+ Enabled: true
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,41 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2017-03-09 13:15:36 +0900 using RuboCop version 0.47.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ Style/Documentation:
11
+ Exclude:
12
+ - 'spec/**/*'
13
+ - 'test/**/*'
14
+ - 'lib/hefted.rb'
15
+
16
+ # Offense count: 1
17
+ # Cop supports --auto-correct.
18
+ # Configuration parameters: SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
19
+ # SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
20
+ Style/HashSyntax:
21
+ EnforcedStyle: hash_rockets
22
+
23
+ # Offense count: 1
24
+ # Cop supports --auto-correct.
25
+ Style/MutableConstant:
26
+ Exclude:
27
+ - 'lib/hefted/version.rb'
28
+
29
+ # Offense count: 26
30
+ # Cop supports --auto-correct.
31
+ # Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
32
+ # SupportedStyles: single_quotes, double_quotes
33
+ Style/StringLiterals:
34
+ Exclude:
35
+ - 'Rakefile'
36
+ - 'bin/console'
37
+ - 'hefted.gemspec'
38
+ - 'lib/hefted.rb'
39
+ - 'lib/hefted/version.rb'
40
+ - 'spec/hefted_spec.rb'
41
+ - 'spec/spec_helper.rb'
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ - 2.4.1
6
+ cache: bundler
7
+ bundler_args: "--without production"
8
+ before_install: gem install bundler
9
+ script:
10
+ - bundle exec rspec
11
+ branches:
12
+ only:
13
+ - master
14
+ notifications:
15
+ email: false
16
+ slack:
17
+ secure: Ux1Axyx+kqBdzI+bXVCMy1aTzA9GPR0Te8DDCo2Hr3enVwcmhbETeYI0IIXrMSUm94jEEjmMkypMMEFb8DcZHbOQAfohqPsJeyHVYvp8Pf2b6P+F7X8kQS1YCNcfuQ1hr/sNCGrQanBSOM4cONWWVPqBu/Sr8wWbNSyjLZzaXEEu/tZBCg3Lxv2IPvIFpTk6FRGMRXL7ESGXhtUIO8juziBP7onFBD4fn3B62ZQ7sXtBpVukB8Quo4ZrnqUzXvgkJq2IplzBQpTP2zG/1DSEjTBympxwJxK+LpJFNfsmU7peh8zlTcWSZkUW54afgugblhAhAK1iJ+XN2kzGcbJEXESHqusmmD50o5lsYJkCfEsCOYkwvyiqu+sUdR+cUX/40b3pG6WiKtL1AhBSuU52rwTX4r6qXdtRu2OYjO/kA8Fijpw+G9f56UVbimTF6wrqLyG96+n0IaxkcBAFpRNbM/HeS0iH/9zqml6K6GDLu9QbKsIr1EAUk5ZWztavkyf+uVvo4X2C+XiChZ8+5HM1R9sFMe6fQ2tflLl0azzWImhzQ86VP4dKl+YrKk4QWfOuyArTNtNFAPJ4GWruz9ZTh4UPhi68+zBsapxyOazg4Z3our7O0GQbwcoXdeQjnD7UmalnzlfIx9LUmREJrhE9G84Mr4/CAWpjpXqAK6Mf6XI=
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hefted.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Hefted
2
+
3
+ [![Build Status](https://travis-ci.org/weathare/hefted.svg?branch=master)](https://travis-ci.org/weathare/hefted)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'hefted'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hefted
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ class Options
25
+ include Hefted
26
+
27
+ hefted(
28
+ name: :generation,
29
+ young: 10,
30
+ middle: 30,
31
+ old: 50
32
+ )
33
+
34
+ hefted(
35
+ name: :gender,
36
+ members: [:none, :male, :female],
37
+ first: 9
38
+ )
39
+
40
+ hefted(
41
+ name: :personal,
42
+ join: [:generation, :gender],
43
+ )
44
+ end
45
+
46
+ Options::Generation.middle
47
+ => 30
48
+
49
+ Options::Gender
50
+ => #<struct none=9, male=10, female=11>
51
+
52
+ Options::Personal.young
53
+ => 10
54
+
55
+ Options::Personal.female
56
+ => 11
57
+ ```
58
+
59
+ ## License
60
+
61
+ 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/hefted.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hefted/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hefted"
8
+ spec.version = Hefted::VERSION
9
+ spec.authors = ["weathare"]
10
+ spec.email = ["fuzuki.close@gmail.com"]
11
+
12
+ spec.summary = "Minimal Struct making constants."
13
+ spec.homepage = "https://github.com/weathare/hefted"
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.required_ruby_version = ">= 2.3.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.14"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
@@ -0,0 +1,51 @@
1
+ module Hefted
2
+ class MissingKeysError < StandardError; end
3
+ class MissingValuesError < StandardError; end
4
+
5
+ class Argument
6
+ using Hefted::Refine
7
+
8
+ def initialize(**args)
9
+ @name = args.fetch(:name)
10
+ @join = args.fetch(:join, nil)
11
+ @first = args.indexer!(:first)[:first]
12
+ @members = args[:members] || args.select { |key, value| !%i(name join).include?(key) }
13
+ end
14
+
15
+ def name
16
+ @name.to_camel
17
+ end
18
+
19
+ def keys
20
+ case @members
21
+ when Array
22
+ raise MissingKeysError if @members.include?(nil)
23
+ @members
24
+ when Hash
25
+ @members.keys
26
+ else
27
+ raise MissingKeysError
28
+ end.map(&:to_sym)
29
+ end
30
+
31
+ def values
32
+ case @members
33
+ when Array
34
+ @members.map.with_index(@first) { |key, i| i }
35
+ when Hash
36
+ raise MissingValuesError if @members.has_value?(nil)
37
+ @members.values
38
+ else
39
+ raise MissingValuesError
40
+ end
41
+ end
42
+
43
+ def join?
44
+ !!@join
45
+ end
46
+
47
+ def joins
48
+ @join.map { |name| name.to_camel }
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,88 @@
1
+ module Hefted
2
+ module ClassMethod
3
+ using Hefted::Refine
4
+
5
+ def hefted(**args)
6
+ arguments = const_join(args)
7
+ template = Base.new(*arguments.keys)
8
+ self.const_set(arguments.name, template.new(*arguments.values).freeze)
9
+ end
10
+
11
+ def release_hefted(*names)
12
+ names.each do |name|
13
+ remove_const(name.to_camel) if const_defined?(name.to_camel)
14
+ end
15
+ end
16
+
17
+ private
18
+ def const_join(**args)
19
+ arguments = Argument.new(**args)
20
+ if arguments.join?
21
+ _consts = arguments.joins.each_with_object({}) do |name, hash|
22
+ hash.merge!(self.const_get(name).to_h)
23
+ end.merge!(arguments.keys.zip(arguments.values).to_h)
24
+ Argument.new(name: arguments.name, **_consts)
25
+ else
26
+ arguments
27
+ end
28
+ end
29
+
30
+ class Base < Struct
31
+ def each
32
+ return to_enum(:each) unless block_given?
33
+ members.each do |key|
34
+ yield(key, send(key))
35
+ end
36
+ end
37
+
38
+ def each_key
39
+ return to_enum(:each_key) unless block_given?
40
+ keys.each { |key| yield(key) }
41
+ end
42
+
43
+ def each_value
44
+ return to_enum(:each_value) unless block_given?
45
+ values.each { |value| yield(value) }
46
+ end
47
+
48
+ def keys
49
+ members
50
+ end
51
+
52
+ def has_key?(key)
53
+ keys.include?(key.to_sym)
54
+ end
55
+ alias :key? :has_key?
56
+
57
+ def has_value?(value)
58
+ values.include?(value)
59
+ end
60
+ alias :value? :has_value?
61
+
62
+ def fetch(key, *args)
63
+ return send(key) if has_key?(key)
64
+ return args.first if args.size > 0
65
+ return yield(key) if block_given?
66
+ end
67
+
68
+ def fetch_values(*_keys)
69
+ _keys.map do |key|
70
+ if has_key?(key)
71
+ fetch(key)
72
+ else
73
+ yield(key) if block_given?
74
+ end
75
+ end
76
+ end
77
+
78
+ def values_at(*_keys)
79
+ fetch_values(*_keys)
80
+ end
81
+
82
+ def [](key)
83
+ fetch(key)
84
+ end
85
+ end
86
+ private_constant :Base
87
+ end
88
+ end
@@ -0,0 +1,3 @@
1
+ require "hefted"
2
+
3
+ Object.send(:include, Hefted)
@@ -0,0 +1,27 @@
1
+ module Hefted
2
+ module Refine
3
+ refine Hash do
4
+ def indexer!(*keys)
5
+ idx = keys.map do |key|
6
+ value = self[key].to_i if self.key?(key)
7
+ value ||= 0
8
+ [key, value]
9
+ end
10
+ self.delete_if { |key, _| keys.include?(key) }
11
+ idx.to_h
12
+ end
13
+ end
14
+
15
+ refine String do
16
+ def to_camel
17
+ self.gsub(/(?:^|_|\s)(.)/) { $1.upcase }
18
+ end
19
+ end
20
+
21
+ refine Symbol do
22
+ def to_camel
23
+ self.to_s.to_camel.to_sym
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module Hefted
2
+ VERSION = "0.3.0"
3
+ end
data/lib/hefted.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "hefted/version"
2
+
3
+ module Hefted
4
+ autoload :Refine, "hefted/refine"
5
+ autoload :Argument, "hefted/argument"
6
+ autoload :ClassMethod, "hefted/class_method"
7
+
8
+ def self.included(base)
9
+ base.extend(ClassMethod)
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hefted
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - weathare
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-25 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - fuzuki.close@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".rubocop_todo.yml"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - README.md
83
+ - Rakefile
84
+ - hefted.gemspec
85
+ - lib/hefted.rb
86
+ - lib/hefted/argument.rb
87
+ - lib/hefted/class_method.rb
88
+ - lib/hefted/core_ext.rb
89
+ - lib/hefted/refine.rb
90
+ - lib/hefted/version.rb
91
+ homepage: https://github.com/weathare/hefted
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.3.0
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.11
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Minimal Struct making constants.
115
+ test_files: []