kalibro_client 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 (87) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/features/kalibro_configuration/all.feature +6 -6
  4. data/features/kalibro_configuration/creation.feature +5 -5
  5. data/features/kalibro_configuration/destroy.feature +6 -6
  6. data/features/kalibro_configuration/metric_configurations.feature +4 -4
  7. data/features/{range → kalibro_range}/destroy.feature +2 -2
  8. data/features/{range → kalibro_range}/exists.feature +2 -2
  9. data/features/{range → kalibro_range}/find.feature +2 -2
  10. data/features/{range → kalibro_range}/ranges_of.feature +4 -4
  11. data/features/{range → kalibro_range}/save.feature +2 -2
  12. data/features/kalibro_range/update.feature +54 -0
  13. data/features/metric_configuration/creation.feature +2 -2
  14. data/features/metric_configuration/destroy.feature +2 -2
  15. data/features/metric_configuration/find.feature +2 -2
  16. data/features/metric_configuration/metric_configurations_of.feature +8 -8
  17. data/features/metric_result/descendant_values.feature +2 -2
  18. data/features/metric_result/history_of.feature +2 -2
  19. data/features/metric_result/metric_results_of.feature +2 -2
  20. data/features/module_result/children.feature +2 -2
  21. data/features/module_result/find.feature +2 -2
  22. data/features/module_result/history_of.feature +2 -2
  23. data/features/processing/first_processing_after.feature +2 -2
  24. data/features/processing/first_processing_of.feature +2 -2
  25. data/features/processing/has_processing.feature +2 -2
  26. data/features/processing/has_processing_after.feature +2 -2
  27. data/features/processing/has_processing_before.feature +2 -2
  28. data/features/processing/has_ready_processing.feature +2 -2
  29. data/features/processing/last_processing_before.feature +2 -2
  30. data/features/processing/last_processing_of.feature +2 -2
  31. data/features/processing/last_processing_state_of.feature +2 -2
  32. data/features/processing/last_ready_processing_of.feature +2 -2
  33. data/features/processing/processing_of.feature +4 -4
  34. data/features/processing/processing_with_date_of.feature +4 -4
  35. data/features/project/update.feature +13 -0
  36. data/features/repository/all.feature +1 -1
  37. data/features/repository/cancel_processing.feature +1 -1
  38. data/features/repository/destroy.feature +1 -1
  39. data/features/repository/exists.feature +1 -1
  40. data/features/repository/find.feature +1 -1
  41. data/features/repository/of.feature +1 -1
  42. data/features/repository/process.feature +2 -2
  43. data/features/step_definitions/kalibro_configuration_steps.rb +40 -0
  44. data/features/step_definitions/metric_configuration_steps.rb +9 -9
  45. data/features/step_definitions/module_result_steps.rb +1 -1
  46. data/features/step_definitions/project_steps.rb +12 -0
  47. data/features/step_definitions/range_steps.rb +25 -1
  48. data/features/step_definitions/reading_steps.rb +1 -1
  49. data/features/step_definitions/repository_steps.rb +1 -1
  50. data/lib/kalibro_client/entities/base.rb +53 -19
  51. data/lib/kalibro_client/entities/configurations/base.rb +4 -1
  52. data/lib/kalibro_client/entities/configurations/kalibro_configuration.rb +1 -1
  53. data/lib/kalibro_client/entities/configurations/kalibro_range.rb +8 -8
  54. data/lib/kalibro_client/entities/configurations/metric_configuration.rb +7 -5
  55. data/lib/kalibro_client/entities/configurations/reading.rb +4 -4
  56. data/lib/kalibro_client/entities/configurations/reading_group.rb +4 -0
  57. data/lib/kalibro_client/entities/processor/base.rb +2 -1
  58. data/lib/kalibro_client/entities/processor/kalibro_module.rb +1 -0
  59. data/lib/kalibro_client/entities/processor/metric_collector_details.rb +7 -2
  60. data/lib/kalibro_client/entities/processor/metric_result.rb +9 -10
  61. data/lib/kalibro_client/entities/processor/module_result.rb +10 -2
  62. data/lib/kalibro_client/entities/processor/processing.rb +26 -10
  63. data/lib/kalibro_client/entities/processor/project.rb +4 -0
  64. data/lib/kalibro_client/entities/processor/repository.rb +63 -4
  65. data/lib/kalibro_client/helpers/date_attributes.rb +11 -0
  66. data/lib/kalibro_client/helpers/request_methods.rb +9 -0
  67. data/lib/kalibro_client/version.rb +1 -1
  68. data/spec/entities/base_spec.rb +106 -24
  69. data/spec/entities/configurations/kalibro_configuration_spec.rb +8 -8
  70. data/spec/entities/configurations/kalibro_range_spec.rb +18 -24
  71. data/spec/entities/configurations/metric_configuration_spec.rb +22 -23
  72. data/spec/entities/configurations/reading_group_spec.rb +27 -0
  73. data/spec/entities/configurations/reading_spec.rb +15 -7
  74. data/spec/entities/processor/metric_collector_details_spec.rb +19 -7
  75. data/spec/entities/processor/metric_result_spec.rb +40 -54
  76. data/spec/entities/processor/module_result_spec.rb +52 -3
  77. data/spec/entities/processor/processing_spec.rb +35 -92
  78. data/spec/entities/processor/project_spec.rb +52 -5
  79. data/spec/entities/processor/repository_spec.rb +255 -5
  80. data/spec/factories/kalibro_configurations.rb +3 -3
  81. data/spec/factories/kalibro_ranges.rb +9 -3
  82. data/spec/factories/metrics.rb +1 -1
  83. data/spec/factories/readings.rb +1 -1
  84. data/spec/factories/repositories.rb +1 -1
  85. data/spec/helpers/date_attributes_spec.rb +30 -0
  86. metadata +21 -14
  87. data/features/step_definitions/configuration_steps.rb +0 -40
