pr 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.rspec +1 -1
  5. data/features/active_model_compatibility.feature +0 -1
  6. data/features/building_forms_from_hashes.feature +57 -0
  7. data/features/building_forms_from_models.feature +50 -0
  8. data/features/building_models_from_form.feature +66 -0
  9. data/features/defining_a_form.feature +36 -0
  10. data/features/step_definitions/active_model_steps.rb +1 -1
  11. data/features/step_definitions/form_steps.rb +23 -0
  12. data/features/step_definitions/hash_steps.rb +15 -0
  13. data/features/step_definitions/model_steps.rb +15 -0
  14. data/features/support/dsl.rb +39 -0
  15. data/lib/pr.rb +10 -0
  16. data/lib/pr/build_from_hash.rb +19 -0
  17. data/lib/pr/build_from_model.rb +20 -0
  18. data/lib/pr/build_model.rb +20 -18
  19. data/lib/pr/define_and_register.rb +28 -0
  20. data/lib/pr/define_field.rb +36 -12
  21. data/lib/pr/field_registry.rb +29 -0
  22. data/lib/pr/fields.rb +31 -0
  23. data/lib/pr/fields/boolean_field.rb +3 -2
  24. data/lib/pr/fields/date_field.rb +3 -2
  25. data/lib/pr/fields/float_field.rb +3 -2
  26. data/lib/pr/fields/generic_field.rb +3 -2
  27. data/lib/pr/fields/string_field.rb +3 -2
  28. data/lib/pr/form.rb +18 -3
  29. data/lib/pr/version.rb +1 -1
  30. data/spec/unit/pr/active_form_spec.rb +12 -3
  31. data/spec/unit/pr/build_from_hash_spec.rb +14 -0
  32. data/spec/unit/pr/build_from_model_spec.rb +27 -0
  33. data/spec/unit/pr/build_model_spec.rb +7 -11
  34. data/spec/unit/pr/define_and_register_spec.rb +93 -0
  35. data/spec/unit/pr/define_field_spec.rb +9 -0
  36. data/spec/unit/pr/field_registry_spec.rb +45 -0
  37. data/spec/unit/pr/fields/boolean_field_spec.rb +11 -3
  38. data/spec/unit/pr/fields/date_field_spec.rb +13 -5
  39. data/spec/unit/pr/fields/float_field_spec.rb +13 -5
  40. data/spec/unit/pr/fields/generic_field_spec.rb +11 -3
  41. data/spec/unit/pr/fields/string_field_spec.rb +11 -3
  42. data/spec/unit/pr/fields_spec.rb +26 -0
  43. data/spec/unit/pr/form_spec.rb +13 -3
  44. metadata +32 -2
  45. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 679887e96c55e4c6b5c183d1557cd1880c6fb9e2
4
- data.tar.gz: baea6db819b904d1a60d32e3db9459a96b6d2211
3
+ metadata.gz: d1967110cefbe5a002dd832abb9e60df5257bd16
4
+ data.tar.gz: 66f1fc2292f655b8203599056b0e43fe737d19d5
5
5
  SHA512:
6
- metadata.gz: deed03062a6e2b737c0c461cf0031f1b2eb849454038fc07b09eadbab8feac28f96d1e74c043e7d31d519bbcc0a090d0789aebcf5c5facf50140feae14241e89
7
- data.tar.gz: 32df338de6e44e53fb6fdf5741c77dc8ff310c1a4ba81fc89047964c689e700afc64f2cdb78c7280b9303ac8c8ff63053427fc1734f27d9b9ae83c338fd0958d
6
+ metadata.gz: 58681a9064ea976b0edb67b0077e2e72411cc5b83aca8936a4dc99d47b3f0869803550881bd4ae3de235bc2c2c11e417c4472acf706499552cca1c953561e962
7
+ data.tar.gz: b0ac34db4e729c275a082fee6e3f4706edf23fa6581d3c00a085f836826330c541c45674cfc4f33ee3b2e6dd4b955983fd6aff5544acefcd7f9b8ac1c776b4a7
Binary file
data.tar.gz.sig CHANGED
Binary file
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
1
  --colour
2
- --format documentation
2
+ --format progress
@@ -16,5 +16,4 @@ Feature: Active Model compatibility
16
16
  """Ruby
17
17
  MyForm.new
18
18
  """
19
-
20
19
  Then it should pass
