beret 0.1.0 → 0.2.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 +4 -4
- data/README.md +22 -0
- data/lib/beret/block.rb +62 -0
- data/lib/beret/block_set.rb +36 -0
- data/lib/beret/search.rb +34 -0
- data/lib/beret/search_result.rb +16 -0
- data/lib/beret/version.rb +2 -2
- data/lib/beret.rb +12 -7
- metadata +5 -2
- data/lib/beret/hash.rb +0 -52
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a817f9ff6f03c7ae6d433570a362cfb69aa8f77
|
4
|
+
data.tar.gz: 8f13e43d494aa258a1fc6ecdf8f52ae54ff66ce9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec88df2a4876be85eb9f3797e23e197130cc54d1bae0f7d1580aa9c0ab4f800e960e1458046847f37affee04a7d08b038f82c5ecbf09d4a9732630dd902dc0ab
|
7
|
+
data.tar.gz: 18756d96c65e26303c8d3c79b212dea5ae6dda16de0ca70056d7da78f15e803c3975c5f117faf6a50d3b815810d7642854435a4c65b22c140b4d119666dcaf2f
|
data/README.md
CHANGED
@@ -21,3 +21,25 @@ Or install it yourself as:
|
|
21
21
|
## Usage
|
22
22
|
|
23
23
|
Beret lets you easily work with [Colonel Kurtz](https://github.com/vigetlabs/colonel-kurtz)-generated JSON data in your Ruby application.
|
24
|
+
|
25
|
+
### Examples
|
26
|
+
```ruby
|
27
|
+
# Grab your CK JSON from wherever it lives.
|
28
|
+
ck_json = "[{\"content\":{\"text\":\"This is some text.\"},\"type\":\"redactor\",\"blocks\":[{\"type\":\"image\", \"content\":{\"photo_id\":\"42\"}},{\"type\":\"avatar\", \"content\":{\"photo_id\":\"144\"}}]}]"
|
29
|
+
|
30
|
+
# Initialization
|
31
|
+
beret = Beret.new(ck_json)
|
32
|
+
|
33
|
+
# Finding values
|
34
|
+
beret.find(field: :photo_id) # => [<Beret::Result value="42">, <Beret::Result value="144">]
|
35
|
+
beret.find(field: :photo_id, in: [:image]) # => [<Beret::Result value="42">]
|
36
|
+
beret.find(field: :photo_id, in: [:avatar]) # => [<Beret::Result value="144">]
|
37
|
+
beret.find(field: :photo_id, in: [:some_other_block_type]) # => []
|
38
|
+
|
39
|
+
# In-place updating
|
40
|
+
beret.update(:count, in: [:kitten_count]) do |value|
|
41
|
+
value + " kittens"
|
42
|
+
end
|
43
|
+
|
44
|
+
# Retrieving JSON
|
45
|
+
beret.to_json
|
data/lib/beret/block.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require "securerandom"
|
2
|
+
|
3
|
+
module Beret
|
4
|
+
class Block
|
5
|
+
|
6
|
+
attr_reader :id, :parent
|
7
|
+
|
8
|
+
def initialize(block_json, options={})
|
9
|
+
@id = SecureRandom.uuid
|
10
|
+
@block_json = block_json
|
11
|
+
@parent = options[:parent]
|
12
|
+
end
|
13
|
+
|
14
|
+
def attributes
|
15
|
+
@attributes ||= block_json[content_key]
|
16
|
+
end
|
17
|
+
|
18
|
+
def update_attribute(field_name, value)
|
19
|
+
@attributes[field_name.to_s] = value
|
20
|
+
end
|
21
|
+
|
22
|
+
def type
|
23
|
+
block_json[type_key]
|
24
|
+
end
|
25
|
+
|
26
|
+
def children
|
27
|
+
@children ||= begin
|
28
|
+
block_json[children_key] && block_json[children_key].map do |block|
|
29
|
+
Block.new(block, parent: self)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_json
|
35
|
+
{}.tap do |json|
|
36
|
+
json[type_key] = block_json[type_key]
|
37
|
+
json[content_key] = attributes
|
38
|
+
|
39
|
+
if children
|
40
|
+
json[children_key] = children.map(&:to_json)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
attr_reader :block_json
|
48
|
+
|
49
|
+
def children_key
|
50
|
+
"blocks"
|
51
|
+
end
|
52
|
+
|
53
|
+
def content_key
|
54
|
+
"content"
|
55
|
+
end
|
56
|
+
|
57
|
+
def type_key
|
58
|
+
"type"
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Beret
|
2
|
+
class BlockSet
|
3
|
+
|
4
|
+
def initialize(block_json)
|
5
|
+
@block_json = block_json
|
6
|
+
end
|
7
|
+
|
8
|
+
def find(field_name, options)
|
9
|
+
Search.new(blocks, field_name, options).results
|
10
|
+
end
|
11
|
+
|
12
|
+
def update(field_name, options)
|
13
|
+
results = find(field_name, options)
|
14
|
+
|
15
|
+
results.each do |result|
|
16
|
+
new_value = yield result.value
|
17
|
+
result.block.update_attribute(field_name, new_value)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def blocks
|
22
|
+
@blocks ||= block_json.map do |block|
|
23
|
+
Block.new(block)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_json
|
28
|
+
blocks.map(&:to_json)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
attr_reader :block_json
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
data/lib/beret/search.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Beret
|
2
|
+
class Search
|
3
|
+
|
4
|
+
def initialize(block_set, field_name, options={})
|
5
|
+
@block_set = block_set
|
6
|
+
@field_name = field_name.to_s
|
7
|
+
@applicable_block_types = Array(options.fetch(:in, [])).map(&:to_s)
|
8
|
+
end
|
9
|
+
|
10
|
+
def results
|
11
|
+
block_list = flatten_block_set(block_set)
|
12
|
+
block_list.map do |block|
|
13
|
+
if (applicable_block_types.empty? || applicable_block_types.include?(block.type)) && block.attributes.has_key?(field_name)
|
14
|
+
SearchResult.new(block, field_name)
|
15
|
+
end
|
16
|
+
end.compact
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_reader :applicable_block_types, :block_set, :field_name
|
22
|
+
|
23
|
+
def flatten_block_set(block_set)
|
24
|
+
block_set.each do |entry|
|
25
|
+
if entry.children
|
26
|
+
block_set = [block_set, flatten_block_set(entry.children)].compact.flatten
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
block_set
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
data/lib/beret/version.rb
CHANGED
data/lib/beret.rb
CHANGED
@@ -1,11 +1,16 @@
|
|
1
|
-
require "
|
2
|
-
|
3
|
-
# require "colonel_kurtz/block"
|
4
|
-
# require "colonel_kurtz/block/data"
|
5
|
-
# require "colonel_kurtz/block/type"
|
6
|
-
# require "colonel_kurtz/model/blockable"
|
1
|
+
require "json"
|
7
2
|
|
8
|
-
require "beret/
|
3
|
+
require "beret/version"
|
4
|
+
require "beret/block"
|
5
|
+
require "beret/search"
|
6
|
+
require "beret/search_result"
|
7
|
+
require "beret/block_set"
|
9
8
|
|
10
9
|
module Beret
|
10
|
+
def self.new(json)
|
11
|
+
array = JSON.parse(json)
|
12
|
+
Beret::BlockSet.new(array)
|
13
|
+
rescue JSON::ParserError
|
14
|
+
raise "String provided to Beret.new() was not valid JSON."
|
15
|
+
end
|
11
16
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: beret
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lawson Kurtz (Viget)
|
@@ -23,7 +23,10 @@ files:
|
|
23
23
|
- README.md
|
24
24
|
- beret.gemspec
|
25
25
|
- lib/beret.rb
|
26
|
-
- lib/beret/
|
26
|
+
- lib/beret/block.rb
|
27
|
+
- lib/beret/block_set.rb
|
28
|
+
- lib/beret/search.rb
|
29
|
+
- lib/beret/search_result.rb
|
27
30
|
- lib/beret/version.rb
|
28
31
|
homepage: https://github.com/vigetlabs/beret
|
29
32
|
licenses:
|
data/lib/beret/hash.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
# Ruby wrapper for Colonel Kurtz data
|
2
|
-
#
|
3
|
-
# Contents of `data` hash
|
4
|
-
#
|
5
|
-
# type:
|
6
|
-
# block type
|
7
|
-
# string, lower-cased and dashed, e.g. "hero-photo"
|
8
|
-
#
|
9
|
-
# content:
|
10
|
-
# block content
|
11
|
-
# hash
|
12
|
-
#
|
13
|
-
# blocks:
|
14
|
-
# block children
|
15
|
-
# array of `data` hashes
|
16
|
-
#
|
17
|
-
|
18
|
-
module ColonelKurtz
|
19
|
-
class Block
|
20
|
-
|
21
|
-
attr_reader :parent
|
22
|
-
|
23
|
-
def initialize(data)
|
24
|
-
@data = Data.new(data).to_hash
|
25
|
-
end
|
26
|
-
|
27
|
-
def type
|
28
|
-
@type ||= Type.new(data.fetch("type")).to_sym
|
29
|
-
end
|
30
|
-
|
31
|
-
def content
|
32
|
-
@content ||= data.fetch("content", {})
|
33
|
-
end
|
34
|
-
|
35
|
-
def parent
|
36
|
-
@parent ||= data.fetch("parent", nil)
|
37
|
-
end
|
38
|
-
|
39
|
-
def children
|
40
|
-
@children ||= blocks.map{ |data| Block.new(data.merge("parent" => self)) }
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
|
-
private
|
45
|
-
|
46
|
-
attr_reader :data
|
47
|
-
|
48
|
-
def blocks
|
49
|
-
data.fetch("blocks", [])
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|