brock 0.1.1 → 0.1.2

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.
data/lib/brock.rb CHANGED
@@ -1,18 +1,2 @@
1
1
  require 'brock/schema'
2
2
  require 'brock/version'
3
-
4
- module Brock
5
-
6
- def self.version
7
- VERSION
8
- end
9
-
10
- def self.to_html(schema, values, options={})
11
- Schema.new(schema).to_html(values)
12
- end
13
-
14
- def self.parse_params(schema, params)
15
- Schema.new(schema).parse_paras(params)
16
- end
17
-
18
- end
data/lib/brock/field.rb CHANGED
@@ -13,13 +13,16 @@ module Brock
13
13
 
14
14
  attr_reader :description
15
15
 
16
- def initialize(params = {})
17
- @type = params.fetch('type').to_sym
18
- @name = params.fetch('name').to_sym
19
- @label = params.fetch('label')
20
- @default = params.fetch('default')
16
+ def self.detect(params)
17
+ params['type'] == type.to_s
18
+ end
19
+
20
+ def self.new_from_params(params)
21
+ new(params.fetch('name'), params)
22
+ end
21
23
 
22
- @description = params['description']
24
+ def initialize(name, params = {})
25
+ @name, @params = name.to_sym, params
23
26
  end
24
27
 
25
28
  def to_html(value=nil)
@@ -27,7 +30,20 @@ module Brock
27
30
  end
28
31
 
29
32
  def parse_param(value)
30
- raise ParamParseError
33
+ raise ParamParseError.new(value)
34
+ end
35
+
36
+ def label
37
+ @params['label'] or
38
+ name.to_s.gsub(/[^[:alnum:]]+/,' ').sub(/^(.)/) {|l| l.upcase }
39
+ end
40
+
41
+ def default
42
+ @params['default']
43
+ end
44
+
45
+ def description
46
+ @params['description']
31
47
  end
32
48
 
33
49
  end
@@ -27,7 +27,7 @@ module Brock
27
27
  end
28
28
 
29
29
  def path
30
- File.join(self.class.share_path, "#{field.type}_field.erb")
30
+ File.join(self.class.share_path, "#{field.class.type}_field.erb")
31
31
  end
32
32
 
33
33
  end
@@ -3,8 +3,20 @@ require 'brock/field'
3
3
  module Brock
4
4
  class BooleanField < Field
5
5
 
6
+ def self.type
7
+ :boolean
8
+ end
9
+
10
+ def default
11
+ if @params.has_key?('default')
12
+ @params['default']
13
+ else
14
+ true
15
+ end
16
+ end
17
+
6
18
  def parse_param(value)
7
- value == '1' || value == 'true' || value == true
19
+ value == 'on' || value == '1' || value == 'true' || value == true
8
20
  end
9
21
 
10
22
  end
@@ -3,10 +3,14 @@ require 'brock/field'
3
3
  module Brock
4
4
  class IntegerField < Field
5
5
 
6
+ def self.type
7
+ :integer
8
+ end
9
+
6
10
  def parse_param(value)
7
11
  Integer(value)
8
- rescue ArgumentError
9
- raise ParamParseError.new(value)
12
+ rescue ArgumentError, TypeError
13
+ super(value)
10
14
  end
11
15
 
12
16
  end
@@ -0,0 +1,26 @@
1
+ require 'brock/field'
2
+
3
+ module Brock
4
+ class SelectField < Field
5
+
6
+ def self.type
7
+ :select
8
+ end
9
+
10
+ def self.detect(field)
11
+ super(field) or (
12
+ field['values'] and
13
+ (field['type'] == 'string' or not field.has_key?('type'))
14
+ )
15
+ end
16
+
17
+ def parse_param(value)
18
+ value.to_s
19
+ end
20
+
21
+ def values
22
+ @params['values'] || []
23
+ end
24
+
25
+ end
26
+ end
@@ -3,6 +3,14 @@ require 'brock/field'
3
3
  module Brock
4
4
  class StringField < Field
5
5
 
