json_schema_tools 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MTVkNGZkMjUxMjk5YTQ0ZTg5NzFlMTg4OGU4MGJmMjQ3NWVkOGVhOQ==
4
+ MWMyMGVjNDg3M2IwYzhkMjU0N2Y3ZjY4OThjYTYxMTNkODY0MzYxZg==
5
5
  data.tar.gz: !binary |-
6
- NTFlMjliNzNkMGZiYzFmOWRjYjM3ODQ3MTBiYzQ5YjQ4ODdjN2EwYg==
6
+ NjZjYjhiMmFhZmZhOWNjYjE5MzllZWE5ODllZjQzY2VjOGU1MTRlMQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- M2UyNTA5ZWQyMjcxYzQ2YTliNjgwZGQ2MWM1MmQ0NDUzNzY0ZDNiN2M2MTg3
10
- MWE5YzZjN2VkMWI2MmVkNWE1ZGI4ZmU2OTc1YWY2ZTIwNDY0YTYwZDU0NzRh
11
- ZTMzOTVhZjUyM2VkZmE0NmY2OWVjM2RhMGVkYzVlZjg4NWI5ZWI=
9
+ MDdkNTk5MWMzMmExMDAxZTU0ZTI4MWYyZmU1OGFkZjRlZTRiODhiNDljNzJk
10
+ YWMxMjA2OTljZWI1ODBiMjY4YjE5MDc3N2JkNGNlMTM1ZWM1YzVmOGRjMWRj
11
+ ZDE2NzIzZTM0NWNjZDk2MWFmNDBiYzFhYjE4Yjg3NWZjNmJkYWM=
12
12
  data.tar.gz: !binary |-
13
- OWRhYzZiMzk1NjgzNmRkMWE1OWVjZTk4M2FhZTc3MzJkOTNlYzczOTU5OTNm
14
- MjAyOTU0NWVlZmIzYzNjYzgzY2QwNjJmZGRlZGQxMGFjZmQ3MTIwY2IyOTA2
15
- MjAyNzhjNzEyNTJmMTY5NzMzOTkzYzkzZTM1OWFiMTViYjUzNTM=
13
+ ZWI5YjZjZTU5MzUzZjliNTEwOTU2Nzc4YTRlMjlmNGMxMmYyNGJlODg1YzU4
14
+ OWU0ODJiYzc4MTBiODM5Yzk4MjYxNGIwODk0NDFkNDk3NzQ3YjYwNjFlOTE1
15
+ MTI1MjFlNGI3ZWNhMzQ5MzkzNTJhYjFhOWVhYmQxMzk5M2M2ODU=
@@ -73,13 +73,15 @@ module SchemaTools
73
73
  # c = Contact.from_hash( {'contact'=>{ "id=>"123456", "last_name"=>"Meier" }} )
74
74
  # c.id #=>123456
75
75
  #
76
- # @param [String|Hash{String=>Mixed}] json string or hash
77
- def from_hash(hash)
76
+ # @param [Hash{String=>Mixed}] json string or hash
77
+ # @param [Object] obj if you want to update an existing objects
78
+ # attributes. e.g during an update
79
+ def from_hash(hash, obj=nil)
78
80
  # test if hash is nested and shift up
79
81
  if hash.length == 1 && hash["#{schema_name}"]
80
82
  hash = hash["#{schema_name}"]
81
83
  end
82
- obj = new
84
+ obj ||= new
83
85
  hash.each do |key, val|
84
86
  next unless obj.respond_to?(key)
85
87
  # set values to raw schema attributes, even if there are no setters
@@ -16,20 +16,10 @@ module SchemaTools
16
16
  include ActiveModel::Conversion
17
17
  include ActiveModel::Validations
18
18
 
19
- #included do
20
- # validate_with_schema :schema_name
21
- #end
22
- # Runs all the validations within the specified context. Returns true if no errors are found,
23
- # false otherwise.
24
- #
25
- # If the argument is false (default is +nil+), the context is set to <tt>:create</tt> if
26
- # <tt>new_record?</tt> is true, and to <tt>:update</tt> if it is not.
27
- #
28
- # Validations with no <tt>:on</tt> option will run no matter the context. Validations with
29
- # some <tt>:on</tt> option will only run in the specified context.
30
- def valid?(context = nil)
31
- #context ||= (new_record? ? :create : :update)
32
- output = super(context)
19
+ # Runs all the validations within the specified context.
20
+ # @return [Boolean] true if no errors are found, false otherwise
21
+ def valid?
22
+ output = super
33
23
  errors.empty? && output
34
24
  end
35
25
 
@@ -42,15 +32,13 @@ module SchemaTools
42
32
  def validate_with(schema, opts={})
43
33
  reader = opts[:reader] || SchemaTools::Reader
44
34
  schema = reader.read(schema, opts[:path])
45
- # make getter / setter
35
+ # create validation methods
46
36
  schema[:properties].each do |key, val|
47
37
  validates_length_of key, validate_length_opts(val) if val['maxLength'] || val['minLength']
48
38
  validates_presence_of key if val['required'] || val['required'] == 'true'
49
- validates_numericality_of key, validate_number_opts(val) if val['type'] == 'number'
39
+ validates_numericality_of key, validate_number_opts(val) if val['type'] == 'number' || val['type'] == 'integer'
50
40
  #TODO array minItems, max unique, null, string
51
41
  # format: date-time, regex color style, email,uri, ..
52
- validates_numericality_of key, validate_number_opts(val) if val['type'] == 'number'
53
- #end
54
42
  end
55
43
  end
56
44
 
@@ -1,3 +1,3 @@
1
1
  module SchemaTools
2
- VERSION = '0.5.1'
2
+ VERSION = '0.5.2'
3
3
  end
@@ -111,7 +111,7 @@ describe SchemaTools::Modules::Attributes do
111
111
  hash = { created_at: "2014-12-06T04:30:26+01:00"}
112
112
  obj = TestClient.from_hash(hash)
113
113
  expect(obj.created_at.class).to eq Time
114
- expect(obj.created_at.zone).to eq 'CET'
114
+ # expect(obj.created_at.zone).to eq 'CET' # fails on travis-ci .. strange
115
115
  expect(obj.created_at.year).to eq 2014
116
116
 
117
117
  hash = { created_at: "2014-12-04T10:39:50.000Z"}
@@ -120,6 +120,15 @@ describe SchemaTools::Modules::Attributes do
120
120
  expect(obj.created_at.hour).to eq 10
121
121
  end
122
122
 
123
+ it 'updates an object' do
124
+ obj = TestContact.from_hash( {first_name: 'Frieda'} )
125
+ expect(obj.first_name).to eq 'Frieda'
126
+ expect(obj.last_name).to eq nil
127
+
128
+ TestContact.from_hash({first_name: 'Paul', last_name: 'Hulk'}, obj)
129
+ expect(obj.first_name).to eq 'Paul'
130
+ expect(obj.last_name).to eq 'Hulk'
131
+ end
123
132
 
124
133
  end
125
134
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schema_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Georg Leciejewski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-08 00:00:00.000000000 Z
11
+ date: 2014-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json