trafaret 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a6f9ebc15fa44c667060779f703eb99d2462d83
4
+ data.tar.gz: 82a0675c08400058c321efa24851b6a0e9d2a66f
5
+ SHA512:
6
+ metadata.gz: cd413ceeafc1db8704c047a81e8bb72d56404183cd5c62c446bdafc8652be4a7b6469b4b3821e4346155b8dfae2f35207c91d3eba9558d19ffcb28bc8824a8e1
7
+ data.tar.gz: 7e978fa7abe98d587cd85985fc18fc862e2c298457eba0b65f8d59afd47f57f8c58e2b3968abada01be6f5e321edc4adf3360443112203bf7a22b6627b2433d1
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in trafaret.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mikhail Krivushin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Trafaret
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'trafaret'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install trafaret
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/trafaret/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module Trafaret
2
+ VERSION = "1.0.1"
3
+ end
data/lib/trafaret.rb ADDED
@@ -0,0 +1,76 @@
1
+ require "trafaret/version"
2
+
3
+ module Trafaret
4
+ class Attribute
5
+ def initialize(name, options = {}, &blk)
6
+ @name = name
7
+ @options = options
8
+ @blk = blk
9
+ end
10
+
11
+ def name
12
+ @name.to_s
13
+ end
14
+
15
+ def get(obj)
16
+ if @blk
17
+ return @blk.call obj
18
+ end
19
+ data = begin
20
+ obj.send(@name)
21
+ rescue NameError
22
+ nil
23
+ end
24
+ data ||= begin
25
+ obj[@name] || obj[@name.to_s]
26
+ rescue NoMethodError, TypeError
27
+ nil
28
+ end
29
+ end
30
+
31
+ alias :dump :get
32
+ end
33
+
34
+ class Array < Attribute
35
+ def dump(obj)
36
+ cls = @options[:class]
37
+ if data = get(obj)
38
+ data.map do |elem|
39
+ cls.dump elem
40
+ end
41
+ else
42
+ []
43
+ end
44
+ end
45
+ end
46
+
47
+ class Hash < Attribute
48
+ def initialize(name = nil, options = {}, &blk)
49
+ @name = name
50
+ @options = options
51
+ @attrs = options[:attributes] || []
52
+ instance_eval(&blk)
53
+ end
54
+
55
+ def attribute(name, options = {}, &blk)
56
+ @attrs << Attribute.new(name, options, &blk)
57
+ end
58
+
59
+ def array(name, options = {}, &blk)
60
+ @attrs << Array.new(name, options, &blk)
61
+ end
62
+
63
+ def hash(name, options = {}, &blk)
64
+ @attrs << Hash.new(name, options = {}, &blk)
65
+ end
66
+
67
+ def dump(obj)
68
+ res = {}
69
+ data = if @name then get(obj) else obj end
70
+ @attrs.each do |attr|
71
+ res[attr.name] = attr.dump(data)
72
+ end
73
+ res
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,31 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'trafaret'
3
+
4
+ describe Trafaret do
5
+ Provider = Trafaret::Hash.new do
6
+ attribute :url
7
+ end
8
+
9
+ Checker = Trafaret::Hash.new do
10
+ attribute :name
11
+ array :providers, class: Provider do |obj|
12
+ obj['oa'].flat_map do |prov_name, accs|
13
+ accs.map {|uuid, data| data}
14
+ end
15
+ end
16
+ end
17
+
18
+ let :raw do
19
+ {'name' => 'kuku',
20
+ 'oa' => {
21
+ 'facebook' => {
22
+ '124234234234234' => {'url' => 'http://ya.ru'},
23
+ '124234234234235' => {'url' => 'http://www.ru'}
24
+ }
25
+ }
26
+ }
27
+ end
28
+ it 'should work' do
29
+ Checker.dump(raw).should == ({"name"=>"kuku", "providers"=>[{"url"=>"http://ya.ru"}, {"url"=>"http://www.ru"}]})
30
+ end
31
+ end
data/trafaret.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'trafaret/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "trafaret"
8
+ spec.version = Trafaret::VERSION
9
+ spec.authors = ["Mikhail Krivushin"]
10
+ spec.email = ["krivushinme@gmail.com"]
11
+ spec.summary = %q{Convert data structures. Like a RABL.}
12
+ spec.description = %q{The thing to convert entities from one to other.
13
+ Idea from Python Trafaret lib in Ruby way.}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trafaret
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mikhail Krivushin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-02 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |-
42
+ The thing to convert entities from one to other.
43
+ Idea from Python Trafaret lib in Ruby way.
44
+ email:
45
+ - krivushinme@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - .gitignore
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/trafaret.rb
56
+ - lib/trafaret/version.rb
57
+ - spec/trafaret_spec.rb
58
+ - trafaret.gemspec
59
+ homepage: ''
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.0.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Convert data structures. Like a RABL.
83
+ test_files:
84
+ - spec/trafaret_spec.rb