6
+ def self.type
7
+ :string
8
+ end
9
+
10
+ def self.detect(field)
11
+ super(field) or not field.has_key?('type')
12
+ end
13
+
6
14
  def parse_param(value)
7
15
  value.to_s
8
16
  end
@@ -0,0 +1,15 @@
1
+ require 'brock/field'
2
+
3
+ module Brock
4
+ class TextField < Field
5
+
6
+ def self.type
7
+ :text
8
+ end
9
+
10
+ def parse_param(value)
11
+ value.to_s
12
+ end
13
+
14
+ end
15
+ end
data/lib/brock/schema.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'brock/fields/boolean_field'
2
2
  require 'brock/fields/integer_field'
3
+ require 'brock/fields/select_field'
3
4
  require 'brock/fields/string_field'
5
+ require 'brock/fields/text_field'
4
6
 
5
7
  module Brock
6
8
 
@@ -11,36 +13,36 @@ module Brock
11
13
 
12
14
  attr_reader :fields
13
15
 
14
- def self.field_for_type(type)
15
- case type
16
- when 'boolean'
17
- BooleanField
18
- when 'integer'
19
- IntegerField
20
- when 'string'
21
- StringField
16
+ FIELD_TYPES = [
17
+ IntegerField,
18
+ BooleanField,
19
+ TextField,
20
+ SelectField,
21
+ StringField
22
+ ]
23
+
24
+ def self.field_for(raw)
25
+ klass = FIELD_TYPES.detect {|type| type.detect(raw) }
26
+ if klass
27
+ klass.new_from_params(raw)
22
28
  else
23
- raise FieldTypeNotSupported
29
+ raise FieldTypeNotSupported.new(raw)
24
30
  end
25
31
  end
26
32
 
27
33
  def initialize(raw_fields)
28
- @fields = raw_fields.map do |raw_field|
29
- klass = self.class.field_for_type(raw_field.fetch('type'))
30
- klass.new(raw_field)
31
- end
34
+ @fields = raw_fields.map {|raw_field| self.class.field_for(raw_field) }
32
35
  end
33
36
 
34
37
  def to_html(values = {})
35
38
  fields.map do |field|
36
- field.to_html(values[field.name])
39
+ field.to_html(values[field.name.to_s])
37
40
  end.join("\n")
38
41
  end
39
42
 
40
43
  def parse_params(params)
41
- params.each_with_object({}) do |(name, value), settings|
42
- field = field_with_name(name)
43
- settings[field.name] = field.parse_param(value)
44
+ fields.each_with_object({}) do |field, settings|
45
+ settings[field.name] = field.parse_param(params[field.name])
44
46
  end
45
47
  end
46
48
 
data/lib/brock/version.rb CHANGED
@@ -1,3 +1,7 @@
1
1
  module Brock
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
+
4
+ def self.version
5
+ VERSION
6
+ end
3
7
  end
@@ -4,10 +4,11 @@
4
4
  <input class="brock-field"
5
5
  type="checkbox"
6
6
  name="brock[<%= name %>]"
7
- <% if value || default %>checked<% end %>
7
+ <% if value || (value.nil? and default) %>
8
+ checked
9
+ <% end %>
8
10
  />
9
11
  <%= label %>
10
12
  </label>
11
13
  </div>
12
14
  </div>
13
-
@@ -6,7 +6,9 @@
6
6
  <input class="brock-field"
7
7
  type="number"
8
8
  name="brock[<%= name %>]"
9
- placeholder="<%= default %>"
9
+ <% if value %>
10
+ placeholder="<%= default %>"
11
+ <% end %>
10
12
  <% if value %>
11
13
  value="<%= value %>"
12
14
  <% end %>