@@ -0,0 +1,57 @@
1
+ Feature: Building a form from a hash
2
+ In order to turn user input into a validated set
3
+ As a developer
4
+ I wish to use a defined form to parse user input from a hash
5
+
6
+ @form @basic @hash
7
+ Scenario: Basic Form
8
+ Given I have a form defined as:
9
+ """Ruby
10
+ class MyForm
11
+ include PR::Form
12
+
13
+ field :event_name, :string
14
+ field :start_date, :date
15
+
16
+ end
17
+ """
18
+ And I have a hash:
19
+ """Ruby
20
+ {
21
+ "event_name" => "Mien Event",
22
+ "start_date" => "31/03/2013"
23
+ }
24
+ """
25
+ When I build my form from the hash:
26
+ """Ruby
27
+ @form = MyForm.new
28
+ PR::BuildFromHash[ @form, @hash ]
29
+ """
30
+ Then my form should have a event_name of "Mien Event"
31
+ Then my form should have a start_date of "31/03/2013"
32
+
33
+ @form @macro @hash
34
+ Scenario: Basic Form built using macro
35
+ Given I have a form defined as:
36
+ """Ruby
37
+ class MyForm
38
+ include PR::Form
39
+
40
+ field :event_name, :string
41
+ field :start_date, :date
42
+
43
+ end
44
+ """
45
+ And I have a hash:
46
+ """Ruby
47
+ {
48
+ "event_name" => "Mien Event",
49
+ "start_date" => "31/03/2013"
50
+ }
51
+ """
52
+ When I build my form using the macro:
53
+ """Ruby
54
+ @form = MyForm.from_hash @hash
55
+ """
56
+ Then my form should have a event_name of "Mien Event"
57
+ Then my form should have a start_date of "31/03/2013"
@@ -0,0 +1,50 @@
1
+ Feature: Building a form from an existing models
2
+ In order to apply user input to an existing model
3
+ As a developer
4
+ I wish to use a defined form to display existing data
5
+
6
+ @form @basic @model
7
+ Scenario: Basic Form
8
+ Given I have a form defined as:
9
+ """Ruby
10
+ class MyForm
11
+ include PR::Form
12
+
13
+ field :event_name, :string
14
+ field :start_date, :date
15
+
16
+ end
17
+ """
18
+ And I have a model defined as:
19
+ """Ruby
20
+ @model = Struct.new(:event_name,:start_date).new 'An Event', Date.civil(2013,2,28)
21
+ """
22
+ When I build my form from a model:
23
+ """Ruby
24
+ @form = PR::BuildFromModel[ MyForm, @model ]
25
+ """
26
+ Then my form should have a event_name of "An Event"
27
+ Then my form should have a start_date of "28/02/2013"
28
+
29
+ @form @macro @model
30
+ Scenario: Basic Form built using macro
31
+ Given I have a form defined as:
32
+ """Ruby
33
+ class MyForm
34
+ include PR::Form
35
+
36
+ field :event_name, :string
37
+ field :start_date, :date
38
+
39
+ end
40
+ """
41
+ And I have a model defined as:
42
+ """Ruby
43
+ @model = Struct.new(:event_name,:start_date).new 'An Event', Date.civil(2013,2,28)
44
+ """
45
+ When I build my form using the macro:
46
+ """Ruby
47
+ @form = MyForm.from_model @model
48
+ """
49
+ Then my form should have a event_name of "An Event"
50
+ Then my form should have a start_date of "28/02/2013"
@@ -0,0 +1,66 @@
1
+ Feature: Building a model from a form
2
+ In order to turn user input into a model
3
+ As a developer
4
+ I wish to user a defined form to build a model
5
+
6
+ @form @basic @build
7
+ Scenario: Basic Form
8
+ Given I have a form defined as:
9
+ """Ruby
10
+ class MyForm
11
+ include PR::Form
12
+
13
+ field :event_name, :string
14
+ field :start_date, :date
15
+
16
+ end
17
+ """
18
+ And I have preloaded the form with data:
19
+ """Ruby
20
+ @form = MyForm.new
21
+ @form.event_name = 'Event'
22
+ @form.start_date = '31/03/2013'
23
+ """
24
+ And I have a model defined as:
25
+ """Ruby
26
+ class Event < Struct.new(:event_name,:start_date)
27
+ end
28
+ """
29
+ When I build my model from the form:
30
+ """Ruby
31
+ @model = Event.new
32
+ PR::BuildModel[ @form, @model ]
33
+ """
34
+ Then my model should have a string for event_name "Event"
35
+ And my model should have a date for start_date `Date.civil(2013,3,31)`
36
+
37
+
38
+ @form @macro @build
39
+ Scenario: Basic Form using macro
40
+ Given I have a form defined as:
41
+ """Ruby
42
+ class MyForm
43
+ include PR::Form
44
+
45
+ field :event_name, :string
46
+ field :start_date, :date
47
+
48
+ end
49
+ """
50
+ And I have preloaded the form with data:
51
+ """Ruby
52
+ @form = MyForm.new
53
+ @form.event_name = 'Event'
54
+ @form.start_date = '31/03/2013'
55
+ """
56
+ And I have a model defined as:
57
+ """Ruby
58
+ class AnEvent < Struct.new(:event_name,:start_date)
59
+ end
60
+ """
61
+ When I build my model from the form:
62
+ """Ruby
63
+ @form.build_model (@model = AnEvent.new)
64
+ """
65
+ Then my model should have a string for event_name "Event"
66
+ And my model should have a date for start_date `Date.civil(2013,3,31)`
@@ -0,0 +1,36 @@
1
+ Feature: Defining a basic form
2
+ In order to utilise some user input
3
+ As a developer
4
+ I wish to define a basic form to capture input
5
+
6
+ @form @basic
7
+ Scenario: Basic Form
8
+ Given I have a form defined as:
9
+ """Ruby
10
+ class MyForm
11
+
12
+ DefineField[ self, :event_name, PR::Fields::StringField ]
13
+ DefineField[ self, :start_date, PR::Fields::DateField ]
14
+
15
+ end
16
+ """
17
+ When I initialise MyForm
18
+ Then my form should have a string input "event_name"
19
+ And my form should have a date input "start_date"
20
+
21
+
22
+ @form
23
+ Scenario: Form (with macros)
24
+ Given I have a form defined as:
25
+ """Ruby
26
+ class MyForm
27
+ include PR::Form
28
+
29
+ field :event_name, :string
30
+ field :start_date, :date
31
+
32
+ end
33
+ """
34
+ When I initialise MyForm
35
+ Then my form should have a string input "event_name"
36
+ And my form should have a date input "start_date"
@@ -19,7 +19,7 @@ class TestActiveModelCompliance < Test::Unit::TestCase
19
19
 
20
20
  end
21
21
  TEST
22
- run_simple unescape 'ruby -Itest test_active_model_compliance.rb'
22
+ run_simple 'ruby -Itest test_active_model_compliance.rb', true, 5
23
23
  end