@@ -6,9 +6,9 @@ Feature: Last processing before
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With one repository just after starting to process
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And I have a reading group with name "Group"
11
- And I have a loc configuration within the given configuration
11
+ And I have a loc configuration within the given kalibro configuration
12
12
  And the given project has the following Repositories:
13
13
  | name | scm_type | address |
14
14
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -6,9 +6,9 @@ Feature: Last processing of
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With one repository just after starting to process
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And I have a reading group with name "Group"
11
- And I have a loc configuration within the given configuration
11
+ And I have a loc configuration within the given kalibro configuration
12
12
  And the given project has the following Repositories:
13
13
  | name | scm_type | address |
14
14
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -6,9 +6,9 @@ Feature: Last processing state of
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With one repository just after starting to process
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And I have a reading group with name "Group"
11
- And I have a loc configuration within the given configuration
11
+ And I have a loc configuration within the given kalibro configuration
12
12
  And the given project has the following Repositories:
13
13
  | name | scm_type | address |
14
14
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -6,9 +6,9 @@ Feature: Last ready processing of
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With one repository just after with ready processing
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And I have a reading group with name "Group"
11
- And I have a loc configuration within the given configuration
11
+ And I have a loc configuration within the given kalibro configuration
12
12
  And the given project has the following Repositories:
13
13
  | name | scm_type | address |
14
14
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -6,9 +6,9 @@ Feature: Processing of
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With one repository just after starting to process
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And I have a reading group with name "Group"
11
- And I have a loc configuration within the given configuration
11
+ And I have a loc configuration within the given kalibro configuration
12
12
  And the given project has the following Repositories:
13
13
  | name | scm_type | address |
14
14
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -20,9 +20,9 @@ Feature: Processing of
20
20
  @kalibro_processor_restart @kalibro_configuration_restart
21
21
  Scenario: With one repository just after with ready processing
22
22
  Given I have a project with name "Kalibro"
23
- And I have a configuration with name "Java"
23
+ And I have a kalibro configuration with name "Java"
24
24
  And I have a reading group with name "Group"
25
- And I have a loc configuration within the given configuration
25
+ And I have a loc configuration within the given kalibro configuration
26
26
  And the given project has the following Repositories:
27
27
  | name | scm_type | address |
28
28
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -6,9 +6,9 @@ Feature: Processing with date of
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With one repository just after starting to process and tomorrow's date
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And I have a reading group with name "Group"
11
- And I have a loc configuration within the given configuration
11
+ And I have a loc configuration within the given kalibro configuration
12
12
  And the given project has the following Repositories:
13
13
  | name | scm_type | address |
14
14
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -20,9 +20,9 @@ Feature: Processing with date of
20
20
  @kalibro_processor_restart @kalibro_configuration_restart
