Skip to content

Commit

Permalink
Convert to_json to as_json to follow Ruby/Rails convention (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwilken authored Nov 2, 2023
1 parent 00ca77e commit f122f0d
Show file tree
Hide file tree
Showing 19 changed files with 42 additions and 37 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

A gem for parsing, generating, and rendering TipTap Documents and Nodes using Ruby.

## Note

This gem is under active development and is changing somewhat quickly. There is a chance that there may be breaking changes until a stable version is released.

## Installation

Install the gem and add to the application's Gemfile by executing:
Expand Down Expand Up @@ -55,7 +59,7 @@ Once you have a Document with some content you can render it to HTML, JSON, and
#### JSON

```ruby
document.to_json # => { type: 'doc', content: […nodes]}
document.as_json # => { type: 'doc', content: […nodes]}
```

### HTML
Expand Down
5 changes: 3 additions & 2 deletions lib/tip_tap/json_renderable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ def include_empty_content_in_json?
end

# Generate a JSON object that is useable by the editor
def to_json
def as_json
json = {type: type_name}
json = json.merge(content: content.map(&:to_json)) if should_include_content?
json = json.merge(content: content.map(&:as_json)) if should_include_content?
json = json.merge(attrs: attrs.deep_symbolize_keys) if attrs.present?
json
end
alias_method :to_h, :as_json

private

Expand Down
2 changes: 1 addition & 1 deletion lib/tip_tap/nodes/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.from_json(json)
new(json["text"], marks: Array(json["marks"]))
end

def to_json
def as_json
{type: type_name, text: text, marks: marks.map(&:deep_symbolize_keys)}.compact_blank
end

Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON string" do
document = TipTap::Document.from_json(json_contents)
json = document.to_json
json = document.as_json

expect(json).to eq({
type: "doc",
Expand Down
6 changes: 3 additions & 3 deletions spec/tip_tap/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
end
end

describe "to_json" do
describe "as_json" do
context "when the node is a Node class" do
it "returns an only the content" do
node = TipTap::Node.new
expect(node.to_json).to eq({type: nil, content: []})
expect(node.as_json).to eq({type: nil, content: []})
end
end

Expand All @@ -59,7 +59,7 @@
klass = Class.new(TipTap::Node)
klass.type_name = "myTestNode"
node = klass.new(test: "test")
expect(node.to_json).to eq({type: "myTestNode", content: [], attrs: {test: "test"}})
expect(node.as_json).to eq({type: "myTestNode", content: [], attrs: {test: "test"}})
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/blockquote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::Blockquote.from_json(json_content)
json = node.to_json
json = node.as_json

expect(json).to eq(json_content.merge(type: "blockquote").deep_symbolize_keys)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/bullet_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::BulletList.from_json(json_contents)
json = node.to_json
json = node.as_json

expect(json).to eq(json_contents.merge(type: "bulletList").deep_symbolize_keys)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/codeblock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::Codeblock.new
json = node.to_json
json = node.as_json

expect(json).to eq({type: "codeBlock", content: []})
end
Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/hard_break_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::HardBreak.new
expect(node.to_json).to eq({type: "hardBreak"})
expect(node.as_json).to eq({type: "hardBreak"})
end
end
end
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/heading_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::Heading.new(level: 1)
json = node.to_json
json = node.as_json

expect(json).to eq({type: "heading", attrs: {level: 1}, content: []})
end
Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/horizontal_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::HorizontalRule.new
expect(node.to_json).to eq({type: "horizontalRule"})
expect(node.as_json).to eq({type: "horizontalRule"})
end
end
end
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::Image.new(src: "https://img.companycam.com/abcd1234.jpeg")
json = node.to_json
json = node.as_json

expect(json).to eq({type: "image", attrs: {src: "https://img.companycam.com/abcd1234.jpeg"}})
end
Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/list_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::ListItem.from_json(json_content)
json = node.to_json
json = node.as_json

expect(json).to eq(json_content.merge(type: "listItem").deep_symbolize_keys)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/ordered_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::OrderedList.from_json(json_contents)
json = node.to_json
json = node.as_json

expect(json).to eq(json_contents.merge(type: "orderedList").deep_symbolize_keys)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/paragraph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::Paragraph.new
json = node.to_json
json = node.as_json

expect(json).to eq({type: "paragraph", content: []})
end
Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/task_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::TaskItem.new(checked: true)
json = node.to_json
json = node.as_json

expect(json).to eq({type: "taskItem", attrs: {checked: true}, content: []})
end
Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/task_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::TaskList.new
json = node.to_json
json = node.as_json

expect(json).to eq({type: "taskList", content: []})
end
Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap/nodes/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@
end
end

describe "to_json" do
describe "as_json" do
it "returns a JSON object" do
node = TipTap::Nodes::Text.new("Hello World!", marks: [{type: "bold"}, {type: "italic"}])
json = node.to_json
json = node.as_json

expect(json).to eq({type: "text", text: "Hello World!", marks: [{type: "bold"}, {type: "italic"}]})
end
Expand Down
4 changes: 2 additions & 2 deletions spec/tip_tap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
end

it "parses the json and serializes it back to json" do
document_2 = TipTap::Document.from_json(document.to_json)
expect(document.to_json).to eq(document_2.to_json)
document_2 = TipTap::Document.from_json(document.as_json)
expect(document.as_json).to eq(document_2.as_json)
end
end
end

0 comments on commit f122f0d

Please sign in to comment.