@@ -0,0 +1,24 @@
1
+ <div class="control-group brock-field brock-field-select">
2
+ <div class="control-label">
3
+ <%= label %>
4
+ </div>
5
+
6
+ <div class="controls">
7
+ <% values.each do |v| %>
8
+ <label class="radio brock-label">
9
+ <input class="brock-field"
10
+ type="radio"
11
+ name="brock[<%= name %>]"
12
+ value="<%= v['value'] %>"
13
+ <% if value == v['value'] or (not value and v['value'] == default) %>
14
+ checked
15
+ <% end %>
16
+ />
17
+ <%= v['label'] %>
18
+ <% if v['description'] %>
19
+ <p class="help-block"><%= v['description'] %></p>
20
+ <% end %>
21
+ </label>
22
+ <% end %>
23
+ </div>
24
+ </div>
@@ -4,7 +4,9 @@
4
4
  <input class="brock-field"
5
5
  type="text"
6
6
  name="brock[<%= name %>]"
7
- placeholder="<%= default %>"
7
+ <% if default %>
8
+ placeholder="<%= default %>"
9
+ <% end %>
8
10
  <% if value %>
9
11
  value="<%= value %>"
10
12
  <% end %>
@@ -0,0 +1,15 @@
1
+ <div class="control-group brock-field brock-field-text">
2
+ <label class="control-label brock-label"><%= label %></label>
3
+ <div class="controls">
4
+ <textarea class="brock-field"
5
+ name="brock[<%= name %>]"
6
+ <% if default %>
7
+ placeholder="<%= default %>"
8
+ <% end %>
9
+ /><% if value %><%= value %><% end %></textarea>
10
+
11
+ <% if description %>
12
+ <p class="help-block brock-description"><%= description %></p>
13
+ <% end %>
14
+ </div>
15
+ </div>
@@ -23,7 +23,7 @@ describe Brock::FieldTemplate do
23
23
  end
24
24
 
25
25
  it "figures out the correct path" do
26
- field.stub(type: 'string')
26
+ field.stub(class: stub(type: 'string'))
27
27
  subject.class.stub(:share_path) { '/' }
28
28
  expect(subject.path).to eq('/string_field.erb')
29
29
  end
@@ -3,21 +3,34 @@ require 'brock/fields/boolean_field'
3
3
 
4
4
  describe Brock::BooleanField do
5
5
 
6
- subject {
7
- described_class.new(
8
- 'type' => 'boolean',
9
- 'name' => 'team_balance',
10
- 'label' => 'Auto team balance',
11
- 'default' => true
12
- )
13
- }
14
-
15
- it "#to_html works" do
16
- expect(subject.to_html(false)).to be_similar_to_fixture('boolean_field.html')
6
+ subject { described_class.new(:espresso) }
7
+
8
+ it_behaves_like "a field"
9
+
10
+ it "asdf" do
11
+
12
+ expect(subject.to_html(true)).to match(/checked/)
13
+
14
+ end
15
+
16
+ describe ".detect" do
17
+
18
+ it "picks fields with type boolean" do
19
+ expect(described_class.detect('type' => 'boolean'))
20
+ end
21
+
22
+ it "picks fields with boolean defaults" do
23
+ expect(described_class.detect('default' => true))
24
+ end
25
+
17
26
  end
18
27
 
19
28
  describe "#parse_param" do
20
29
 
30
+ it "works with on" do
31
+ expect(subject.parse_param('on')).to eq(true)
32
+ end
33
+
21
34
  it "works with 1" do
22
35
  expect(subject.parse_param('1')).to eq(true)
23
36
  end
@@ -31,4 +44,18 @@ describe Brock::BooleanField do
31
44
  end
32
45
 
33
46
  end
47
+
48
+ describe "#default" do
49
+
50
+ it "can be set in the constructor" do
51
+ subject = described_class.new(:latte, 'default' => false)
52
+ expect(subject.default).to be_false
53
+ end
54
+
55
+ it "defaults to true" do
56
+ expect(subject.default)
57
+ end
58
+
59
+ end
60
+
34
61
  end
@@ -3,18 +3,9 @@ require 'brock/fields/integer_field'
3
3
 
4
4
  describe Brock::IntegerField do
5
5
 