24
24
 
25
25
  Then "it should pass" do
@@ -0,0 +1,23 @@
1
+ Given "I have a form defined as:" do |code|
2
+ define_form code
3
+ end
4
+
5
+ Given "I have preloaded the form with data:" do |code|
6
+ run_test code
7
+ end
8
+
9
+ When "I build my form from a model:" do |code|
10
+ run_test code
11
+ end
12
+
13
+ When /^I initialise (\w+)$/ do |klass|
14
+ @form = fetch(klass).new
15
+ end
16
+
17
+ Then /^my form should have a string input "(\w+)"$/ do |name|
18
+ set @form, name, "my demo string"
19
+ end
20
+
21
+ Then /^my form should have a date input "(\w+)"$/ do |name|
22
+ set @form, name, "31/03/2013"
23
+ end
@@ -0,0 +1,15 @@
1
+ Given "I have a hash:" do |code|
2
+ define_hash code
3
+ end
4
+
5
+ Given "I build my form using the macro:" do |code|
6
+ run_test code
7
+ end
8
+
9
+ When "I build my form from the hash:" do |code|
10
+ run_test code
11
+ end
12
+
13
+ Then /^my form should have a ([\w_]+) of "(.*)"$/ do |field_name,field_data|
14
+ access("@form").send(field_name).should == field_data
15
+ end
@@ -0,0 +1,15 @@
1
+ Given "I have a model defined as:" do |code|
2
+ define_model code
3
+ end
4
+
5
+ When "I build my model from the form:" do |code|
6
+ run_test code
7
+ end
8
+
9
+ Then /^my model should have a string for (\w+) "(\w+)"$/ do |field_name,field_data|
10
+ access("@model").send(field_name).should == field_data
11
+ end
12
+
13
+ Then /^my model should have a date for ([^\s]+) `(.*)`$/ do |field_name,field_data|
14
+ access("@model").send(field_name).should == eval(field_data)
15
+ end
@@ -0,0 +1,39 @@
1
+ module DSL
2
+
3
+ class World
4
+ require 'pr'
5
+
6
+ def run code
7
+ eval code
8
+ end
9
+ end
10
+
11
+
12
+ def run_test code
13
+ world.run code
14
+ end
15
+ alias define_form run_test
16
+ alias define_model run_test
17
+
18
+ def define_hash code
19
+ world.instance_variable_set '@hash', world.run(code)
20
+ end
21
+
22
+ def fetch klass
23
+ world.class.const_get klass
24
+ end
25
+
26
+ def access var
27
+ world.instance_variable_get var
28
+ end
29
+
30
+ def world
31
+ @world ||= Class.new(World).new
32
+ end
33
+
34
+ def set thing, attribute, value
35
+ thing.send "#{attribute}=", value
36
+ end
37
+
38
+ end
39
+ World DSL
data/lib/pr.rb CHANGED
@@ -4,5 +4,15 @@ module PR
4
4
  end
5
5
 
6
6
  require 'pr/fields'
7
+
8
+ #how we define fields
9
+ require 'pr/field_registry'
10
+ require 'pr/define_field'
11
+ require 'pr/define_and_register'
12
+
13
+ require 'pr/build_model'
14
+ require 'pr/build_from_hash'
15
+ require 'pr/build_from_model'
16
+
7
17
  require 'pr/form'
8
18
  require 'pr/active_form' if defined?(Rails)
@@ -0,0 +1,19 @@
1
+ module PR
2
+ class BuildFromHash
3
+
4
+ def self.[] form, hash
5
+ new(form).from hash
6
+ end
7
+
8
+ def initialize form
9
+ @form = form
10
+ end
11
+
12
+ def from hash
13
+ hash.each_pair do |key,value|
14
+ @form.send "#{key}=", value
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module PR
2
+ class BuildFromModel
3
+
4
+ def self.[] klass, model
5
+ new(PR::FieldRegistry[klass], klass.new).build_from model
6
+ end
7
+
8
+ def initialize registry, form
9
+ @registry, @form = registry, form
10
+ end
11
+
12
+ def build_from model
13
+ @registry.fields.each do |field|
14
+ @form.send("__#{field}").populate model.send(field) if model.respond_to? field
15
+ end
16
+ @form
17
+ end
18
+
19
+ end
20
+ end
@@ -1,24 +1,26 @@
1
- class BuildModel
2
- def self.[] form, model
3
- new(PR::FieldRegistry).for form, model
4
- end
5
-
6
- def initialize registry
7
- @registry = registry
8
- end
9
-
10
- def for form, model
11
- @registry.fields_for(form) do |field_name|
12
- set model, field_name, value(form,field_name)
1
+ module PR
2
+ class BuildModel
3
+ def self.[] form, model
4
+ new(PR::FieldRegistry[form.class]).for form, model
13
5
  end
14
- end
15
6
 
16
- private
17
- def set thing, name, value
18
- thing.send "#{name}=", value
7
+ def initialize registry
8
+ @registry = registry
19
9
  end
20
- def value thing, name
21
- thing.send("__#{name}").convert
10
+
11
+ def for form, model
12
+ @registry.fields.each do |field_name|
13
+ set model, field_name, value(form, field_name)
14
+ end
22
15
  end
23
16
 
17
+ private
18
+ def set thing, name, value
19
+ thing.send "#{name}=", value
20
+ end
21
+ def value thing, name
22
+ thing.send("__#{name}").convert
23
+ end
24
+
25
+ end
24
26
  end
