self_data 0.1.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rspec +1 -0
- data/.rubocop.yml +5 -15
- data/.travis.yml +14 -3
- data/Gemfile +3 -1
- data/LICENSE.txt +1 -1
- data/README.md +86 -1
- data/Rakefile +2 -0
- data/bin/console +2 -10
- data/bin/rspec +17 -0
- data/bin/rubocop +29 -0
- data/lib/self_data.rb +29 -18
- data/lib/self_data/config.rb +9 -3
- data/lib/self_data/errors.rb +2 -0
- data/lib/self_data/version.rb +3 -1
- data/self_data.gemspec +18 -15
- metadata +46 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ace750c9fee999c5fee53ce98fa9e958f8cfa405234fc76433d0c12225167076
|
4
|
+
data.tar.gz: 1f37f78e87718c9ef7ce72c477803033e5a4d2c0d0eace08a89c416bdbfc1aed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c49d4345d6861651dc72f9a3389335baee5abebac90364830c68b05f5435ea81860873808e4d21055b929a4d0b9a3f441035ba76d94261640256054eb35935f
|
7
|
+
data.tar.gz: eaa40576e8998947d0799852c6adf5b474f702a3e1a77a05d466e7ec6de49efd7bbae83b646ef60158f1799e8cefc9085bd16b50bf5487c36e020b26ddf1d140
|
data/.rspec
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,16 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Exclude:
|
4
|
-
- 'self_data.gemspec'
|
1
|
+
inherit_gem:
|
2
|
+
rubocop-config-umbrellio: lib/rubocop.yml
|
5
3
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
Style/StringLiterals:
|
10
|
-
Enabled: false
|
11
|
-
|
12
|
-
Style/MultilineOperationIndentation:
|
13
|
-
EnforcedStyle: indented
|
14
|
-
|
15
|
-
Style/AndOr:
|
16
|
-
EnforcedStyle: conditionals
|
4
|
+
AllCops:
|
5
|
+
DisplayCopNames: true
|
6
|
+
TargetRubyVersion: 2.5
|
data/.travis.yml
CHANGED
@@ -1,4 +1,15 @@
|
|
1
1
|
language: ruby
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
sudo: false
|
3
|
+
before_install: gem install bundler
|
4
|
+
script:
|
5
|
+
- bundle exec rubocop
|
6
|
+
- bundle exec rspec
|
7
|
+
matrix:
|
8
|
+
fast_finish: true
|
9
|
+
include:
|
10
|
+
- rvm: 2.3.7
|
11
|
+
- rvm: 2.4.4
|
12
|
+
- rvm: 2.5.1
|
13
|
+
- rvm: ruby-head
|
14
|
+
allow_failures:
|
15
|
+
- rvm: ruby-head
|
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1 +1,86 @@
|
|
1
|
-
# SelfData
|
1
|
+
# SelfData · [![Gem Version](https://badge.fury.io/rb/self_data.svg)](https://badge.fury.io/rb/self_data) [![Build Status](https://travis-ci.org/umbrellio/self_data.svg?branch=master)](https://travis-ci.org/umbrellio/self_data) [![Coverage Status](https://coveralls.io/repos/github/umbrellio/self_data/badge.svg)](https://coveralls.io/github/umbrellio/self_data)
|
2
|
+
|
3
|
+
It's like native ruby `DATA` but you can use it not only for main file.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# Gemfile
|
9
|
+
gem 'self_data'
|
10
|
+
```
|
11
|
+
|
12
|
+
```shell
|
13
|
+
$ bundle install
|
14
|
+
-- or --
|
15
|
+
$ gem install self_data
|
16
|
+
```
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require 'self_data'
|
20
|
+
```
|
21
|
+
|
22
|
+
## Synopsis
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
# main.rb
|
26
|
+
|
27
|
+
require 'self_data'
|
28
|
+
require './a.rb'
|
29
|
+
|
30
|
+
puts "DATA: #{A.native_data}"
|
31
|
+
puts "SelfData: #{A.self_data}"
|
32
|
+
|
33
|
+
__END__
|
34
|
+
main.rb data
|
35
|
+
```
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
# a.rb
|
39
|
+
|
40
|
+
module A
|
41
|
+
module_function
|
42
|
+
|
43
|
+
def native_data
|
44
|
+
DATA.read
|
45
|
+
end
|
46
|
+
|
47
|
+
def self_data
|
48
|
+
SelfData.read
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
__END__
|
53
|
+
a.rb data
|
54
|
+
```
|
55
|
+
|
56
|
+
and if you execute it, you'll get:
|
57
|
+
|
58
|
+
```shell
|
59
|
+
$ ruby main.rb
|
60
|
+
DATA: main.rb data
|
61
|
+
SelfData: a.rb data
|
62
|
+
```
|
63
|
+
|
64
|
+
## Usage and Notes
|
65
|
+
|
66
|
+
Use `SelfData.read()` to get text or use `SelfData.load(*formats, **options)` to get formatted data (like yaml, xml, erb, etc).
|
67
|
+
|
68
|
+
Look at the [lib/self_data/config.rb](https://github.com/umbrellio/self_data/blob/master/lib/self_data/config.rb) for more details.
|
69
|
+
|
70
|
+
SelfData uses `caller`, it's not fast, that's why you shouldn't use SelfData in loops.
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/self_data.
|
75
|
+
|
76
|
+
## License
|
77
|
+
|
78
|
+
Released under MIT License.
|
79
|
+
|
80
|
+
## Authors
|
81
|
+
|
82
|
+
Created by [7dr1v3](https://github.com/7dr1v3)
|
83
|
+
|
84
|
+
<a href="https://github.com/umbrellio/">
|
85
|
+
<img style="float: left;" src="https://umbrellio.github.io/Umbrellio/supported_by_umbrellio.svg" alt="Supported by Umbrellio" width="439" height="72">
|
86
|
+
</a>
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -2,13 +2,5 @@
|
|
2
2
|
|
3
3
|
require "bundler/setup"
|
4
4
|
require "self_data"
|
5
|
-
|
6
|
-
|
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
|
5
|
+
require "pry"
|
6
|
+
Pry.start
|
data/bin/rspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'rspec' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
16
|
+
|
17
|
+
load Gem.bin_path("rspec-core", "rspec")
|
data/bin/rubocop
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rubocop' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("rubocop", "rubocop")
|
data/lib/self_data.rb
CHANGED
@@ -1,20 +1,31 @@
|
|
1
|
-
|
2
|
-
require "active_support/core_ext/module/delegation.rb"
|
3
|
-
require "active_support/core_ext/class/attribute.rb"
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
3
|
class SelfData
|
6
|
-
class_attribute :default_formats, :default_options, :filters, :converters
|
7
|
-
|
8
4
|
class << self
|
9
|
-
|
5
|
+
attr_accessor :default_formats, :default_options
|
6
|
+
|
7
|
+
def read(*args, **kargs)
|
8
|
+
new.read(*args, **kargs)
|
9
|
+
end
|
10
|
+
|
11
|
+
def load(*args, **kargs)
|
12
|
+
new.load(*args, **kargs)
|
13
|
+
end
|
14
|
+
|
15
|
+
def filters
|
16
|
+
@filters ||= []
|
17
|
+
end
|
10
18
|
|
11
19
|
def add_filter(&block)
|
12
20
|
filters << block
|
13
21
|
end
|
14
22
|
|
23
|
+
def converters
|
24
|
+
@converters ||= {}
|
25
|
+
end
|
26
|
+
|
15
27
|
def add_converter(name, block)
|
16
|
-
|
17
|
-
self.converters[name] = block
|
28
|
+
converters[name] = block
|
18
29
|
end
|
19
30
|
end
|
20
31
|
|
@@ -25,32 +36,32 @@ class SelfData
|
|
25
36
|
end
|
26
37
|
|
27
38
|
def load(*formats, **options)
|
28
|
-
formats = default_formats if formats.empty?
|
29
|
-
options = default_options if options.empty?
|
39
|
+
formats = self.class.default_formats if formats.empty?
|
40
|
+
options = self.class.default_options if options.empty?
|
30
41
|
|
31
42
|
formats.reduce(read) do |data, format|
|
32
|
-
|
43
|
+
raise ConverterNotFound, format unless self.class.converters[format]
|
33
44
|
begin
|
34
|
-
converters[format].call(data, options)
|
35
|
-
rescue =>
|
36
|
-
raise ConversionError
|
45
|
+
self.class.converters[format].call(data, options)
|
46
|
+
rescue => error
|
47
|
+
raise ConversionError.new(format, error)
|
37
48
|
end
|
38
49
|
end
|
39
50
|
end
|
40
51
|
|
41
52
|
def read
|
42
|
-
IO.read(file).scan(/\n__END__\n(.*)/m).flatten.first or
|
53
|
+
IO.read(file).scan(/\n__END__\n(.*)/m).flatten.first or raise NoDataFound, file
|
43
54
|
end
|
44
55
|
|
45
56
|
private
|
46
57
|
|
47
58
|
def caller_file
|
48
59
|
calls = caller.lazy
|
49
|
-
.map { |call_string| call_string.split(
|
50
|
-
.
|
60
|
+
.map { |call_string| call_string.split(":").first }
|
61
|
+
.reject { |file| file == __FILE__ }
|
51
62
|
.select(&File.method(:exist?))
|
52
63
|
|
53
|
-
filters.each do |filter|
|
64
|
+
self.class.filters.each do |filter|
|
54
65
|
calls = calls.select(&filter.method(:call))
|
55
66
|
end
|
56
67
|
|
data/lib/self_data/config.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "erb"
|
4
|
+
require "yaml"
|
5
|
+
require "json"
|
6
|
+
|
1
7
|
SelfData.default_formats = [:erb, :yaml]
|
2
8
|
SelfData.default_options = {}
|
3
|
-
SelfData.filters = []
|
4
9
|
|
5
|
-
SelfData.add_converter :erb, ->(data, options) { ERB.new(data).result(options[:context]) }
|
6
|
-
SelfData.add_converter :yaml, ->(data, _options) { YAML.load(data) }
|
10
|
+
SelfData.add_converter :erb, -> (data, options) { ERB.new(data).result(options[:context]) }
|
11
|
+
SelfData.add_converter :yaml, -> (data, _options) { YAML.load(data) }
|
12
|
+
SelfData.add_converter :json, -> (data, _options) { JSON.parse(data) }
|
data/lib/self_data/errors.rb
CHANGED
data/lib/self_data/version.rb
CHANGED
data/self_data.gemspec
CHANGED
@@ -1,29 +1,32 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
5
|
+
require "self_data/version"
|
6
|
+
|
7
|
+
files_regexp = %r{^(test|spec|features)/}
|
5
8
|
|
6
9
|
Gem::Specification.new do |spec|
|
7
10
|
spec.name = "self_data"
|
8
11
|
spec.version = SelfData::VERSION
|
9
|
-
spec.authors = ["
|
10
|
-
spec.email = ["
|
12
|
+
spec.authors = ["Umbrellio"]
|
13
|
+
spec.email = ["info@umbrellio.biz"]
|
11
14
|
|
12
|
-
spec.summary = ""
|
15
|
+
spec.summary = "It's like DATA but better"
|
13
16
|
spec.description = "It's like DATA but better"
|
14
|
-
spec.homepage = "https://github.com/
|
15
|
-
spec.license =
|
17
|
+
spec.homepage = "https://github.com/umbrellio/self_data"
|
18
|
+
spec.license = "MIT"
|
16
19
|
|
17
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(files_regexp) }
|
18
21
|
spec.bindir = "exe"
|
19
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
23
|
spec.require_paths = ["lib"]
|
21
24
|
|
22
|
-
spec.
|
23
|
-
|
24
|
-
spec.add_development_dependency "bundler", "~> 1.10"
|
25
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
-
spec.add_development_dependency "rspec", "~> 3.4"
|
27
|
-
spec.add_development_dependency "rubocop", "~> 0.35"
|
25
|
+
spec.add_development_dependency "bundler"
|
26
|
+
spec.add_development_dependency "coveralls"
|
28
27
|
spec.add_development_dependency "pry"
|
28
|
+
spec.add_development_dependency "rake"
|
29
|
+
spec.add_development_dependency "rspec"
|
30
|
+
spec.add_development_dependency "rubocop-config-umbrellio"
|
31
|
+
spec.add_development_dependency "simplecov"
|
29
32
|
end
|
metadata
CHANGED
@@ -1,87 +1,101 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: self_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Umbrellio
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
20
|
-
type: :
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: coveralls
|
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: pry
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
|
-
- - "
|
45
|
+
- - ">="
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
47
|
+
version: '0'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
|
-
- - "
|
52
|
+
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rake
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
|
-
- - "
|
59
|
+
- - ">="
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
61
|
+
version: '0'
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
|
-
- - "
|
66
|
+
- - ">="
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rspec
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
|
-
- - "
|
73
|
+
- - ">="
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
75
|
+
version: '0'
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
|
-
- - "
|
80
|
+
- - ">="
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
82
|
+
version: '0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
|
-
name: rubocop
|
84
|
+
name: rubocop-config-umbrellio
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
|
-
- - "
|
87
|
+
- - ">="
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0
|
89
|
+
version: '0'
|
76
90
|
type: :development
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
|
-
- - "
|
94
|
+
- - ">="
|
81
95
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
98
|
+
name: simplecov
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
101
|
- - ">="
|
@@ -96,7 +110,7 @@ dependencies:
|
|
96
110
|
version: '0'
|
97
111
|
description: It's like DATA but better
|
98
112
|
email:
|
99
|
-
-
|
113
|
+
- info@umbrellio.biz
|
100
114
|
executables: []
|
101
115
|
extensions: []
|
102
116
|
extra_rdoc_files: []
|
@@ -110,13 +124,15 @@ files:
|
|
110
124
|
- README.md
|
111
125
|
- Rakefile
|
112
126
|
- bin/console
|
127
|
+
- bin/rspec
|
128
|
+
- bin/rubocop
|
113
129
|
- bin/setup
|
114
130
|
- lib/self_data.rb
|
115
131
|
- lib/self_data/config.rb
|
116
132
|
- lib/self_data/errors.rb
|
117
133
|
- lib/self_data/version.rb
|
118
134
|
- self_data.gemspec
|
119
|
-
homepage: https://github.com/
|
135
|
+
homepage: https://github.com/umbrellio/self_data
|
120
136
|
licenses:
|
121
137
|
- MIT
|
122
138
|
metadata: {}
|
@@ -135,9 +151,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
151
|
- !ruby/object:Gem::Version
|
136
152
|
version: '0'
|
137
153
|
requirements: []
|
138
|
-
|
139
|
-
rubygems_version: 2.4.5.1
|
154
|
+
rubygems_version: 3.1.2
|
140
155
|
signing_key:
|
141
156
|
specification_version: 4
|
142
|
-
summary: '
|
157
|
+
summary: It's like DATA but better
|
143
158
|
test_files: []
|