6
- subject {
7
- described_class.new(
8
- 'type' => 'integer',
9
- 'name' => 'rounds',
10
- 'label' => 'Rounds',
11
- 'default' => 10
12
- )
13
- }
14
-
15
- it "#to_html works" do
16
- expect(subject.to_html(5)).to be_similar_to_fixture('integer_field.html')
17
- end
6
+ subject { described_class.new(:espresso) }
7
+
8
+ it_behaves_like "a field"
18
9
 
19
10
  describe "#parse_param" do
20
11
 
@@ -33,4 +24,9 @@ describe Brock::IntegerField do
33
24
  end
34
25
 
35
26
  end
27
+
28
+ it "#default is optional" do
29
+ expect(subject.default).to eq(nil)
30
+ end
31
+
36
32
  end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'brock/fields/select_field'
3
+
4
+ describe Brock::SelectField do
5
+
6
+ subject {
7
+ described_class.new(:espresso, 'values' => [
8
+ {'value' => 0, 'label' => 'First', 'description' => 'The first'},
9
+ {'value' => 1, 'label' => 'Second', 'description' => 'The second'}
10
+ ])
11
+ }
12
+
13
+ it_behaves_like "a field"
14
+
15
+ describe ".detect" do
16
+
17
+ it "is true for fields without a type and just a value" do
18
+ expect(described_class.detect("type" => "string", "values" => []))
19
+ end
20
+
21
+ it "is true for fields without a type and just a value" do
22
+ expect(described_class.detect("values" => []))
23
+ end
24
+
25
+ end
26
+
27
+ describe "#parse_param" do
28
+
29
+ it "works with string" do
30
+ expect(subject.parse_param('foo')).to eq('foo')
31
+ end
32
+
33
+ it "calls to_s on other objects" do
34
+ obj = stub(to_s: 'sentinal')
35
+ expect(subject.parse_param(obj)).to eq('sentinal')
36
+ end
37
+
38
+ end
39
+
40
+ it "#default is optional" do
41
+ expect(subject.default).to eq(nil)
42
+ end
43
+
44
+ end
@@ -3,17 +3,16 @@ require 'brock/fields/string_field'
3
3
 
4
4
  describe Brock::StringField do
5
5
 
6
- subject {
7
- described_class.new(
8
- 'type' => 'string',
9
- 'name' => 'first_name',
10
- 'label' => 'First name',
11
- 'default' => 'Chris'
12
- )
13
- }
14
-
15
- it "#to_html works" do
16
- expect(subject.to_html(5)).to be_similar_to_fixture('string_field.html')
6
+ subject { described_class.new(:espresso) }
7
+
8
+ it_behaves_like "a field"
9
+
10
+ describe ".detect" do
11
+
12
+ it "is true for fields without a type" do
13
+ expect(described_class.detect({}))
14
+ end
15
+
17
16
  end
18
17
 
19
18
  describe "#parse_param" do
@@ -28,4 +27,9 @@ describe Brock::StringField do
28
27
  end
29
28
 
30
29
  end
30
+
31
+ it "#default is optional" do
32
+ expect(subject.default).to eq(nil)
33
+ end
34
+
31
35
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require 'brock/fields/text_field'
3
+
4
+ describe Brock::TextField do
5
+
6
+ subject {
7
+ described_class.new(:espresso)
8
+ }
9
+
10
+ it_behaves_like "a field"
11
+
12
+ describe "#parse_param" do
13
+
14
+ it "works with string" do
15
+ expect(subject.parse_param('foo')).to eq('foo')
16
+ end
17
+
18
+ end
19
+
20
+ it "#default is optional" do
21
+ expect(subject.default).to eq(nil)
22
+ end
23
+
24
+ end
@@ -16,6 +16,10 @@ describe Brock::Schema do
16
16
  "type" => "boolean",
17
17
  "label" => "Auto Team Balance",
18
18
  "default" => true
19
+ }, {
20
+ "name" => "first_name",
21
+ "label" => "First name",
22
+ "default" => "Chris"
19
23
  }
20
24
  ]
21
25
  }