@@ -0,0 +1,28 @@
1
+ module PR
2
+ class DefineAndRegister
3
+
4
+ class << self
5
+ def [] klass, name, type, options = {}
6
+ new(klass).define_and_register name, type, options
7
+ end
8
+ end
9
+
10
+ def initialize klass
11
+ @registry = FieldRegistry[klass]
12
+ @klass = klass
13
+ end
14
+
15
+ def define_and_register name, type, options
16
+ register name, define(name,type,options)
17
+ end
18
+
19
+ def register name, field
20
+ @registry.register name, field
21
+ end
22
+
23
+ def define name, type, options = {}
24
+ DefineField[ @klass, name, Fields[type], options ]
25
+ end
26
+
27
+ end
28
+ end
@@ -1,21 +1,21 @@
1
1
  class DefineField
2
+ class << self
3
+ def on klass, field_name, field, options = {}
4
+ new.on klass, field_name, field, options
5
+ end
6
+ def [] klass, field_name, field, options = {}
7
+ new.on klass, field_name, field, options
8
+ end
9
+ end
2
10
 
3
- def on klass, field_name, field
11
+ def on klass, field_name, field, options = {}
4
12
  var_name = "@field_#{field_name}"
5
13
  field_setter = "#{field_name}="
6
14
  actual_field = "__#{field_name}"
7
15
 
8
- klass.class_eval do
9
- define_method(actual_field) do
10
- instance_variable_get(var_name) || send(field_setter,nil)
11
- end
12
- define_method(field_name) do
13
- send(actual_field).raw
14
- end
15
- define_method(field_setter) do |value|
16
- instance_variable_set var_name, field.new(*([value].compact))
17
- end
18
- end
16
+ define_field klass, actual_field, var_name, field_setter
17
+ define_reader klass, field_name, actual_field
18
+ define_setter klass, var_name, field_setter, field
19
19
  end
20
20
 
21
21
  private
@@ -23,4 +23,28 @@ class DefineField
23
23
  "@field_#{name}"
24
24
  end
25
25
 
26
+ def define_field klass, actual_field, var_name, field_setter
27
+ klass.class_eval do
28
+ define_method(actual_field) do
29
+ instance_variable_get(var_name) || send(field_setter,nil)
30
+ end
31
+ end
32
+ end
33
+
34
+ def define_reader klass, field_name, actual_field
35
+ klass.class_eval do
36
+ define_method(field_name) do
37
+ send(actual_field).raw
38
+ end
39
+ end
40
+ end
41
+
42
+ def define_setter klass, var_name, field_setter, field
43
+ klass.class_eval do
44
+ define_method(field_setter) do |value|
45
+ instance_variable_set var_name, field.new(*([value].compact))
46
+ end
47
+ end
48
+ end
49
+
26
50
  end
@@ -0,0 +1,29 @@
1
+ module PR
2
+ class FieldRegistry
3
+
4
+ def self.[] klass
5
+ @registers ||= {}
6
+ @registers[klass] ||= new(klass)
7
+ end
8
+
9
+ def initialize klass
10
+ @for = klass
11
+ @fields = {}
12
+ end
13
+ attr_reader :for
14
+
15
+ def register field_name, field
16
+ @fields[field_name] = field
17
+ end
18
+
19
+ def fetch field_name
20
+ @fields.fetch field_name
21
+ end
22
+
23
+ def fields
24
+ @fields.keys
25
+ end
26
+
27
+
28
+ end
29
+ end
@@ -4,3 +4,34 @@ require 'pr/fields/string_field'
4
4
  require 'pr/fields/boolean_field'
5
5
  require 'pr/fields/date_field'
6
6
  require 'pr/fields/float_field'
7
+
8
+ module PR
9
+ module Fields
10
+
11
+ PreDefined =
12
+ {
13
+ generic: GenericField,
14
+ string: StringField,
15
+ boolean: BooleanField,
16
+ date: DateField,
17
+ float: FloatField
18
+ }
19
+
20
+ class << self
21
+
22
+ def [] name
23
+ __fields[name]
24
+ end
25
+
26
+ def []= custom_name, field
27
+ __fields[custom_name] = field
28
+ end
29
+
30
+ def __fields
31
+ @__fields ||= PreDefined.dup
32
+ end
33
+ private :__fields
34
+
35
+ end
36
+ end
37
+ end
@@ -4,9 +4,10 @@ module PR
4
4
  TrueValues = [1,"1","true",true]
5
5
  FalseValues = [0,"0","false",false, ""]
6
6
 
7
- def initialize value = nil
8
- @value = value
7
+ def initialize value = nil, options = {}
8
+ @value, @options = value, options
9
9
  end
10
+ attr_reader :options
10
11
 
11
12
  def populate value
12
13
  @value = value
@@ -4,9 +4,10 @@ module PR
4
4
  class DateField
5
5
  Format = '%d/%m/%Y'
6
6
 
7
- def initialize value = ''
8
- @value = value
7
+ def initialize value = '', options = {}
8
+ @value, @options = value, options
9
9
  end
10
+ attr_reader :options
10
11
 
11
12
  def raw
12
13
  @value
@@ -2,9 +2,10 @@ module PR
2
2
  module Fields
3
3
  class FloatField
4
4
 
5
- def initialize value = ''
6
- @value = value
5
+ def initialize value = '', options = {}
6
+ @value, @options = value, options
7
7
  end
8
+ attr_reader :options
8
9
 
9
10
  def raw
10
11
  @value
@@ -2,9 +2,10 @@ module PR
2
2
  module Fields
3
3
  class GenericField
4
4
 
5
- def initialize value = nil
6
- @value = value
5
+ def initialize value = nil, options = {}
6
+ @value, @options = value, options
7
7
  end