21
21
  Scenario: With one repository just after starting to process and yesterday's date
22
22
  Given I have a project with name "Kalibro"
23
- And I have a configuration with name "Java"
23
+ And I have a kalibro configuration with name "Java"
24
24
  And I have a reading group with name "Group"
25
- And I have a loc configuration within the given configuration
25
+ And I have a loc configuration within the given kalibro configuration
26
26
  And the given project has the following Repositories:
27
27
  | name | scm_type | address |
28
28
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -0,0 +1,13 @@
1
+ Feature: Update
2
+ In order to be able to have projects
3
+ As a developer
4
+ I want to update projects
5
+
6
+ @kalibro_processor_restart
7
+ Scenario: Setting and resetting the name of a valid project (ensures that update is getting called instead of create)
8
+ Given I have a project with name "Kalibro"
9
+ And I set the project name to "QtCalculator"
10
+ And I save the given project
11
+ And I set the project name to "Kalibro"
12
+ And I save the given project
13
+ Then I should get true as the response
@@ -6,7 +6,7 @@ Feature: Repositories listing
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With existing project repository
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And the given project has the following Repositories:
11
11
  | name | scm_type | address |
12
12
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -6,7 +6,7 @@ Feature: Process
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With one repository
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And the given project has the following Repositories:
11
11
  | name | scm_type | address |
12
12
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -6,7 +6,7 @@ Feature: Repositories destroying
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With existing repository
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And the given project has the following Repositories:
11
11
  | name | scm_type | address |
12
12
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -6,7 +6,7 @@ Feature: Repositories listing
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With existing project repository
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And the given project has the following Repositories:
11
11
  | name | scm_type | address |
12
12
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -6,7 +6,7 @@ Feature: Repositories listing
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With existing project repository
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And the given project has the following Repositories:
11
11
  | name | scm_type | address |
12
12
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -6,7 +6,7 @@ Feature: Repositories listing
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With existing project repository
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And the given project has the following Repositories:
11
11
  | name | scm_type | address |
12
12
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -6,9 +6,9 @@ Feature: Process
6
6
  @kalibro_processor_restart @kalibro_configuration_restart
7
7
  Scenario: With one repository
8
8
  Given I have a project with name "Kalibro"
9
- And I have a configuration with name "Java"
9
+ And I have a kalibro configuration with name "Java"
10
10
  And I have a reading group with name "Group"
11
- And I have a loc configuration within the given configuration
11
+ And I have a loc configuration within the given kalibro configuration
12
12
  And the given project has the following Repositories:
13
13
  | name | scm_type | address |
14
14
  | Kalibro | GIT | https://git.gitorious.org/sbking/sbking.git |
@@ -0,0 +1,40 @@
1
+ Given(/^I have a kalibro configuration with name "(.*?)"$/) do |name|
2
+ @kalibro_configuration = FactoryGirl.create(:kalibro_configuration, {name: name})
3
+ end
4
+
5
+ When(/^I get all the kalibro configurations$/) do
6
+ @all_kalibro_configurations = KalibroClient::Entities::Configurations::KalibroConfiguration.all
7
+ end
8
+
9
+ When(/^I create the kalibro configuration with name "(.*?)"$/) do |name|
10
+ @kalibro_configuration = FactoryGirl.create(:kalibro_configuration, {name: name})
11
+ end
12
+
13
+ Then(/^I should get a list with the given kalibro configuration$/) do
14
+ expect(@all_kalibro_configurations.include?(@kalibro_configuration)).to be_truthy
15
+ end
16
+
17
+ Then(/^the kalibro configuration should exist$/) do
18
+ expect(KalibroClient::Entities::Configurations::KalibroConfiguration.exists?(@kalibro_configuration.id)).to be_truthy
19
+ end
20
+
21
+ When(/^I destroy the kalibro configuration$/) do
22
+ @kalibro_configuration.destroy
23
+ end
24
+
25
+ Then(/^the kalibro configuration should no longer exist$/) do
26
+ expect(KalibroClient::Entities::Configurations::KalibroConfiguration.exists?(@kalibro_configuration.id)).to be_falsey
27
+ end
28
+
29
+ Given(/^the kalibro configuration has a metric configuration$/) do
30
+ @metric_configuration = FactoryGirl.create(:metric_configuration, kalibro_configuration_id: @kalibro_configuration.id)
31
+ end
32
+
33
+ When(/^I list all the metric configurations of the kalibro configuration$/) do
34
+ @metric_configurations = @kalibro_configuration.metric_configurations
35
+ end
36
+
37
+ Then(/^I should get a list with the given metric configuration$/) do
38
+ expect(@metric_configurations.include?(@metric_configuration)).to be_truthy
39
+ end
40
+
@@ -1,21 +1,21 @@
1
- Given(/^I have a metric configuration within the given configuration$/) do
1
+ Given(/^I have a metric configuration within the given kalibro configuration$/) do
2
2
  @metric_configuration = FactoryGirl.create(:metric_configuration,
3
3
  {reading_group_id: @reading_group.id,
4
- kalibro_configuration_id: @configuration.id})
4
+ kalibro_configuration_id: @kalibro_configuration.id})
5
5
  end