@@ -26,5 +30,15 @@ describe Brock::Schema do
26
30
  expect { subject }.to_not raise_error(Brock::FieldTypeNotSupported)
27
31
  end
28
32
 
33
+ {
34
+ {"name" => "a"} => Brock::StringField,
35
+ {"name" => "a", "values" => []} => Brock::SelectField,
36
+ {"name" => "a", "type" => "string", "values" => []} => Brock::SelectField
37
+ }.each do |raw, klass|
38
+ it ".field_for matches a shortcut to #{klass}" do
39
+ described_class.field_for(raw).should be_a(klass)
40
+ end
41
+ end
42
+
29
43
  end
30
44
 
@@ -1,8 +1,8 @@
1
1
  <div class="control-group brock-field brock-field-boolean">
2
2
  <div class="controls checkbox">
3
3
  <label class="control-label brock-label">
4
- <input class="brock-field" type="checkbox" name="brock[team_balance]" checked />
5
- Auto team balance
4
+ <input class="brock-field" type="checkbox" name="brock[espresso]" checked />
5
+ Espresso
6
6
  </label>
7
7
  </div>
8
8
  </div>
@@ -1,6 +1,6 @@
1
1
  <div class="control-group brock-field brock-field-integer">
2
- <label class="control-label brock-label">Rounds</label>
2
+ <label class="control-label brock-label">Espresso</label>
3
3
  <div class="controls">
4
- <input class="brock-field" type="number" name="brock[rounds]" placeholder="10" value="5" />
4
+ <input class="brock-field" type="number" name="brock[espresso]" />
5
5
  </div>
6
6
  </div>
@@ -0,0 +1,20 @@
1
+ <div class="control-group brock-field brock-field-select">
2
+ <div class="control-label">
3
+ Espresso
4
+ </div>
5
+
6
+ <div class="controls">
7
+ <label class="radio brock-label">
8
+ <input class="brock-field" type="radio" name="brock[espresso]" value="0" />
9
+ First
10
+
11
+ <p class="help-block">The first</p>
12
+ </label>
13
+ <label class="radio brock-label">
14
+ <input class="brock-field" type="radio" name="brock[espresso]" value="1" />
15
+ Second
16
+
17
+ <p class="help-block">The second</p>
18
+ </label>
19
+ </div>
20
+ </div>
@@ -1,6 +1,6 @@
1
1
  <div class="control-group brock-field brock-field-string">
2
- <label class="control-label brock-label">First name</label>
2
+ <label class="control-label brock-label">Espresso</label>
3
3
  <div class="controls">
4
- <input class="brock-field" type="text" name="brock[first_name]" placeholder="Chris" value="5" />
4
+ <input class="brock-field" type="text" name="brock[espresso]" />
5
5
  </div>
6
6
  </div>
@@ -0,0 +1,6 @@
1
+ <div class="control-group brock-field brock-field-text">
2
+ <label class="control-label brock-label">Espresso</label>
3
+ <div class="controls">
4
+ <textarea class="brock-field" name="brock[espresso]" /></textarea>
5
+ </div>
6
+ </div>
data/spec/spec_helper.rb CHANGED
@@ -1,22 +1,4 @@
1
- module FixtureHelpers
2
-
3
- def fixture(path)
4
- File.read(File.join(File.dirname(__FILE__), 'fixtures', path))
5
- end
6
-
7
- end
8
-
9
- RSpec::Matchers.define :be_similar_to_fixture do |fixture_path|
10
-
11
- def scrub(str)
12
- str.gsub(/\s+/, ' ')
13
- end
14
-
15
- match do |actual|
16
- scrub(actual) == scrub(fixture(fixture_path))
17
- end
18
-
19
- end
1
+ Dir["./spec/support/**/*.rb"].sort.each {|f| require f }
20
2
 
21
3
  RSpec.configure do |config|
22
4
  config.include(FixtureHelpers)