8
+ attr_reader :options
8
9
 
9
10
  def raw
10
11
  @value
@@ -2,9 +2,10 @@ module PR
2
2
  module Fields
3
3
  class StringField
4
4
 
5
- def initialize value = ''
6
- @value = value
5
+ def initialize value = '', options = {}
6
+ @value, @options = value, options
7
7
  end
8
+ attr_reader :options
8
9
 
9
10
  def raw
10
11
  @value
@@ -2,12 +2,27 @@ module PR
2
2
  module Form
3
3
 
4
4
  def self.included other
5
- other.send :extend, DSL
5
+ other.send :extend, DSL
6
+ other.send :include, Macros
6
7
  end
7
8
 
8
9
  module DSL
9
- def field name, options = {}
10
- DefineAndRegister[ self, name, options ]
10
+ def field name, type, options = {}
11
+ PR::DefineAndRegister[ self, name, type, options ]
12
+ end
13
+ def from_hash hash
14
+ form = new
15
+ PR::BuildFromHash[ form, hash ]
16
+ form
17
+ end
18
+ def from_model model
19
+ PR::BuildFromModel[ self, model ]
20
+ end
21
+ end
22
+
23
+ module Macros
24
+ def build_model model
25
+ BuildModel[ self, model ]
11
26
  end
12
27
  end
13
28
 
@@ -1,3 +1,3 @@
1
1
  module PR
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -7,11 +7,20 @@ describe 'the PR Form' do
7
7
  let(:form_klass) { TestForm }
8
8
 
9
9
  describe 'when included extends the other with its DSL' do
10
+ let(:name) { double "field name" }
11
+ let(:type) { double "field type" }
12
+
13
+ before do
14
+ stub_const 'PR::DefineAndRegister', double
15
+ end
10
16
 
11
17
  it 'sets up field to define fields' do
12
- stub_const 'DefineAndRegister', double
13
- DefineAndRegister.should_receive(:[]).with form_klass, :name, my: :options
14
- form_klass.field :name, my: :options
18
+ PR::DefineAndRegister.should_receive(:[]).with form_klass, name, type, {}
19
+ form_klass.field name, type
20
+ end
21
+ it 'sets up field to define fields and passes in options' do
22
+ PR::DefineAndRegister.should_receive(:[]).with form_klass, name, type, my: :option
23
+ form_klass.field name, type, my: :option
15
24
  end
16
25
 
17
26
  it 'sets up an id' do
@@ -0,0 +1,14 @@
1
+ require 'pr/build_from_hash'
2
+
3
+ describe 'building a form from a hash' do
4
+ let(:form) { Struct.new(:a,:b).new }
5
+ let(:hash) { { "a" => "a", "b" => "b" } }
6
+
7
+ let(:builder) { PR::BuildFromHash.new form }
8
+
9
+ it 'sets hash values on form' do
10
+ builder.from hash
11
+ expect(form.a).to eq "a"
12
+ expect(form.b).to eq "b"
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ require 'pr/build_from_model'
2
+
3
+ describe 'building forms from models' do
4
+
5
+ let(:registry) { double "field registry", fields: [:a,:b] }
6
+ let(:form) { double "form", __a: field_a, __b: field_b }
7
+ let(:field_a) { double "field a", populate: nil }
8
+ let(:field_b) { double "field b" }
9
+ let(:model) { Struct.new(:a).new 'a' }
10
+
11
+ let(:builder) { PR::BuildFromModel.new(registry, form) }
12
+
13
+ it 'gets fields from the registry' do
14
+ registry.should_receive(:fields)
15
+ builder.build_from model
16
+ end
17
+
18
+ it 'populates the form values from the model when it has them' do
19
+ field_a.should_receive(:populate).with('a')
20
+ builder.build_from model
21
+ end
22
+
23
+ it 'returns the form' do
24
+ expect( builder.build_from model ).to eq form
25
+ end
26
+
27
+ end
@@ -2,20 +2,16 @@ require 'pr/build_model'
2
2
 
3
3
  describe 'building a model from a form' do
4
4
  let(:model) { Struct.new(:a,:b).new }
5
- let(:registry) { double }
5
+ let(:registry) { double fields: [:a,:b] }
6
6
 
7
- let(:field_a) { double "field", convert: "a_value" }
8
- let(:field_b) { double "field", convert: "b_value" }
9
- let(:form) { double "form", __a: field_a, __b: field_b }
7
+ let(:field_a) { double "field", convert: "a_value" }
8
+ let(:field_b) { double "field", convert: "b_value" }
9
+ let(:form) { double "form", __a: field_a, __b: field_b }
10
10
 
11
- let(:builder) { BuildModel.new registry }
11
+ let(:builder) { PR::BuildModel.new registry }
12
12
 
13
- before do
14
- registry.stub(:fields_for).and_yield(:a).and_yield(:b)
15
- end
16
-
17
- it 'fetches fields from the registry' do
18
- registry.should_receive(:fields_for).with(form)
13
+ it 'gets the fields' do
14
+ registry.should_receive(:fields)
19
15
  builder.for form, model
20
16
  end
21
17
  it 'converts fields to values' do