6
6
 
7
- Given(/^I have a metric configuration within the given configuration with the given metric$/) do
7
+ Given(/^I have a metric configuration within the given kalibro configuration with the given metric$/) do
8
8
  @metric_configuration = FactoryGirl.create(:metric_configuration,
9
9
  {metric: @metric,
10
10
  reading_group_id: @reading_group.id,
11
- kalibro_configuration_id: @configuration.id})
11
+ kalibro_configuration_id: @kalibro_configuration.id})
12
12
  end
13
13
 
14
- Given(/^I have a loc configuration within the given configuration$/) do
14
+ Given(/^I have a loc configuration within the given kalibro configuration$/) do
15
15
  @metric_configuration = FactoryGirl.create(:metric_configuration,
16
16
  {metric: FactoryGirl.build(:loc),
17
17
  reading_group_id: @reading_group.id,
18
- kalibro_configuration_id: @configuration.id})
18
+ kalibro_configuration_id: @kalibro_configuration.id})
19
19
  end
20
20
 
21
21
  When(/^I search a metric configuration with the same id of the given metric configuration$/) do
@@ -32,8 +32,8 @@ When(/^I search an inexistent metric configuration$/) do
32
32
  end
33
33
  end
34
34
 
35
- When(/^I request all metric configurations of the given configuration$/) do
36
- @metric_configurations = KalibroClient::Entities::Configurations::MetricConfiguration.metric_configurations_of(@configuration.id)
35
+ When(/^I request all metric configurations of the given kalibro configuration$/) do
36
+ @metric_configurations = KalibroClient::Entities::Configurations::MetricConfiguration.metric_configurations_of(@kalibro_configuration.id)
37
37
  end
38
38
 
39
39
  Then(/^it should return the same metric configuration as the given one$/) do
@@ -53,7 +53,7 @@ When(/^I destroy the metric configuration$/) do
53
53
  end
54
54
 
55
55
  Then(/^the metric configuration should no longer exist$/) do
56
- expect { KalibroClient::Entities::Configurations::MetricConfiguration.find(@metric_configuration.id) }.to raise_error(KalibroClient::Errors::RecordNotFound)
56
+ expect { KalibroClient::Entities::Configurations::MetricConfiguration.find(@metric_configuration.id)}.to raise_error(KalibroClient::Errors::RecordNotFound)
57
57
  end
58
58
 
59
59
  Then(/^the metric configuration should exist$/) do
@@ -30,7 +30,7 @@ Given(/^I get the module result of the processing$/) do
30
30
  end
31
31
 
32
32
  When(/^I ask for the history of the given module result$/) do
33
- @history = KalibroClient::Entities::Processor::ModuleResult.history_of(@module_result.id)
33
+ @history = KalibroClient::Entities::Processor::ModuleResult.history_of(@module_result, @repository.id)
34
34
  end
35
35
 
36
36
  Then(/^I should get a list with date module results$/) do
@@ -10,6 +10,10 @@ Given(/^I have a project with name "(.*?)"$/) do |name|
10
10
  @project = FactoryGirl.create(:project, {name: name})
11
11
  end
12
12
 
13
+ Given(/^I set the project name to "(.*?)"$/) do |name|
14
+ @project.name = name
15
+ end
16
+
13
17
  When(/^I save the project$/) do
14
18
  @project.save
15
19
  end
