eskimo-core 3.0.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c3955c3e689986abaa0de22b261c49a26a3b00af0c5632cfadfddcb41fe84db8
4
+ data.tar.gz: 9ca28e3abcb1d2465d997994cf6fa9177377d163067affef5983a172848fdabe
5
+ SHA512:
6
+ metadata.gz: ec6ac5e1d883fb4b6beb3560a3626743efe9f7f01a6d1f96211b26701b00face6c9bc31cb6364558a2f0ddf2a5f4d4fc7f03e56aaf5d90cea959debbb99171ca
7
+ data.tar.gz: 13a20463fac542d3043aef17ea9f3688ec5297846dda310ed1532cb2cb24efa0c510ff707e9ec0616fe2bc2e71ebfd574023952497dd75706d755d5ca7e563e6
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eskimo
4
+ module Core
5
+ # Render a component or a list of ones.
6
+ class Renderer
7
+ # @param props [Hash]
8
+ # Properties to pass to each component being rendered.
9
+ def initialize(**props)
10
+ @props = { **props, render: method(:render) }
11
+ end
12
+
13
+ # @param components [Proc]
14
+ # A block that returns components to render.
15
+ def apply(&components)
16
+ render(components)
17
+ end
18
+
19
+ private
20
+
21
+ def render(*components)
22
+ buf = +''
23
+
24
+ for component in components do
25
+ if component.is_a?(String)
26
+ buf << component
27
+ elsif component.is_a?(Array)
28
+ buf << render(*component)
29
+ elsif component.is_a?(Proc)
30
+ buf << render(component[**@props])
31
+ elsif component.respond_to?(:render)
32
+ buf << render(component.render(**@props))
33
+ elsif component
34
+ bail(component)
35
+ end
36
+ end
37
+
38
+ buf
39
+ end
40
+
41
+ def bail(component)
42
+ raise ArgumentError.new(
43
+ "Eskimo: don't know how to render #{component.class} => #{component}"
44
+ )
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eskimo
4
+ module Core
5
+ VERSION = '3.0.0.pre.1'
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ require 'eskimo/core/renderer'
2
+ require 'eskimo/core/version'
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Eskimo::Core::Renderer do
6
+ it 'renders nothing' do
7
+ expect(subject.apply).to eq('')
8
+ end
9
+
10
+ it 'renders a string' do
11
+ expect(subject.apply { 'hello' }).to eq('hello')
12
+ end
13
+
14
+ it 'renders a proc' do
15
+ expect(subject.apply { lambda do |**| 'hello' end }).to eq('hello')
16
+ end
17
+
18
+ it 'renders an array of renderables' do
19
+ expect(subject.apply { ['h','ello'] }).to eq('hello')
20
+ end
21
+
22
+ it 'renders a #render renderable' do
23
+ class Renderable
24
+ def render(**)
25
+ 'hello'
26
+ end
27
+ end
28
+
29
+ expect(subject.apply { Renderable.new }).to eq('hello')
30
+ end
31
+
32
+ it 'ignores falseys' do
33
+ expect(subject.apply { nil }).to eq('')
34
+ expect(subject.apply { false }).to eq('')
35
+ expect(subject.apply { [nil,false] }).to eq('')
36
+ end
37
+
38
+ it 'passes props to renderers' do
39
+ props = nil
40
+
41
+ described_class.new(foo: 'bar').apply {
42
+ lambda { |injected_props| ''.tap { props = injected_props } }
43
+ }
44
+
45
+ expect(props).to be_a(Hash)
46
+ expect(props).to include(foo: 'bar')
47
+ end
48
+
49
+ it 'injects a "render" routine for procs' do
50
+ expect(
51
+ subject.apply { lambda { |render:, **| render['hello'] } }
52
+ ).to eq('hello')
53
+ end
54
+
55
+ it 'bails on unrenderable inputs' do
56
+ expect {
57
+ subject.apply { :foo }
58
+ }.to raise_error(ArgumentError,
59
+ "Eskimo: don't know how to render #{:foo.class} => foo"
60
+ )
61
+ end
62
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'simplecov'
4
+
5
+ SimpleCov.start do
6
+ add_filter(%r{/spec/})
7
+ coverage_dir("#{ENV.fetch('COVERAGE_DIR', 'coverage')}")
8
+ command_name 'eskimo-core'
9
+ end
10
+
11
+ require 'eskimo/core'
12
+
13
+ RSpec.configure do |config|
14
+ config.expect_with :rspec do |expectations|
15
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
16
+ end
17
+
18
+ config.mock_with :rspec do |mocks|
19
+ mocks.verify_partial_doubles = true
20
+ end
21
+
22
+ config.shared_context_metadata_behavior = :apply_to_host_groups
23
+
24
+ config.filter_run_when_matching :focus
25
+
26
+ config.disable_monkey_patching!
27
+
28
+ config.warnings = true
29
+
30
+ config.default_formatter = 'doc' if config.files_to_run.one?
31
+
32
+ config.order = :random
33
+
34
+ Kernel.srand config.seed
35
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eskimo-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.0.pre.1
5
+ platform: ruby
6
+ authors:
7
+ - Ahmad Amireh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: simplecov
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.16'
41
+ description: Format text for humans using a declarative, extensible API.
42
+ email: ahmad@instructure.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/eskimo/core.rb
48
+ - lib/eskimo/core/renderer.rb
49
+ - lib/eskimo/core/version.rb
50
+ - spec/renderer_spec.rb
51
+ - spec/spec_helper.rb
52
+ homepage: https://github.com/amireh/eskimo
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '2.4'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">"
68
+ - !ruby/object:Gem::Version
69
+ version: 1.3.1
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.7.6
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Declarative text formatting
76
+ test_files:
77
+ - spec/spec_helper.rb
78
+ - spec/renderer_spec.rb