@@ -0,0 +1,93 @@
1
+ require 'pr/define_and_register'
2
+
3
+ describe 'defining and registering fields' do
4
+
5
+ #values
6
+ let(:form_klass) { double "form klass" }
7
+ let(:field_name) { double "field name" }
8
+ let(:field_type) { double "field type" }
9
+ let(:field_klass) { double "field klass" }
10
+ let(:registry) { double "registry", register: true }
11
+ let(:field) { double "field" }
12
+ let(:options) { Hash.new }
13
+
14
+ #services
15
+ let(:field_registry) { double "registry", :[] => registry }
16
+ let(:field_lookup) { double "fields", :[] => field_klass }
17
+ let(:field_definition) { double "definition", :[] => field }
18
+
19
+ before do
20
+ stub_const 'PR::FieldRegistry', field_registry
21
+ stub_const 'PR::Fields', field_lookup
22
+ stub_const 'PR::DefineField', field_definition
23
+ end
24
+
25
+ describe 'using the macro' do
26
+ it 'looks up the field registry for this form klass' do
27
+ PR::FieldRegistry.should_receive(:[]).with(form_klass).and_return(registry)
28
+ PR::DefineAndRegister[ form_klass, field_name, field_type ]
29
+ end
30
+
31
+ it 'looks up the the field for this type' do
32
+ PR::Fields.should_receive(:[]).with(field_type)
33
+ PR::DefineAndRegister[ form_klass, field_name, field_type ]
34
+ end
35
+
36
+ it 'defines a field on this form klass' do
37
+ PR::DefineField.should_receive(:[]).with( form_klass, field_name, field_klass, {} )
38
+ PR::DefineAndRegister[ form_klass, field_name, field_type ]
39
+ end
40
+
41
+ it 'registers this definition' do
42
+ registry.should_receive(:register).with(field_name,field)
43
+ PR::DefineAndRegister[ form_klass, field_name, field_type ]
44
+ end
45
+
46
+ context 'with options' do
47
+ it 'defines a field on this form klass' do
48
+ PR::DefineField.should_receive(:[]).with( form_klass, field_name, field_klass, options )
49
+ PR::DefineAndRegister[ form_klass, field_name, field_type, options ]
50
+ end
51
+ end
52
+ end
53
+
54
+ describe 'registering field' do
55
+ let(:definition) { PR::DefineAndRegister.new form_klass }
56
+
57
+ it 'looks up the field registry for this form klass' do
58
+ PR::FieldRegistry.should_receive(:[]).with(form_klass).and_return(registry)
59
+ definition.register field_name, field
60
+ end
61
+
62
+ it 'registers this definition' do
63
+ registry.should_receive(:register).with(field_name,field)
64
+ definition.register field_name, field
65
+ end
66
+ end
67
+
68
+ describe 'defining field' do
69
+ let(:definition) { PR::DefineAndRegister.new form_klass }
70
+
71
+ it 'looks up the field registry for this form klass' do
72
+ PR::FieldRegistry.should_receive(:[]).with(form_klass).and_return(registry)
73
+ definition.define field_name, field_type
74
+ end
75
+
76
+ it 'looks up the the field for this type' do
77
+ PR::Fields.should_receive(:[]).with(field_type)
78
+ definition.define field_name, field_type
79
+ end
80
+
81
+ it 'defines a field on this form klass' do
82
+ PR::DefineField.should_receive(:[]).with( form_klass, field_name, field_klass, {} )
83
+ definition.define field_name, field_type
84
+ end
85
+
86
+ context 'with options' do
87
+ it 'defines a field on this form klass' do
88
+ PR::DefineField.should_receive(:[]).with( form_klass, field_name, field_klass, options )
89
+ definition.define field_name, field_type, options
90
+ end
91
+ end
92
+ end
93
+ end
@@ -12,13 +12,22 @@ describe 'defining a field on a form' do
12
12
  define_field.on form_klass, :name, field
13
13
  expect(form.name).to eq field.new.raw
14
14
  end
15
+
15
16
  it 'defines a writer which will set the field value' do
16
17
  define_field.on form_klass, :name, field
17
18
  form.name = new_raw_value
18
19
  expect(form.name).to eq new_raw_value
19
20
  end
21
+
20
22
  it 'defines a reader to return the actual field' do
21
23
  define_field.on form_klass, :name, field
22
24
  expect(form.__name).to be_a field
23
25
  end
26
+
27
+ it 'takes options and passes them to field' do
28
+ options = Hash.new
29
+ define_field.on form_klass, :name, field, options
30
+ expect(form.__name.options).to eq options
31
+ end
32
+
24
33
  end
@@ -0,0 +1,45 @@
1
+ require 'pr/field_registry'
2
+
3
+ describe 'field registry' do
4
+ let(:registry) { PR::FieldRegistry }
5
+
6
+ #values
7
+ let(:klass) { double "form klass" }
8
+
9
+ describe '.[]' do
10
+ it 'returns a field registry for the klass' do
11
+ expect(registry[klass]).to be_a registry
12
+ expect(registry[klass].for).to eq klass
13
+ end
14
+ it 'returns the same field registry multiple times' do
15
+ expect(registry[klass]).to eq registry[klass]
16
+ end
17
+ end
18
+
19
+ describe '#initialize' do
20
+ it 'stores the klass for retrieval' do
21
+ expect(registry.new(klass).for).to eq klass
22
+ end
23
+ end
24
+
25
+ describe 'registeration' do
26
+ let(:name) { double "field name" }
27
+ let(:field) { double "field" }
28
+
29
+ let(:specific_register) { registry.new klass }
30
+
31
+ it 'stores the field on the register' do
32
+ specific_register.register name, field
33
+ end
34
+
35
+ it 'allows retreival of that field' do
36
+ specific_register.register name, field
37
+ expect(specific_register.fetch name).to eq field
38
+ end
39
+
40
+ it 'enumerates fields' do
41
+ specific_register.register name, field
42
+ expect(specific_register.fields).to eq [name]
43
+ end
44
+ end
45
+ end
@@ -1,15 +1,23 @@
1
1
  require 'pr/fields/boolean_field'