@@ -31,6 +35,10 @@ When(/^I ask for all the projects$/) do
31
35
  @all_projects = KalibroClient::Entities::Processor::Project.all
32
36
  end
33
37
 
38
+ When(/^I save the given project$/) do
39
+ @response = @project.save
40
+ end
41
+
34
42
  Then(/^the project should exist$/) do
35
43
  expect(KalibroClient::Entities::Processor::Project.exists?(@project.id)).to be_truthy
36
44
  end
@@ -46,3 +54,7 @@ end
46
54
  Then(/^I should get a list with the given project$/) do
47
55
  expect(@all_projects).to include(@project)
48
56
  end
57
+
58
+ Then(/^I should get true as the response$/) do
59
+ expect(@response).to be_truthy
60
+ end
@@ -1,5 +1,5 @@
1
1
  Given(/^I have a range within the given reading$/) do
2
- @range = FactoryGirl.create(:range, {reading_id: @reading.id, metric_configuration_id: @metric_configuration.id})
2
+ @range = FactoryGirl.create(:range, {reading_id: @reading.id, metric_configuration_id: @metric_configuration.id, beginning: 1.1, end: 5.1})
3
3
  end
4
4
 
5
5
  Given(/^I have an unsaved range$/) do
@@ -70,3 +70,27 @@ Then(/^the range should no longer exist$/) do
70
70
  expect(KalibroClient::Entities::Configurations::KalibroRange.exists?(@range.id)).to be_falsey
71
71
  end
72
72
 
73
+ When(/^I change the "(.*?)" to "(.*?)"$/) do |attribute, value|
74
+ @range.send("#{attribute}=", value)
75
+ end
76
+
77
+ When(/^I ask to update the given range$/) do
78
+ @range.update
79
+ end
80
+
81
+ Then(/^I should not receive errors$/) do
82
+ expect(@range.kalibro_errors).to be_empty
83
+ end
84
+
85
+ Then(/^I should get the error "(.*?)"$/) do |message|
86
+ expect(@range.kalibro_errors).to include(message)
87
+ end
88
+
89
+ Given(/^I have another range within the given reading$/) do
90
+ @another_range = FactoryGirl.create(:another_range, reading_id: @reading.id, metric_configuration_id: @metric_configuration.id)
91
+ end
92
+
93
+ When(/^I change the "(.*?)" of the other range to "(.*?)"$/) do |attribute, value|
94
+ @another_range.send("#{attribute}=", value)
95
+ end
96
+
@@ -6,7 +6,7 @@ Given(/^the given reading group has the following readings:$/) do |table|
6
6
  end
7
7
 
8
8
  Given(/^I have a reading within the given reading group$/) do
9
- @reading = FactoryGirl.create(:reading, {reading_group_id: @reading_group.id})
9
+ @reading = FactoryGirl.create(:reading, reading_group_id: @reading_group.id, grade: 10.5)
10
10
  end
11
11
 
12
12
  When(/^I ask for all the readings$/) do
@@ -1,7 +1,7 @@
1
1
  Given(/^the given project has the following Repositories:$/) do |table|
2
2
  hash = table.hashes.first
3
3
  hash[:project_id] = @project.id
4
- hash[:configuration_id] = @configuration.id
4
+ hash[:kalibro_configuration_id] = @kalibro_configuration.id
5
5
 
6
6
  @repository = FactoryGirl.create(:repository, hash)
7
7
  end
@@ -21,17 +21,19 @@ require 'kalibro_client/helpers/request_methods'
21
21
  module KalibroClient
22
22
  module Entities
23
23
  class Base
24
- attr_accessor :kalibro_errors
24
+ attr_accessor :kalibro_errors, :persisted
25
25
 
26
- def initialize(attributes={})
26
+ def initialize(attributes={}, persisted=false)
27
27
  attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) }
28
28
  @kalibro_errors = []
29
+ @persisted = persisted
29
30
  end
30
31
 
31
32
  def to_hash(options={})
32
33
  hash = Hash.new
33
34
  excepts = options[:except].nil? ? [] : options[:except]
34
35
  excepts << "kalibro_errors"
36
+ excepts << "persisted"
35
37
  fields.each do |field|
36
38
  hash = field_to_hash(field).merge(hash) if !excepts.include?(field)
37
39
  end
@@ -52,7 +54,7 @@ module KalibroClient
52
54
  end