@@ -0,0 +1,35 @@
1
+ require 'brock/field'
2
+
3
+ shared_examples "a field" do
4
+
5
+ let(:default_fixture) { "#{described_class.type}_field.html" }
6
+
7
+ it "is a Brock::Field" do
8
+ expect(subject).to be_a(Brock::Field)
9
+ end
10
+
11
+ it ".type is a symbol" do
12
+ expect(described_class.type).to be_a(Symbol)
13
+ end
14
+
15
+ it '.detect with same type' do
16
+ expect(described_class.detect('type' => described_class.type.to_s))
17
+ end
18
+
19
+ it "#name is a symbol" do
20
+ expect(subject.name).to be_a(Symbol)
21
+ end
22
+
23
+ it "#label is optional" do
24
+ subject.instance_variable_set('@label', nil)
25
+ subject.stub(name: 'mp_tag')
26
+ expect(subject.label).to eq('Mp tag')
27
+ end
28
+
29
+ it "#to_html matches the fixture" do
30
+ scrubbed_html = scrub_html(subject.to_html)
31
+ scrubbed_fixture = scrub_html(fixture(default_fixture))
32
+ expect(scrubbed_html).to eq(scrubbed_fixture)
33
+ end
34
+
35
+ end
@@ -0,0 +1,11 @@
1
+ module FixtureHelpers
2
+
3
+ def scrub_html(str)
4
+ str.gsub(/\s+/, ' ')
5
+ end
6
+
7
+ def fixture(filename)
8
+ File.read(File.join(File.expand_path('../../fixtures', __FILE__), filename))
9
+ end
10
+
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-15 00:00:00.000000000 Z
12
+ date: 2013-02-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -58,24 +58,33 @@ files:
58
58
  - lib/brock/field_template_context.rb
59
59
  - lib/brock/fields/boolean_field.rb
60
60
  - lib/brock/fields/integer_field.rb
61
+ - lib/brock/fields/select_field.rb
61
62
  - lib/brock/fields/string_field.rb
63
+ - lib/brock/fields/text_field.rb
62
64
  - lib/brock/schema.rb
63
65
  - lib/brock/version.rb
64
66
  - lib/brock.rb
65
67
  - share/boolean_field.erb
66
68
  - share/integer_field.erb
69
+ - share/select_field.erb
67
70
  - share/string_field.erb
68
- - spec/brock/field_spec.rb
71
+ - share/text_field.erb
69
72
  - spec/brock/field_template_context_spec.rb
70
73
  - spec/brock/field_template_spec.rb
71
74
  - spec/brock/fields/boolean_field_spec.rb
72
75
  - spec/brock/fields/integer_field_spec.rb
76
+ - spec/brock/fields/select_field_spec.rb
73
77
  - spec/brock/fields/string_field_spec.rb
78
+ - spec/brock/fields/text_field_spec.rb
74
79
  - spec/brock/schema_spec.rb
75
80
  - spec/fixtures/boolean_field.html
76
81
  - spec/fixtures/integer_field.html
82
+ - spec/fixtures/select_field.html
77
83
  - spec/fixtures/string_field.html
84
+ - spec/fixtures/text_field.html
78
85
  - spec/spec_helper.rb
86
+ - spec/support/field_spec.rb
87
+ - spec/support/fixture_helpers.rb
79
88
  homepage: https://minefold.com
80
89
  licenses: []
81
90
  post_install_message:
@@ -1,29 +0,0 @@
1
- require 'spec_helper'
2
- require 'brock/field'
3
-
4
- describe Brock::Field do
5
-
6
- subject {
7
- described_class.new(
8
- 'type' => 'str',
9
- 'name' => 'a',
10
- 'label' => 'A',
11
- 'default' => 'a'
12
- )
13
- }
14
-
15
- it "#type is a symbol" do
16
- expect(subject.type).to be_a(Symbol)
17
- end
18
-
19
- it "#name is a symbol" do
20
- expect(subject.type).to be_a(Symbol)
21
- end
22
-
23
- it "#parse_param raises by default" do
24
- expect {
25
- subject.parse_param(nil)
26
- }.to raise_error(Brock::ParamParseError)
27
- end
28
-
29
- end