shrift 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +23 -0
- data/.travis.yml +1 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +4 -2
- data/README.md +55 -3
- data/lib/shrift/shrift_cell.rb +16 -0
- data/lib/shrift/shrift_exception.rb +3 -0
- data/lib/shrift/shrift_int.rb +12 -0
- data/lib/shrift/shrift_map.rb +51 -18
- data/lib/shrift/shrift_mapper.rb +61 -0
- data/lib/shrift/shrift_version.rb +3 -0
- data/lib/shrift.rb +25 -4
- data/shrift.gemspec +3 -3
- metadata +10 -6
- data/lib/shrift/shriftlet.rb +0 -5
- data/lib/shrift/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ffd10c8e7f662dcd01939712e0f864dd5b6e159
|
4
|
+
data.tar.gz: 12153ba532ac676cfa55daeb6b017822c6427831
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 939ae77dd4dc6b30b350b6d9bd4467d5533d61a8b9ea6a3d891370eb6ffe402d83acff3edce152dfd7c3d006082225d6783b423053c7db4aa2dceb19067b2ad8
|
7
|
+
data.tar.gz: 39c347c5b630b14d98a6a623ed4b3aae6ad65593d4195b39de7e993a62f0ecc2a6974e16bed2960cbfb2a6e771c9c5284455037859b1c4c0403b5fee61a5397a
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# https://github.com/bbatsov/rubocop/blob/master/config/enabled.yml
|
2
|
+
|
3
|
+
Metrics/AbcSize:
|
4
|
+
Enabled: true
|
5
|
+
Max: 22
|
6
|
+
|
7
|
+
Metrics/LineLength:
|
8
|
+
Description: 'Limit lines to 80 characters.'
|
9
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#80-character-limits'
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Style/SpaceInsideHashLiteralBraces:
|
13
|
+
EnforcedStyle: no_space
|
14
|
+
EnforcedStyleForEmptyBraces: no_space
|
15
|
+
SupportedStyles:
|
16
|
+
- space
|
17
|
+
- no_space
|
18
|
+
|
19
|
+
Style/IndentHash:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Style/MethodLength:
|
23
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
shrift (0.0.
|
4
|
+
shrift (0.0.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -50,6 +50,7 @@ GEM
|
|
50
50
|
coercible (~> 1.0)
|
51
51
|
descendants_tracker (~> 0.0, >= 0.0.3)
|
52
52
|
equalizer (~> 0.0, >= 0.0.9)
|
53
|
+
yamlable (0.0.2)
|
53
54
|
|
54
55
|
PLATFORMS
|
55
56
|
ruby
|
@@ -62,6 +63,7 @@ DEPENDENCIES
|
|
62
63
|
reek
|
63
64
|
rubocop
|
64
65
|
shrift!
|
66
|
+
yamlable
|
65
67
|
|
66
68
|
BUNDLED WITH
|
67
|
-
1.
|
69
|
+
1.12.3
|
data/README.md
CHANGED
@@ -2,9 +2,13 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/folkengine/shrift.svg?branch=master)](https://travis-ci.org/folkengine/shrift)
|
4
4
|
|
5
|
-
|
5
|
+
Makes short shrift of objects.
|
6
6
|
|
7
|
-
|
7
|
+
## Warning
|
8
|
+
|
9
|
+
This is alpha software and only works with basic Objects.
|
10
|
+
|
11
|
+
Shrift is a simple DSL generator that turns Objects to single line Strings for easy storage and debugging.
|
8
12
|
|
9
13
|
## Installation
|
10
14
|
|
@@ -24,7 +28,55 @@ Or install it yourself as:
|
|
24
28
|
|
25
29
|
## Usage
|
26
30
|
|
27
|
-
|
31
|
+
Given a simple class:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Character
|
35
|
+
attr_accessor :character_name, :strength, :dexterity,
|
36
|
+
:constitution, :wisdom, :intelligence, :charisma, :hit_points
|
37
|
+
|
38
|
+
def initialize(character_name = '')
|
39
|
+
@character_name = character_name
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
I setup a Shrift that maps the first cell to character_name, the second cell to a ShriftMap of most of the integer
|
45
|
+
fields, and the third field mapped as an integer to hit_points:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
shrift_mapper = ShriftMapper.new(hashmap: {
|
49
|
+
ST: 'strength',
|
50
|
+
DX: 'dexterity',
|
51
|
+
CN: 'constitution',
|
52
|
+
W: 'wisdom',
|
53
|
+
I: 'intelligence',
|
54
|
+
CH: 'charisma'})
|
55
|
+
shrift = Shrift.new(Character, [ShriftCell.new(:character_name), shrift_mapper, ShriftInt.new(:hit_points)])
|
56
|
+
|
57
|
+
bub = Character.new('bub')
|
58
|
+
bub.strength = 15
|
59
|
+
bub.dexterity = 14
|
60
|
+
bub.constitution = 10
|
61
|
+
bub.wisdom = 18
|
62
|
+
bub.intelligence = 3
|
63
|
+
bub.charisma = 6
|
64
|
+
bub.hit_points = 4
|
65
|
+
|
66
|
+
bubbu_shrift = shrift.short(bub)
|
67
|
+
```
|
68
|
+
|
69
|
+
This returns a Shrift String of:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
bub:st15dx14cn10w18i3ch6:4
|
73
|
+
```
|
74
|
+
|
75
|
+
To turn it back into an Object, I just need to pass the Shrift String into the Shrift's classify method:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
new_bub = @shrift.classify(bubbu_shrift)
|
79
|
+
```
|
28
80
|
|
29
81
|
## Development
|
30
82
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# A basic Shrift Cell mapping to a String.
|
2
|
+
class ShriftCell
|
3
|
+
attr_reader :field
|
4
|
+
|
5
|
+
def initialize(field)
|
6
|
+
@field = field
|
7
|
+
end
|
8
|
+
|
9
|
+
def process(target)
|
10
|
+
target.send(field)
|
11
|
+
end
|
12
|
+
|
13
|
+
def set(value, target)
|
14
|
+
target.send("#{@field}=", value)
|
15
|
+
end
|
16
|
+
end
|
data/lib/shrift/shrift_map.rb
CHANGED
@@ -1,26 +1,59 @@
|
|
1
|
-
|
2
|
-
class ShriftMap
|
3
|
-
attr_accessor :default, :map
|
4
|
-
attr_reader :shrift_map_string
|
5
|
-
|
6
|
-
def initialize(shrift_map_string, default = nil)
|
7
|
-
@default = default
|
8
|
-
@shrift_map_string = shrift_map_string
|
9
|
-
parse(shrift_map_string)
|
10
|
-
end
|
1
|
+
require_relative 'shrift_exception'
|
11
2
|
|
12
|
-
|
13
|
-
|
14
|
-
|
3
|
+
# A String that alternates between characters and numbers. Short hand for a collection
|
4
|
+
# of variables that hold integer values.
|
5
|
+
#
|
6
|
+
# Shrift map keys are lowercase.
|
7
|
+
#
|
8
|
+
class ShriftMap
|
9
|
+
attr_reader :hashmap, :shrift_map_string
|
15
10
|
|
16
|
-
|
17
|
-
|
18
|
-
|
11
|
+
def initialize(shrift_map_string)
|
12
|
+
@shrift_map_string = shrift_map_string
|
13
|
+
@hashmap = {}
|
14
|
+
split(shrift_map_string)
|
15
|
+
end
|
16
|
+
|
17
|
+
def fetch(key)
|
18
|
+
@hashmap[key.downcase.to_sym].to_i
|
19
|
+
end
|
20
|
+
|
21
|
+
def store(key, value)
|
22
|
+
@hashmap[key.downcase.to_sym] = value.to_i
|
23
|
+
end
|
19
24
|
|
20
|
-
|
25
|
+
def to_hash
|
26
|
+
@hashmap
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_s
|
30
|
+
@shrift_map_string
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.to_shrift_map(hash)
|
34
|
+
ShriftMap.new(ShriftMap.to_shrift_map_string(hash))
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.to_shrift_map_string(hash)
|
38
|
+
shrift_map_string = ''
|
39
|
+
hash.each do |key, val|
|
40
|
+
shrift_map_string << key.to_s
|
41
|
+
shrift_map_string << val.to_s
|
42
|
+
end
|
43
|
+
shrift_map_string
|
44
|
+
end
|
21
45
|
|
22
|
-
|
46
|
+
private
|
23
47
|
|
48
|
+
def split(shrift_map_string)
|
49
|
+
until shrift_map_string.empty?
|
50
|
+
shrift_group = shrift_map_string.scan(/^([a-zA-Z]+)(\d+)(.*)/).first
|
51
|
+
begin
|
52
|
+
store(shrift_group[0], shrift_group[1])
|
53
|
+
rescue NoMethodError
|
54
|
+
raise ShriftException, "Unbalanced ShriftMap: #{@shrift_map_string}"
|
55
|
+
end
|
56
|
+
shrift_map_string = shrift_group[2]
|
24
57
|
end
|
25
58
|
end
|
26
59
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require_relative 'shrift_map'
|
2
|
+
|
3
|
+
# I map ShriftMaps
|
4
|
+
class ShriftMapper
|
5
|
+
attr_reader :hashmap
|
6
|
+
|
7
|
+
def initialize(hashmap: {})
|
8
|
+
@hashmap = hashmap
|
9
|
+
clean
|
10
|
+
end
|
11
|
+
|
12
|
+
def fetch(key)
|
13
|
+
@hashmap[key.downcase.to_sym]
|
14
|
+
end
|
15
|
+
|
16
|
+
def store(key, value)
|
17
|
+
@hashmap[key] = value
|
18
|
+
end
|
19
|
+
|
20
|
+
def parse(shrift_map_string)
|
21
|
+
hash = {}
|
22
|
+
ShriftMap.new(shrift_map_string).hashmap.each do |key, val|
|
23
|
+
hash[@hashmap[key]] = val
|
24
|
+
end
|
25
|
+
hash
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_hash
|
29
|
+
@hashmap
|
30
|
+
end
|
31
|
+
|
32
|
+
# :reek:UncommunicativeVariableName :reek:FeatureEnvy
|
33
|
+
def to_shrift_string(mappie)
|
34
|
+
return mappie if mappie.is_a?(String)
|
35
|
+
return mappie.map { |k, v| @hashmap.key(k).to_s.downcase + v.to_s }.join if mappie.is_a?(Hash)
|
36
|
+
@hashmap.map { |k, v| k.to_s.downcase + mappie.send(v).to_s }.join
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_shrift_map(mappie)
|
40
|
+
ShriftMap.new(to_shrift_string(mappie))
|
41
|
+
end
|
42
|
+
|
43
|
+
# Shrift Cell Methods
|
44
|
+
|
45
|
+
def process(target)
|
46
|
+
to_shrift_string(target)
|
47
|
+
end
|
48
|
+
|
49
|
+
# :reek:UncommunicativeVariableName
|
50
|
+
def set(value, target)
|
51
|
+
shrift_map = to_shrift_map(value)
|
52
|
+
@hashmap.map { |k, v| target.send("#{v}=", shrift_map.fetch(k)) }
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
# Convert Hash keys to symbols
|
58
|
+
def clean
|
59
|
+
@hashmap = Hash[@hashmap.map { |key, value| [key.downcase.to_sym, value] }]
|
60
|
+
end
|
61
|
+
end
|
data/lib/shrift.rb
CHANGED
@@ -1,9 +1,30 @@
|
|
1
|
-
require 'shrift/
|
2
|
-
require 'shrift/
|
1
|
+
require 'shrift/shrift_cell'
|
2
|
+
require 'shrift/shrift_int'
|
3
3
|
require 'shrift/shrift_map'
|
4
|
+
require 'shrift/shrift_mapper'
|
4
5
|
|
5
|
-
|
6
|
-
|
6
|
+
# Making short shrift of Objects.
|
7
|
+
class Shrift
|
8
|
+
attr_reader :clazz
|
7
9
|
|
10
|
+
def initialize(clazz, guide = [])
|
11
|
+
@clazz = clazz
|
12
|
+
@guide = guide
|
13
|
+
end
|
14
|
+
|
15
|
+
# Turns the target class into a Shrift String
|
16
|
+
def short(target)
|
17
|
+
@guide.map { |cell| cell.process(target) }.join(':')
|
18
|
+
end
|
19
|
+
|
20
|
+
def classify(shrift_string)
|
21
|
+
zippy = @guide.zip(shrift_string.split(':'))
|
22
|
+
objective = Object.const_get(clazz.to_s).new
|
23
|
+
|
24
|
+
zippy.each do |cell|
|
25
|
+
cell[0].set(cell[1], objective)
|
26
|
+
end
|
27
|
+
|
28
|
+
objective
|
8
29
|
end
|
9
30
|
end
|
data/shrift.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'shrift/
|
4
|
+
require 'shrift/shrift_version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = 'shrift'
|
8
|
-
spec.version =
|
8
|
+
spec.version = ShriftVersion::VERSION
|
9
9
|
spec.authors = ['Folkengine']
|
10
10
|
spec.email = ['gaoler@electronicpanopticon.com']
|
11
11
|
|
12
|
-
spec.summary = '
|
12
|
+
spec.summary = 'Ruby utility library to make short shrift of objects.'
|
13
13
|
spec.description = 'Custom object serializer DSL'
|
14
14
|
spec.homepage = 'https://github.com/folkengine/shrift'
|
15
15
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shrift
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Folkengine
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -46,6 +46,7 @@ extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
48
|
- ".gitignore"
|
49
|
+
- ".rubocop.yml"
|
49
50
|
- ".travis.yml"
|
50
51
|
- Gemfile
|
51
52
|
- Gemfile.lock
|
@@ -53,9 +54,12 @@ files:
|
|
53
54
|
- README.md
|
54
55
|
- Rakefile
|
55
56
|
- lib/shrift.rb
|
57
|
+
- lib/shrift/shrift_cell.rb
|
58
|
+
- lib/shrift/shrift_exception.rb
|
59
|
+
- lib/shrift/shrift_int.rb
|
56
60
|
- lib/shrift/shrift_map.rb
|
57
|
-
- lib/shrift/
|
58
|
-
- lib/shrift/
|
61
|
+
- lib/shrift/shrift_mapper.rb
|
62
|
+
- lib/shrift/shrift_version.rb
|
59
63
|
- shrift.gemspec
|
60
64
|
homepage: https://github.com/folkengine/shrift
|
61
65
|
licenses: []
|
@@ -76,8 +80,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
80
|
version: '0'
|
77
81
|
requirements: []
|
78
82
|
rubyforge_project:
|
79
|
-
rubygems_version: 2.
|
83
|
+
rubygems_version: 2.4.8
|
80
84
|
signing_key:
|
81
85
|
specification_version: 4
|
82
|
-
summary:
|
86
|
+
summary: Ruby utility library to make short shrift of objects.
|
83
87
|
test_files: []
|
data/lib/shrift/shriftlet.rb
DELETED
data/lib/shrift/version.rb
DELETED