53
55
 
54
56
  def self.to_object value
55
- value.kind_of?(Hash) ? new(value) : value
57
+ value.kind_of?(Hash) ? new(value, true) : value
56
58
  end
57
59
 
58
60
  def self.to_objects_array value
@@ -61,17 +63,26 @@ module KalibroClient
61
63
  end
62
64
 
63
65
  def save
64
- begin
65
- response = self.class.request(save_action, save_params, :post, save_prefix)
66
- self.id = response[instance_class_name]["id"]
67
- self.kalibro_errors = response["kalibro_errors"] unless response["kalibro_errors"].nil?
68
- self.created_at = response[instance_class_name]["created_at"] unless response[instance_class_name]["created_at"].nil?
69
- self.updated_at = response[instance_class_name]["updated_at"] unless response[instance_class_name]["updated_at"].nil?
70
-
71
- self.kalibro_errors.empty? ? true : false
72
- rescue Exception => exception
73
- add_error exception
74
- false
66
+ if persisted?
67
+ self.update
68
+ else
69
+ begin
70
+ response = self.class.request(save_action, save_params, :post, save_prefix)
71
+
72
+ if response["errors"].nil?
73
+ self.id = response[instance_class_name]["id"]
74
+ self.created_at = response[instance_class_name]["created_at"] unless response[instance_class_name]["created_at"].nil?
75
+ self.updated_at = response[instance_class_name]["updated_at"] unless response[instance_class_name]["updated_at"].nil?
76
+ @persisted = true
77
+ true
78
+ else
79
+ self.kalibro_errors = response["errors"]
80
+ false
81
+ end
82
+ rescue Exception => exception
83
+ add_error exception
84
+ false
85
+ end
75
86
  end
76
87
  end
77
88
 
@@ -85,13 +96,24 @@ module KalibroClient
85
96
  new_model
86
97
  end
87
98
 
99
+ def update(attributes={})
100
+ attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) }
101
+ response = self.class.request(update_action, update_params, :put, update_prefix)
102
+ unless response["errors"].nil?
103
+ response["errors"].each { |error| add_error(error) }
104
+ false
105
+ else
106
+ true
107
+ end
108
+ end
109
+
88
110
  def ==(another)
89
111
  unless self.class == another.class
90
112
  return false
91
113
  end
92
114
 
93
115
  self.variable_names.each do |name|
94
- next if name == "created_at" or name == "updated_at"
116
+ next if name == "created_at" or name == "updated_at" or name == "persisted"
95
117
  unless self.send("#{name}") == another.send("#{name}") then
96
118
  return false
97
119
  end
@@ -107,7 +129,7 @@ module KalibroClient
107
129
  def self.find(id)
108
130
  if(exists?(id))
109
131
  response = request(find_action, id_params(id), :get)
110
- new response[entity_name]
132
+ new(response[entity_name], true)
111
133
  else
112
134
  raise KalibroClient::Errors::RecordNotFound
113
135
  end
@@ -115,8 +137,18 @@ module KalibroClient
115
137
 
116
138
  def destroy
117
139
  begin
118
- self.class.request(destroy_action, destroy_params, :delete, destroy_prefix)
119
- self.kalibro_errors.empty? ? true : false
140
+ response = self.class.request(destroy_action, destroy_params, :delete, destroy_prefix)
141
+
142
+ unless response['errors'].nil?
143
+ response['errors'].each { |error| add_error(error) }
144
+ end
145
+
146
+ if self.kalibro_errors.empty?
147
+ @persisted = false
148
+ true
149
+ else
150
+ false
151
+ end
120
152
  rescue Exception => exception
121
153
  add_error exception
122
154
  false
@@ -124,7 +156,7 @@ module KalibroClient
124
156
  end
125
157
 
126
158
  def self.create_objects_array_from_hash (response)
127
- create_array_from_hash(response[entity_name.pluralize]).map { |hash| new hash }
159
+ create_array_from_hash(response[entity_name.pluralize]).map { |hash| new(hash, true) }
128
160
  end
129
161
 
130
162
  def self.create_array_from_hash (response)
@@ -133,6 +165,8 @@ module KalibroClient
133
165
  response
134
166
  end
135
167
 
168
+ alias_method :persisted?, :persisted
169
+
136
170
  protected
137
171
 
138
172
  def instance_variable_names