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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8b3c03ab5b0b69e0e0a0f6011cb10706c8bf26e
4
- data.tar.gz: 311c1a1ac4b348bd4e388228072c2f6f5003eb55
3
+ metadata.gz: 087c4d05bea7da10985fc022655554cdba6d4c4f
4
+ data.tar.gz: 4f453d2bf47fe64596fe6678b3fc0aad15e38fa5
5
5
  SHA512:
6
- metadata.gz: ab5ebe356a01bedacca2b83e1e815556394ef1d6d5fc75c8309a59bef8a67b95aa845d6dd516f2f3ed9b79af7aa314cc7f2fef530a7effefd69363e587f80857
7
- data.tar.gz: ca75e51789c70af6fcd87635351c403cf9ed9d75ef1f24ad06f83ae4965f399a0de92e6a477b6f5f0a01d5467e8921799ea9966c6bb03ee11663c2f7d1da0124
6
+ metadata.gz: a34ab39c48791c2d00677eda5e4e8b37f1ef9a60f7c820c0cde849b2b4a105cf491bce63a43e62e3dc83e09a7967e5920a2b556c9992361c21b70c7f67554b57
7
+ data.tar.gz: c05f45df128cd497062e6e4db078169b67be8e36d263376985c755c96d2fb4c332f77854e7bb96e6478e1b34b317cbb2f7672cb45860f3ab7077b94a2fa6ed0a
data/Gemfile CHANGED
@@ -7,4 +7,6 @@ group :development, :test do
7
7
  gem 'guard-rspec'
8
8
  gem 'guard-rubocop'
9
9
  gem 'guard-bundler'
10
+
11
+ gem 'byebug'
10
12
  end
data/Guardfile CHANGED
@@ -7,7 +7,7 @@ guard :bundler do
7
7
  # watch(/^.+\.gemspec/)
8
8
  end
9
9
 
10
- guard :rspec do
10
+ guard :rspec, cmd: 'bundle exec rspec' do
11
11
  watch(%r{^spec/.+_spec\.rb$})
12
12
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
13
13
  watch('spec/spec_helper.rb') { "spec" }
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
- json_attr :data do
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
@@ -2,8 +2,6 @@ require "rails"
2
2
 
3
3
  module SchemalessField
4
4
  class Railtie < Rails::Railtie # :nodoc:
5
- config.eager_load_namespaces << SchemalessField
6
-
7
5
  initializer "schemaless_field.initialize" do |app|
8
6
  ActiveSupport.on_load(:active_record) do
9
7
  include SchemalessField::DSL
@@ -9,8 +9,3 @@ require 'active_support/core_ext/hash'
9
9
 
10
10
  require 'railtie' if defined?(Rails)
11
11
 
12
- module SchemalessField
13
- def self.eager_load!
14
-
15
- end
16
- end
@@ -2,7 +2,7 @@ module SchemalessField
2
2
  module DSL
3
3
  def self.included(base)
4
4
  base.class_eval do
5
- def self.json_attr(attr)
5
+ def self.schemaless_field(attr)
6
6
  yield Field.new(self, attr)
7
7
  end
8
8
  end
@@ -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__#{attribute}, '#{path}')[0]
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__#{attribute})
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__#{attribute}
30
+ def json__#{@model_attr}
31
31
  val = self.send(:#{@model_attr})
32
32
  case val
33
33
  when String
@@ -1,3 +1,3 @@
1
1
  module SchemalessField
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -6,23 +6,62 @@ class Dummy
6
6
 
7
7
  def initialize
8
8
  self.data = {
9
- lannisters: {
10
- geoffrey: 'king',
11
- cersei: 'queen regent',
12
- tyrion: 'master of coin'
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
- it 'creates and yields a new field' do
20
- Dummy.json_attr :data do |f|
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
- dummy = Dummy.new
25
- expect(dummy.lannisters).to eq(dummy.data['lannisters'])
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
 
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- class Dummy
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 { Dummy.new }
23
+ subject { FieldDummy.new }
24
24
  let(:field) { described_class.new(subject.class, :data) }
25
25
 
26
26
  before { field.field :foo, '$..foo' }
@@ -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.3
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-05-30 00:00:00.000000000 Z
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.0
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.