habuco 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 +7 -0
- data/.gitignore +3 -0
- data/.rspec +2 -0
- data/.rubocop.yml +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +115 -0
- data/Rakefile +1 -0
- data/habuco.gemspec +29 -0
- data/lib/habuco.rb +76 -0
- data/lib/habuco/attribute_definition.rb +11 -0
- data/lib/habuco/each_definition.rb +10 -0
- data/lib/habuco/version.rb +3 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 76ff8bfc1da34ac38c246427a72c1f45ceb9d3b1
|
4
|
+
data.tar.gz: ac88cff937a56dd4cfa64ea527e52295a6aa324c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c7c03b201b7016f00bb19dcccd4e88e4ed0f143cfc068a7dfbd4a1a81cd510dcd5e89c5d7151fccc40a6ed4c5e1ebfb6b7d2db042eec0739ead9f0404b764f9a
|
7
|
+
data.tar.gz: e7c1500f6d7d4212db08b4d6b433ca22f9b3a3c940c9412ecead424d78e0da2e2e6dc36e83b6f2a0e0336e719b6e50ed9ddf8ad7a513339b0259851a1cee3ced
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Krzysztof Wawer
|
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,115 @@
|
|
1
|
+
# Habuco
|
2
|
+
|
3
|
+
Habuco name come from HAsh BUilder with COntext.
|
4
|
+
|
5
|
+
It allows to use DSL to create hash structure with passed context.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'habuco'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install habuco
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Static value
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class HashBuilder
|
29
|
+
include Habuco
|
30
|
+
|
31
|
+
attribute :foo, :bar
|
32
|
+
end
|
33
|
+
|
34
|
+
HashBuilder.build # => { foo: :bar }
|
35
|
+
```
|
36
|
+
|
37
|
+
### Date and Time value
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class HashBuilder
|
41
|
+
include Habuco
|
42
|
+
|
43
|
+
attribute :date, -> { Date.new(2000) }
|
44
|
+
end
|
45
|
+
|
46
|
+
HashBuilder.build # => { date: #<Date: 2000-01-01 ((2447893j,0s,0n),+0s,2299161j)> }
|
47
|
+
```
|
48
|
+
|
49
|
+
### Value with context
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class HashBuilder
|
53
|
+
include Habuco
|
54
|
+
|
55
|
+
attribute :foo, -> { user_name }
|
56
|
+
end
|
57
|
+
|
58
|
+
HashBuilder.build(user_name: 'John') # => { foo: 'John' }
|
59
|
+
```
|
60
|
+
|
61
|
+
### Namespace
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
class HashBuilder
|
65
|
+
include Habuco
|
66
|
+
|
67
|
+
namespace :foo do
|
68
|
+
attribute :bar, :foobar
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
HashBuilder.build # => { foo: { bar: :foobar } }
|
73
|
+
```
|
74
|
+
|
75
|
+
### Dynamic key
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
class HashBuilder
|
79
|
+
include Habuco
|
80
|
+
|
81
|
+
attribute -> { :"foo_#{n}" }, :bar
|
82
|
+
end
|
83
|
+
|
84
|
+
HashBuilder.build(n: 123) # => { foo_123: :bar }
|
85
|
+
```
|
86
|
+
|
87
|
+
### Collection
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class HashBuilder
|
91
|
+
include Habuco
|
92
|
+
|
93
|
+
each_with_index -> { collection } do |val, index|
|
94
|
+
attribute :"foo_#{index}", val
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
HashBuilder.build(collection: %i[red green blue]) # => { foo_0: :red, foo_1: :green, foo_2: :blue }
|
99
|
+
```
|
100
|
+
|
101
|
+
## Development
|
102
|
+
|
103
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
104
|
+
|
105
|
+
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).
|
106
|
+
|
107
|
+
## Contributing
|
108
|
+
|
109
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/habuco.
|
110
|
+
|
111
|
+
|
112
|
+
## License
|
113
|
+
|
114
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
115
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/habuco.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'habuco/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'habuco'
|
9
|
+
spec.version = Habuco::VERSION
|
10
|
+
spec.authors = ['Krzysztof Wawer']
|
11
|
+
spec.email = ['krzysztof.wawer@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'HAsh BUilder with COntext'
|
14
|
+
spec.description = 'HAsh BUilder with COntext'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.5'
|
27
|
+
spec.add_development_dependency 'rubocop', '0.49.1'
|
28
|
+
spec.add_development_dependency 'timecop', '~> 0.9'
|
29
|
+
end
|
data/lib/habuco.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
require 'habuco/attribute_definition'
|
4
|
+
require 'habuco/each_definition'
|
5
|
+
require 'habuco/version'
|
6
|
+
|
7
|
+
module Habuco
|
8
|
+
module ClassMethods
|
9
|
+
def attribute(name, value = nil)
|
10
|
+
ns = namespace_scope.dup
|
11
|
+
attribute_definitions[name] = AttributeDefinition.new(name, value, ns)
|
12
|
+
end
|
13
|
+
|
14
|
+
def attribute_definitions
|
15
|
+
@attribute_definitions ||= {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def namespace(name)
|
19
|
+
namespace_scope.push(name)
|
20
|
+
yield
|
21
|
+
namespace_scope.pop
|
22
|
+
end
|
23
|
+
|
24
|
+
def namespace_scope
|
25
|
+
@namespace_scope ||= []
|
26
|
+
end
|
27
|
+
|
28
|
+
def each_with_index(coll, &block)
|
29
|
+
each_definitions[coll.hash] = EachDefinition.new(coll, block)
|
30
|
+
end
|
31
|
+
|
32
|
+
def each_definitions
|
33
|
+
@each_definitions ||= {}
|
34
|
+
end
|
35
|
+
|
36
|
+
def build(context = {})
|
37
|
+
new(context).build
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.included(base)
|
42
|
+
base.extend(ClassMethods)
|
43
|
+
end
|
44
|
+
|
45
|
+
attr_reader :context
|
46
|
+
|
47
|
+
def initialize(context = {})
|
48
|
+
@context = OpenStruct.new(context)
|
49
|
+
end
|
50
|
+
|
51
|
+
def build
|
52
|
+
{}.tap do |data|
|
53
|
+
perform_each_definitions
|
54
|
+
perform_attribute_definitions(data)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def perform_each_definitions
|
61
|
+
self.class.each_definitions.each do |_key, definition|
|
62
|
+
values = definition.values
|
63
|
+
block = definition.block
|
64
|
+
context.instance_exec(&values).each_with_index(&block)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def perform_attribute_definitions(data)
|
69
|
+
self.class.attribute_definitions.each do |key, definition|
|
70
|
+
val = definition.value
|
71
|
+
d = definition.namespace.inject(data) { |h, k| h[k] ||= {} }
|
72
|
+
k = key.respond_to?(:call) ? context.instance_exec(&key) : key
|
73
|
+
d[k] = val.respond_to?(:call) ? context.instance_exec(&val) : val
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: habuco
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Krzysztof Wawer
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-02 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: 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.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.49.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.49.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: timecop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.9'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.9'
|
83
|
+
description: HAsh BUilder with COntext
|
84
|
+
email:
|
85
|
+
- krzysztof.wawer@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".rubocop.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- habuco.gemspec
|
98
|
+
- lib/habuco.rb
|
99
|
+
- lib/habuco/attribute_definition.rb
|
100
|
+
- lib/habuco/each_definition.rb
|
101
|
+
- lib/habuco/version.rb
|
102
|
+
homepage:
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.6.11
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: HAsh BUilder with COntext
|
126
|
+
test_files: []
|