2
2
 
3
3
  describe 'a boolean field' do
4
- let(:klass) { PR::Fields::BooleanField }
5
- let(:value) { double "value" }
6
- let(:field) { klass.new value }
4
+ let(:klass) { PR::Fields::BooleanField }
5
+ let(:value) { double "value" }
6
+ let(:options) { Hash.new }
7
+ let(:field) { klass.new value, options }
7
8
 
8
9
  describe "default value" do
9
10
  subject { klass.new }
10
11
 
11
12
  its(:raw) { should equal(false) }
12
13
  its(:convert) { should equal(false) }
14
+ its(:options) { should == {} }
15
+ end
16
+
17
+ describe '#options' do
18
+ it 'has retrievable options' do
19
+ expect(field.options).to eq options
20
+ end
13
21
  end
14
22
 
15
23
  describe :raw do
@@ -2,15 +2,23 @@ require 'pr/fields/invalid_value'
2
2
  require 'pr/fields/date_field'
3
3
 
4
4
  describe 'a date field' do
5
- let(:klass) { PR::Fields::DateField }
6
- let(:value) { double "value" }
7
- let(:field) { klass.new value }
5
+ let(:klass) { PR::Fields::DateField }
6
+ let(:value) { double "value" }
7
+ let(:options) { Hash.new }
8
+ let(:field) { klass.new value, options }
8
9
 
9
10
  describe "default value" do
10
11
  subject { klass.new }
11
12
 
12
- its(:raw) { should == '' }
13
- specify { expect { subject.convert }.to raise_error PR::Fields::InvalidValue }
13
+ its(:raw) { should == '' }
14
+ specify { expect { subject.convert }.to raise_error PR::Fields::InvalidValue }
15
+ its(:options) { should == {} }
16
+ end
17
+
18
+ describe '#options' do
19
+ it 'has retrievable options' do
20
+ expect(field.options).to eq options
21
+ end
14
22
  end
15
23
 
16
24
  describe "raw" do
@@ -2,13 +2,21 @@ require 'pr/fields/invalid_value'
2
2
  require 'pr/fields/float_field'
3
3
 
4
4
  describe 'a float field' do
5
- let(:klass) { PR::Fields::FloatField }
6
- let(:value) { double "value" }
7
- let(:field) { klass.new value }
5
+ let(:klass) { PR::Fields::FloatField }
6
+ let(:value) { double "value" }
7
+ let(:options) { Hash.new }
8
+ let(:field) { klass.new value, options }
8
9
 
9
10
  describe "default value" do
10
- subject { klass.new }
11
- its(:raw) { should == '' }
11
+ subject { klass.new }
12
+ its(:raw) { should == '' }
13
+ its(:options) { should == {} }
14
+ end
15
+
16
+ describe '#options' do
17
+ it 'has retrievable options' do
18
+ expect(field.options).to eq options
19
+ end
12
20
  end
13
21
 
14
22
  describe "#raw" do
@@ -1,15 +1,23 @@
1
1
  require 'pr/fields/generic_field'
2
2
 
3
3
  describe 'a generic field' do
4
- let(:klass) { PR::Fields::GenericField }
5
- let(:value) { double "value" }
6
- let(:field) { klass.new value }
4
+ let(:klass) { PR::Fields::GenericField }
5
+ let(:value) { double "value" }
6
+ let(:options) { Hash.new }
7
+ let(:field) { klass.new value, options }
7
8
 
8
9
  describe "default value" do
9
10
  subject { klass.new }
10
11
 
11
12
  its(:raw) { should == nil }
12
13
  its(:convert) { should == nil }
14
+ its(:options) { should == {} }
15
+ end
16
+
17
+ describe '#options' do
18
+ it 'has retrievable options' do
19
+ expect(field.options).to eq options
20
+ end
13
21
  end
14
22
 
15
23
  describe :raw do
@@ -1,15 +1,23 @@
1
1
  require 'pr/fields/string_field'
2
2
 
3
3
  describe 'a string field' do
4
- let(:klass) { PR::Fields::StringField }
5
- let(:value) { double "value" }
6
- let(:field) { klass.new value }
4
+ let(:klass) { PR::Fields::StringField }
5
+ let(:value) { double "value" }
6
+ let(:options) { Hash.new }
7
+ let(:field) { klass.new value, options }
7
8
 
8
9
  describe "default value" do
9
10
  subject { klass.new }
10
11
 
11
12
  its(:raw) { should == '' }
12
13
  its(:convert) { should == '' }
14
+ its(:options) { should == {} }
15
+ end
16
+
17
+ describe '#options' do
18
+ it 'has retrievable options' do
19
+ expect(field.options).to eq options
20
+ end
13
21
  end
14
22
 
15
23
  describe "#populate" do
@@ -0,0 +1,26 @@
1
+ require 'pr/fields'
2
+
3
+ describe 'accessing fields' do
4
+
5
+ let(:lookup) { PR::Fields }
6
+
7
+ describe 'finding fields via constant' do
8
+ subject { lookup }
9
+
10
+ its([:generic]) { should eq PR::Fields::GenericField }
11
+ its([:string]) { should eq PR::Fields::StringField }
12
+ its([:boolean]) { should eq PR::Fields::BooleanField }
13
+ its([:date]) { should eq PR::Fields::DateField }
14
+ its([:float]) { should eq PR::Fields::FloatField }
15
+ end
16
+
17
+ describe 'extending with custom fields' do
18
+ let(:my_custom_field) { double "my custom field" }
19
+
20
+ it 'allows extension with a custom field' do
21
+ lookup[:custom] = my_custom_field
22
+ expect(lookup[:custom]).to eq my_custom_field
23
+ end
24
+ end
25
+
26
+ end
@@ -8,10 +8,20 @@ describe 'the PR Form' do
8
8
  end
