data2ruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f661988cfc00ef0950fc24a44ae3d71ea780987b
4
+ data.tar.gz: 278ef2df7e775ea6374fe537aa4cb2d421c632ec
5
+ SHA512:
6
+ metadata.gz: 56977e822d77a4e6641444e0fa1fd84ae8ecdf9aef2c1044390e132c11bc463ee981685b0a6ecc3b7e3e42584092054c8154bd9f9a264a4a4e3305aec29ec2eb
7
+ data.tar.gz: 23519951835fcfbc12653457b2ba1bca6ecbb494485e2f5fe2e8857640c1a9da0d13db80b43eb6efd72c4a65371080a2e0bb5fcafa8960365f9268fee3480a58
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in data2ruby.gemspec
6
+ gemspec
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ data2ruby (0.1.0)
5
+ activemodel (~> 5)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.1)
11
+ activesupport (= 5.2.1)
12
+ activesupport (5.2.1)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ concurrent-ruby (1.0.5)
18
+ diff-lcs (1.3)
19
+ i18n (1.1.1)
20
+ concurrent-ruby (~> 1.0)
21
+ minitest (5.11.3)
22
+ rake (10.5.0)
23
+ rspec (3.8.0)
24
+ rspec-core (~> 3.8.0)
25
+ rspec-expectations (~> 3.8.0)
26
+ rspec-mocks (~> 3.8.0)
27
+ rspec-core (3.8.0)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-expectations (3.8.2)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.8.0)
32
+ rspec-mocks (3.8.0)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.8.0)
35
+ rspec-support (3.8.0)
36
+ thread_safe (0.3.6)
37
+ tzinfo (1.2.5)
38
+ thread_safe (~> 0.1)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ bundler (~> 1.16)
45
+ data2ruby!
46
+ rake (~> 10.0)
47
+ rspec (~> 3.0)
48
+
49
+ BUNDLED WITH
50
+ 1.16.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Roberto Maestroni
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,86 @@
1
+ # Data2ruby
2
+
3
+ Convert an heterogeneous tree structure in nested `ActiveModel::Model` objects.
4
+
5
+ For instance, if you have a JSON object describing a train journey:
6
+
7
+ ```json
8
+ {
9
+ "id": 987,
10
+ "connections": [
11
+ {
12
+ "start": "London",
13
+ "finish": "Paris",
14
+ "departure_time": "2015-07-11T09:23:00+01:00",
15
+ "arrival_time": "2015-07-11T12:41:00+02:00",
16
+ "fare": {
17
+ "currency": "GBP",
18
+ "value": 159.0
19
+ }
20
+ },
21
+ {
22
+ "start": "Paris",
23
+ "finish": "Barcelona",
24
+ "departure_time": "2015-07-11T13:56:00+02:00",
25
+ "arrival_time": "2015-07-11T20:17:00+02:00",
26
+ "fare": {
27
+ "currency": "GBP",
28
+ "value": 50.0
29
+ }
30
+ }
31
+ ]
32
+ }
33
+ ```
34
+
35
+ You can define a Ruby class describing it as
36
+
37
+ ```ruby
38
+ class TravelOption
39
+ include Data2ruby
40
+
41
+ has_one :journey do
42
+ attr_accessor :id
43
+ validates_numericality_of :id, only_integer: true
44
+
45
+ has_many :connections do
46
+ attr_accessor :start, :finish, :departure_time, :arrival_time
47
+ validates_presence_of :start, :finish, :departure_time, :arrival_time
48
+
49
+ has_one :fare do
50
+ attr_accessor :currency, :value
51
+ validates_presence_of :currency
52
+ validates_numericality_of :value
53
+ end
54
+ end
55
+ end
56
+ end
57
+ ```
58
+
59
+ Then instantiate and validate:
60
+
61
+ ```ruby
62
+ travel_option = TravelOption.new(JSON.parse(json_string))
63
+
64
+ # run ActiveModel validations
65
+ travel_option.valid_structure? # => true
66
+ travel_option.invalid_items # => []
67
+
68
+ # navigate the associations
69
+ travel_option.journey.id # => 987
70
+ travel_option.connections[0].start # => "London"
71
+ travel_option.connections[0].fare.currency # => "GBP"
72
+ ```
73
+
74
+ ## Development
75
+
76
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
77
+
78
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rmaestroni/data2ruby.
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "data2ruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "data2ruby/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "data2ruby"
8
+ spec.version = Data2ruby::VERSION
9
+ spec.authors = ["Roberto Maestroni"]
10
+ spec.email = ["r.maestroni@gmail.com"]
11
+
12
+ spec.summary = %q{Define a DSL in Ruby to validate and access any data}
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/rmaestroni/data2ruby"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+
30
+ spec.add_dependency 'activemodel', '~> 5'
31
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+ require 'active_model'
3
+
4
+ module Data2ruby
5
+ include Enumerable
6
+
7
+ module ClassMethods
8
+
9
+ def associations
10
+ @_associations ||= []
11
+ end
12
+
13
+ def has_one(label, &block)
14
+ attr_writer(label)
15
+ associations << label
16
+ klass = build_assoc_class(&block)
17
+ assoc_instance_builder = build_assoc_instance_builder(klass)
18
+ inst_var_memo = "@_has_one_#{label}"
19
+
20
+ define_method(label) do
21
+ memoize(inst_var_memo) { assoc_instance_builder.(data[label]) }
22
+ end
23
+ end
24
+
25
+ def has_many(label, &block)
26
+ attr_writer(label)
27
+ associations << label
28
+ klass = build_assoc_class(&block)
29
+ assoc_instance_builder = build_assoc_instance_builder(klass)
30
+ inst_var_memo = "@_has_many_#{label}"
31
+
32
+ define_method(label) do
33
+ memoize(inst_var_memo) do
34
+ data[label].map do |item|
35
+ assoc_instance_builder.(item)
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def build_assoc_class(&block)
44
+ Class.new do
45
+ include ActiveModel::Model
46
+ include Data2ruby
47
+ extend ActiveModel::Naming
48
+
49
+ def self.name
50
+ 'AnonAssociation'
51
+ end
52
+
53
+ def name
54
+ self.class.name
55
+ end
56
+ end
57
+ .tap { |klass| klass.class_exec(&block) }
58
+ end
59
+
60
+ def build_assoc_instance_builder(klass)
61
+ lambda do |data|
62
+ klass.new(data).tap do |obj|
63
+ obj.define_singleton_method(:data) { data }
64
+ end
65
+ end
66
+ end
67
+ end # module ClassMethods
68
+
69
+ def self.included(klass)
70
+ klass.extend(ClassMethods)
71
+ end
72
+
73
+ def data
74
+ raise NotImplementedError, 'Implement in the including class'
75
+ end
76
+
77
+ def each(&block)
78
+ self.class.associations.each do |assoc_name|
79
+ object = public_send(assoc_name)
80
+ object.is_a?(Array) ? object.each(&block) : block.yield(object)
81
+ end
82
+ end
83
+
84
+ def invalid_items
85
+ reduce(select { |item| item.errors.any? }) do |sum, item|
86
+ sum + item.invalid_items
87
+ end
88
+ end
89
+
90
+ def valid_structure?
91
+ reduce(respond_to?(:valid?) ? valid? : true) do |sum, item|
92
+ item.valid_structure? && sum # forces to evaluate validity for all the nodes
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ def memoize(var_name, &block)
99
+ if instance_variable_defined?(var_name)
100
+ instance_variable_get(var_name)
101
+ else
102
+ instance_variable_set(
103
+ var_name,
104
+ instance_exec(&block)
105
+ )
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Data2ruby
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data2ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Roberto Maestroni
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5'
69
+ description:
70
+ email:
71
+ - r.maestroni@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - data2ruby.gemspec
86
+ - lib/data2ruby.rb
87
+ - lib/data2ruby/version.rb
88
+ homepage: https://github.com/rmaestroni/data2ruby
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.6.14
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Define a DSL in Ruby to validate and access any data
112
+ test_files: []