beret 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 080b4b65d075cdafbd2c3b47ca269e0ffb96f667
4
- data.tar.gz: 79c7eaf729ffe3f9605080eb117743db5fd700be
3
+ metadata.gz: 3a817f9ff6f03c7ae6d433570a362cfb69aa8f77
4
+ data.tar.gz: 8f13e43d494aa258a1fc6ecdf8f52ae54ff66ce9
5
5
  SHA512:
6
- metadata.gz: 80e88484b46722c3d144acc41f9659d32a818aa42332cf58c627d581f0a96ccc3e63390b8c12b6e715ebbd0f9138ffef96417dce260583f8fd7f93c303c259c7
7
- data.tar.gz: a2279ac02496949c267b43315ae24637100a982c8f4fd0877bec82f4e3dc54162fee79523db15a1623d70479be1fb320d9dda79551ab646de3496afa322163da
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
@@ -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
@@ -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
@@ -0,0 +1,16 @@
1
+ module Beret
2
+ class SearchResult
3
+
4
+ attr_reader :block, :field_name
5
+
6
+ def initialize(block, field_name)
7
+ @block = block
8
+ @field_name = field_name
9
+ end
10
+
11
+ def value
12
+ block.attributes.fetch(field_name)
13
+ end
14
+
15
+ end
16
+ end
data/lib/beret/version.rb CHANGED
@@ -2,8 +2,8 @@ module Beret
2
2
 
3
3
  VERSION = [
4
4
  0, # major
5
- 1, # minor
5
+ 2, # minor
6
6
  0 # patch
7
7
  ].join('.')
8
-
8
+
9
9
  end
data/lib/beret.rb CHANGED
@@ -1,11 +1,16 @@
1
- require "beret/version"
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/hash"
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.1.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/hash.rb
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