schemaless_field 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/Guardfile +1 -1
- data/README.md +1 -1
- data/lib/railtie.rb +0 -2
- data/lib/schemaless_field.rb +0 -5
- data/lib/schemaless_field/dsl.rb +1 -1
- data/lib/schemaless_field/field.rb +3 -3
- data/lib/schemaless_field/version.rb +1 -1
- data/spec/dsl_spec.rb +48 -9
- data/spec/field_spec.rb +2 -2
- data/spec/spec_helper.rb +0 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 087c4d05bea7da10985fc022655554cdba6d4c4f
|
4
|
+
data.tar.gz: 4f453d2bf47fe64596fe6678b3fc0aad15e38fa5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a34ab39c48791c2d00677eda5e4e8b37f1ef9a60f7c820c0cde849b2b4a105cf491bce63a43e62e3dc83e09a7967e5920a2b556c9992361c21b70c7f67554b57
|
7
|
+
data.tar.gz: c05f45df128cd497062e6e4db078169b67be8e36d263376985c755c96d2fb4c332f77854e7bb96e6478e1b34b317cbb2f7672cb45860f3ab7077b94a2fa6ed0a
|
data/Gemfile
CHANGED
data/Guardfile
CHANGED
data/README.md
CHANGED
@@ -24,7 +24,7 @@ Or install it yourself as:
|
|
24
24
|
class Item < ActiveRecord::Base # Any class with a json string or hash can be used
|
25
25
|
after_initialize :set_data
|
26
26
|
|
27
|
-
|
27
|
+
schemaless_field :data do
|
28
28
|
f.field :color, '$..color' # explicit path (not required)
|
29
29
|
f.field :array # implicit path
|
30
30
|
f.field :nested_field # nested implicit path i.e: $..nested.field
|
data/lib/railtie.rb
CHANGED
data/lib/schemaless_field.rb
CHANGED
data/lib/schemaless_field/dsl.rb
CHANGED
@@ -14,20 +14,20 @@ module SchemalessField
|
|
14
14
|
mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
15
15
|
# Getter
|
16
16
|
def #{attribute}
|
17
|
-
::JsonPath.on(json__#{
|
17
|
+
::JsonPath.on(json__#{@model_attr}, '#{path}')[0]
|
18
18
|
end
|
19
19
|
|
20
20
|
# Setter
|
21
21
|
def #{attribute}=(value)
|
22
22
|
self.#{@model_attr} = ::JsonPath
|
23
|
-
.for(json__#{
|
23
|
+
.for(json__#{@model_attr})
|
24
24
|
.gsub('#{path}') { |v| value }
|
25
25
|
.to_hash
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
29
29
|
|
30
|
-
def json__#{
|
30
|
+
def json__#{@model_attr}
|
31
31
|
val = self.send(:#{@model_attr})
|
32
32
|
case val
|
33
33
|
when String
|
data/spec/dsl_spec.rb
CHANGED
@@ -6,23 +6,62 @@ class Dummy
|
|
6
6
|
|
7
7
|
def initialize
|
8
8
|
self.data = {
|
9
|
-
lannisters
|
10
|
-
geoffrey
|
11
|
-
cersei
|
12
|
-
tyrion
|
9
|
+
'lannisters' => {
|
10
|
+
'geoffrey' => 'king',
|
11
|
+
'cersei' => 'queen regent',
|
12
|
+
'tyrion' => 'master of coin'
|
13
|
+
},
|
14
|
+
'places' => [
|
15
|
+
{ 'name' => 'kings landing', 'region' => 'Crownlands' }
|
16
|
+
],
|
17
|
+
'kings' => [
|
18
|
+
'Geoffrey',
|
19
|
+
'Robert',
|
20
|
+
'Stannis',
|
21
|
+
'Renly',
|
22
|
+
'Tommen'
|
23
|
+
],
|
24
|
+
'assign' => {
|
25
|
+
'me' => nil
|
13
26
|
}
|
14
27
|
}
|
15
28
|
end
|
16
29
|
end
|
17
30
|
|
18
|
-
describe SchemalessField::DSL do
|
19
|
-
|
20
|
-
Dummy.
|
31
|
+
RSpec.describe SchemalessField::DSL do
|
32
|
+
before :all do
|
33
|
+
Dummy.schemaless_field :data do |f|
|
21
34
|
f.field :lannisters
|
35
|
+
f.field :first_king, '$..kings[0]'
|
36
|
+
f.field :lannisters_tyrion
|
37
|
+
f.field :not_real
|
38
|
+
f.field :kings_landing, '$..places[?@.name=="kings landing"].region'
|
39
|
+
f.field :assign_me
|
22
40
|
end
|
41
|
+
end
|
42
|
+
|
43
|
+
subject { Dummy.new }
|
44
|
+
|
45
|
+
it 'retreives correct values' do
|
46
|
+
expect(subject.lannisters).to eq(subject.data['lannisters'])
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'retrieves values from json path' do
|
50
|
+
expect(subject.first_king).to eq(subject.data['kings'].first)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'inferes path from name' do
|
54
|
+
expect(subject.lannisters_tyrion).to eq(subject.data['lannisters']['tyrion'])
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'returns nil for non-existant values' do
|
58
|
+
expect(subject.not_real).to be_nil
|
59
|
+
end
|
23
60
|
|
24
|
-
|
25
|
-
|
61
|
+
it 'allows you to assign values' do
|
62
|
+
subject.assign_me = '123'
|
63
|
+
expect(subject.assign_me).to eq('123')
|
64
|
+
expect(subject.data['assign']['me']).to eq('123')
|
26
65
|
end
|
27
66
|
end
|
28
67
|
|
data/spec/field_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class FieldDummy
|
4
4
|
attr_accessor :data
|
5
5
|
|
6
6
|
def initialize
|
@@ -20,7 +20,7 @@ class Dummy
|
|
20
20
|
end
|
21
21
|
|
22
22
|
describe SchemalessField::Field do
|
23
|
-
subject {
|
23
|
+
subject { FieldDummy.new }
|
24
24
|
let(:field) { described_class.new(subject.class, :data) }
|
25
25
|
|
26
26
|
before { field.field :foo, '$..foo' }
|
data/spec/spec_helper.rb
CHANGED
@@ -9,7 +9,6 @@ require 'schemaless_field'
|
|
9
9
|
#
|
10
10
|
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
11
11
|
RSpec.configure do |config|
|
12
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
13
12
|
config.run_all_when_everything_filtered = true
|
14
13
|
config.filter_run :focus
|
15
14
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schemaless_field
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stan Bondi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jsonpath
|
@@ -111,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
111
|
version: '0'
|
112
112
|
requirements: []
|
113
113
|
rubyforge_project:
|
114
|
-
rubygems_version: 2.2.
|
114
|
+
rubygems_version: 2.2.2
|
115
115
|
signing_key:
|
116
116
|
specification_version: 4
|
117
117
|
summary: Basic accessor methods for schemaless ORM fields.
|