9
9
 
10
10
  describe 'when included extends the other with its DSL' do
11
+ let(:name) { double "field name" }
12
+ let(:type) { double "field type" }
13
+
14
+ before do
15
+ stub_const 'PR::DefineAndRegister', double
16
+ end
17
+
11
18
  it 'sets up field to define fields' do
12
- stub_const 'DefineAndRegister', double
13
- DefineAndRegister.should_receive(:[]).with form_klass, :name, my: :options
14
- form_klass.field :name, my: :options
19
+ PR::DefineAndRegister.should_receive(:[]).with form_klass, name, type, {}
20
+ form_klass.field name, type
21
+ end
22
+ it 'sets up field to define fields and passes in options' do
23
+ PR::DefineAndRegister.should_receive(:[]).with form_klass, name, type, my: :option
24
+ form_klass.field name, type, my: :option
15
25
  end
16
26
  end
17
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Rowe
@@ -29,7 +29,7 @@ cert_chain:
29
29
  2feWfO4gCNmvfFjULOAYHq9JHEjN5SLSXvj5HdSnDcCyIfJKn5Ya3JahWQaWIsXf
30
30
  /NPE/mB57TOwj+d7XUa2NC4HUadF8R51IYEcIB0PpIEzJlKtfXFcOZxO
31
31
  -----END CERTIFICATE-----
32
- date: 2013-03-31 00:00:00.000000000 Z
32
+ date: 2013-04-10 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: rake
@@ -115,12 +115,24 @@ files:
115
115
  - README.md
116
116
  - Rakefile
117
117
  - features/active_model_compatibility.feature
118
+ - features/building_forms_from_hashes.feature
119
+ - features/building_forms_from_models.feature
120
+ - features/building_models_from_form.feature
121
+ - features/defining_a_form.feature
118
122
  - features/step_definitions/active_model_steps.rb
123
+ - features/step_definitions/form_steps.rb
124
+ - features/step_definitions/hash_steps.rb
125
+ - features/step_definitions/model_steps.rb
119
126
  - features/support/aruba.rb
127
+ - features/support/dsl.rb
120
128
  - lib/pr.rb
121
129
  - lib/pr/active_form.rb
130
+ - lib/pr/build_from_hash.rb
131
+ - lib/pr/build_from_model.rb
122
132
  - lib/pr/build_model.rb
133
+ - lib/pr/define_and_register.rb
123
134
  - lib/pr/define_field.rb
135
+ - lib/pr/field_registry.rb
124
136
  - lib/pr/fields.rb
125
137
  - lib/pr/fields/boolean_field.rb
126
138
  - lib/pr/fields/date_field.rb
@@ -133,13 +145,18 @@ files:
133
145
  - pr.gemspec
134
146
  - spec/spec_helper.rb
135
147
  - spec/unit/pr/active_form_spec.rb
148
+ - spec/unit/pr/build_from_hash_spec.rb
149
+ - spec/unit/pr/build_from_model_spec.rb
136
150
  - spec/unit/pr/build_model_spec.rb
151
+ - spec/unit/pr/define_and_register_spec.rb
137
152
  - spec/unit/pr/define_field_spec.rb
153
+ - spec/unit/pr/field_registry_spec.rb
138
154
  - spec/unit/pr/fields/boolean_field_spec.rb
139
155
  - spec/unit/pr/fields/date_field_spec.rb
140
156
  - spec/unit/pr/fields/float_field_spec.rb
141
157
  - spec/unit/pr/fields/generic_field_spec.rb
142
158
  - spec/unit/pr/fields/string_field_spec.rb
159
+ - spec/unit/pr/fields_spec.rb
143
160
  - spec/unit/pr/form_spec.rb
144
161
  homepage: https://github.com/JonRowe/PR
145
162
  licenses: []
@@ -166,15 +183,28 @@ specification_version: 4
166
183
  summary: Public Relations tools for Rails
167
184
  test_files:
168
185
  - features/active_model_compatibility.feature
186
+ - features/building_forms_from_hashes.feature
187
+ - features/building_forms_from_models.feature
188
+ - features/building_models_from_form.feature
189
+ - features/defining_a_form.feature
169
190
  - features/step_definitions/active_model_steps.rb
191
+ - features/step_definitions/form_steps.rb
192
+ - features/step_definitions/hash_steps.rb
193
+ - features/step_definitions/model_steps.rb
170
194
  - features/support/aruba.rb
195
+ - features/support/dsl.rb
171
196
  - spec/spec_helper.rb
172
197
  - spec/unit/pr/active_form_spec.rb
198
+ - spec/unit/pr/build_from_hash_spec.rb
199
+ - spec/unit/pr/build_from_model_spec.rb
173
200
  - spec/unit/pr/build_model_spec.rb
201
+ - spec/unit/pr/define_and_register_spec.rb
174
202
  - spec/unit/pr/define_field_spec.rb
203
+ - spec/unit/pr/field_registry_spec.rb
175
204
  - spec/unit/pr/fields/boolean_field_spec.rb
176
205
  - spec/unit/pr/fields/date_field_spec.rb
177
206
  - spec/unit/pr/fields/float_field_spec.rb
178
207
  - spec/unit/pr/fields/generic_field_spec.rb
179
208
  - spec/unit/pr/fields/string_field_spec.rb
209
+ - spec/unit/pr/fields_spec.rb
180
210
  - spec/unit/pr/form_spec.rb
metadata.gz.sig CHANGED
Binary file