sway 0.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: 983cc173d303a2b27145a06d89656958e798698f
4
+ data.tar.gz: efd13806aeb181ad7c6764b08f1658f33a19a27b
5
+ SHA512:
6
+ metadata.gz: 6c0b866633c3a617e87489c86398b1e768eceb9ad43ea18fbcd2f5f2e2cf1c73f669905fb823e0b874e7b5d6993d8783122948ff4b5e028aa490f1cf9884d313
7
+ data.tar.gz: b8d28c5417837c682b66192d6fb11946e93deb6d9f610e4473d1904201863a25f54df3256bfdc91e6550714e39c6908e973d0f27fe2c754e52f3ad82b2b9a31e
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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format d
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sway.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 blp1526
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,55 @@
1
+ # Sway
2
+
3
+ This gem creates Hashie::Mash objects from JSON, YAML, CSV.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sway'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sway
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+
23
+ # When JSON or YAML file.
24
+ foo = Sway::File.new('path_to_your_json_or_yaml_file')
25
+ foo.mash # if 'path_to_your_json_or_yaml_file' is hash.
26
+ foo.mashes # if 'path_to_your_json_or_yaml_file' is array.
27
+
28
+ # If csv, then you have to use option[:header] == :first_line
29
+ bar = Sway::File.new('path_to_your_csv_file', header: :first_line)
30
+ bar.mash #=> nil
31
+ bar.mashes
32
+
33
+ # You can also do as below.
34
+ baz = Sway::JSON.new('json_string')
35
+ baz.mash # if 'json_string' is hash.
36
+ baz.mashes # if 'json_string' is array.
37
+
38
+ # When YAML string.
39
+ qux = Sway::YAML.new('yaml_string')
40
+ qux.mash # if 'yaml_string' is hash.
41
+ qux.mashes # if 'yaml_string' is array.
42
+
43
+ # When CSV string.
44
+ quux = Sway::CSV.new('csv_string', header: :first_line)
45
+ quux.mash #=> nil
46
+ quux.mashes
47
+
48
+ ```
49
+ ## Contributing
50
+
51
+ 1. Fork it ( http://github.com/blp1526/sway/fork )
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/sway.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'sway/version'
2
+ require 'sway/file'
3
+ require 'sway/json'
4
+ require 'sway/yaml'
5
+ require 'sway/csv'
6
+
7
+ module Sway
8
+ end
data/lib/sway/base.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'sway/mashable'
2
+
3
+ module Sway
4
+ class Base
5
+ include Sway::Mashable
6
+ attr_reader :mash, :mashes
7
+
8
+ def initialize(array_or_hash)
9
+ case array_or_hash.class.to_s
10
+ when 'Array'
11
+ array = array_or_hash
12
+ @mashes = create_mashes_from(array)
13
+ when 'Hash'
14
+ hash = array_or_hash
15
+ @mash = create_mash_from(hash)
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/sway/csv.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'sway/base'
2
+ require 'active_support'
3
+ require 'csv'
4
+
5
+ module Sway
6
+ class CSV < Sway::Base
7
+ def initialize(csv, *options)
8
+ extracted_options = options.extract_options!
9
+ without_header = !(extracted_options[:header] == :first_line)
10
+ raise "CSV can't be moshed without a header." if without_header
11
+ lines = ::CSV.new(csv)
12
+ key = lines.first
13
+ values = lines.map {|line| line}
14
+ array = values.inject([]) {|key_value, value| key_value << Hash[key.zip(value)]}
15
+ super(array)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ module Sway
2
+ module Extensions
3
+ YAML = ['yml', 'yaml']
4
+ JSON = ['json']
5
+ CSV = ['csv']
6
+ ALL = self.constants.inject([]) {|all, const_symbol| all << const_get(const_symbol)}.flatten
7
+ end
8
+ end
data/lib/sway/file.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'sway/base'
2
+ require 'sway/extensions'
3
+ require 'sway/json'
4
+ require 'sway/yaml'
5
+ require 'sway/csv'
6
+ require 'active_support'
7
+
8
+ module Sway
9
+ class File < Sway::Base
10
+ def initialize(array_or_hash_path, *options)
11
+ raise 'Not file path argument.' unless ::File.exist?(array_or_hash_path)
12
+ @filename_extension = ::File.extname(array_or_hash_path).delete('.')
13
+ raise 'Sorry, unsupported extension.' unless Extensions::ALL.include?(@filename_extension)
14
+ @file = ::File.open(array_or_hash_path, 'r').read
15
+ @options = options.extract_options!
16
+ filename_extension_instance = create_filename_extension_instance
17
+ @mash = filename_extension_instance.mash
18
+ @mashes = filename_extension_instance.mashes
19
+ end
20
+
21
+ private
22
+ def create_filename_extension_instance
23
+ case @filename_extension
24
+ when *Extensions::YAML
25
+ Sway::YAML.new(@file)
26
+ when *Extensions::JSON
27
+ Sway::JSON.new(@file)
28
+ when *Extensions::CSV
29
+ Sway::CSV.new(@file, @options)
30
+ end
31
+ end
32
+ end
33
+ end
data/lib/sway/json.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'sway/base'
2
+ require 'json'
3
+
4
+ module Sway
5
+ class JSON < Sway::Base
6
+ def initialize(json)
7
+ array_or_hash = ::JSON.parse(json)
8
+ super(array_or_hash)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'hashie'
2
+
3
+ module Sway
4
+ module Mashable
5
+ def create_mashes_from(array)
6
+ array.inject([]) {|items, item| items << Hashie::Mash.new(item)}
7
+ end
8
+
9
+ def create_mash_from(hash)
10
+ Hashie::Mash.new(hash)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Sway
2
+ VERSION = "0.0.1"
3
+ end
data/lib/sway/yaml.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'sway/base'
2
+ require 'yaml'
3
+
4
+ module Sway
5
+ class YAML < Sway::Base
6
+ def initialize(yaml)
7
+ array_or_hash = ::YAML.load(yaml)
8
+ super(array_or_hash)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ title,year
2
+ Sticky Fingers,1971
3
+ Exile on Main St.,1972
4
+ Goats Head Soup,1973
@@ -0,0 +1,5 @@
1
+ [
2
+ {"title" : "Sticky Fingers", "year" : "1971"},
3
+ {"title" : "Exile on Main St.", "year" : "1972"},
4
+ {"title" : "Goat Head Soup", "year" : "1973"}
5
+ ]
@@ -0,0 +1,6 @@
1
+ - title: Sticky Fingers
2
+ year: 1971
3
+ - title: Exile on Main St.
4
+ year: 1972
5
+ - title: Goat Head Soup
6
+ year: 1973
@@ -0,0 +1,6 @@
1
+ - title: Sticky Fingers
2
+ year: 1971
3
+ - title: Exile on Main St.
4
+ year: 1972
5
+ - title: Goat Head Soup
6
+ year: 1973
@@ -0,0 +1,7 @@
1
+ {
2
+ "the_glimmer_twins" :
3
+ {
4
+ "vocal" : "Mick Jagger",
5
+ "guitar" : "Keith Richards"
6
+ }
7
+ }
@@ -0,0 +1,3 @@
1
+ the_glimmer_twins:
2
+ vocal: Mick Jagger
3
+ guitar: Keith Richards
@@ -0,0 +1,3 @@
1
+ the_glimmer_twins:
2
+ vocal: Mick Jagger
3
+ guitar: Keith Richards
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+ require 'sway/file'
3
+
4
+ describe Sway::File do
5
+ describe 'array.json' do
6
+ before {@instance = Sway::File.new('spec/fixtures/array.json')}
7
+ context 'mashes.class' do
8
+ it {expect(@instance.mashes.class).to eq(Array)}
9
+ end
10
+ context 'mashes.first.class' do
11
+ it {expect(@instance.mashes.first.class).to eq(Hashie::Mash)}
12
+ end
13
+ context 'mash.class' do
14
+ it {expect(@instance.mash.class).to eq(NilClass)}
15
+ end
16
+ end
17
+
18
+ describe 'hash.json' do
19
+ before {@instance = Sway::File.new('spec/fixtures/hash.json')}
20
+ context 'mashes.class' do
21
+ it {expect(@instance.mashes.class).to eq(NilClass)}
22
+ end
23
+ context 'mash.class' do
24
+ it {expect(@instance.mash.class).to eq(Hashie::Mash)}
25
+ end
26
+ end
27
+
28
+ describe 'array.yaml' do
29
+ before {@instance = Sway::File.new('spec/fixtures/array.yaml')}
30
+ context 'mashes.class' do
31
+ it {expect(@instance.mashes.class).to eq(Array)}
32
+ end
33
+ context 'mashes.first.class' do
34
+ it {expect(@instance.mashes.first.class).to eq(Hashie::Mash)}
35
+ end
36
+ context 'mash.class' do
37
+ it {expect(@instance.mash.class).to eq(NilClass)}
38
+ end
39
+ end
40
+
41
+ describe 'hash.yaml' do
42
+ before {@instance = Sway::File.new('spec/fixtures/hash.yaml')}
43
+ context 'mashes.class' do
44
+ it {expect(@instance.mashes.class).to eq(NilClass)}
45
+ end
46
+ context 'mash.class' do
47
+ it {expect(@instance.mash.class).to eq(Hashie::Mash)}
48
+ end
49
+ end
50
+
51
+ describe 'array.yml' do
52
+ before {@instance = Sway::File.new('spec/fixtures/array.yml')}
53
+ context 'mashes.class' do
54
+ it {expect(@instance.mashes.class).to eq(Array)}
55
+ end
56
+ context 'mashes.first.class' do
57
+ it {expect(@instance.mashes.first.class).to eq(Hashie::Mash)}
58
+ end
59
+ context 'mash.class' do
60
+ it {expect(@instance.mash.class).to eq(NilClass)}
61
+ end
62
+ end
63
+
64
+ describe 'hash.yml' do
65
+ before {@instance = Sway::File.new('spec/fixtures/hash.yml')}
66
+ context 'mashes.class' do
67
+ it {expect(@instance.mashes.class).to eq(NilClass)}
68
+ end
69
+ context 'mash.class' do
70
+ it {expect(@instance.mash.class).to eq(Hashie::Mash)}
71
+ end
72
+ end
73
+
74
+ describe 'array.csv with header: :first_line' do
75
+ before {@instance = Sway::File.new('spec/fixtures/array.csv', header: :first_line)}
76
+ context 'mashes.class' do
77
+ it {expect(@instance.mashes.class).to eq(Array)}
78
+ end
79
+ context 'mashes.first.class' do
80
+ it {expect(@instance.mashes.first.class).to eq(Hashie::Mash)}
81
+ end
82
+ context 'mash.class' do
83
+ it {expect(@instance.mash.class).to eq(NilClass)}
84
+ end
85
+ end
86
+
87
+ describe 'array.csv without header: :first_line' do
88
+ it {expect { Sway::File.new('spec/fixtures/array.csv')}.to raise_error(RuntimeError)}
89
+ end
90
+ end
@@ -0,0 +1 @@
1
+ require 'rspec'
data/sway.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'sway/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "sway"
7
+ spec.version = Sway::VERSION
8
+ spec.authors = ["blp1526"]
9
+ spec.email = ["blp1526@gmail.com"]
10
+ spec.summary = %q{Creates Hashie::Mash objects from YAML, JSON, CSV}
11
+ spec.description = %q{This gem creates Hashie::Mash objects from YAML, JSON, CSV. Use various uses.}
12
+ spec.homepage = "https://github.com/blp1526/sway"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "hashie", "2.0.5"
21
+ spec.add_dependency "activesupport", "4.0.2"
22
+
23
+ spec.add_development_dependency "bundler", "1.5.0.rc.1"
24
+ spec.add_development_dependency "rake", "10.1.0"
25
+ spec.add_development_dependency "rspec", "2.14.1"
26
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sway
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - blp1526
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hashie
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.5.0.rc.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.5.0.rc.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 10.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 10.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.14.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.14.1
83
+ description: This gem creates Hashie::Mash objects from YAML, JSON, CSV. Use various
84
+ uses.
85
+ email:
86
+ - blp1526@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - .rspec
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - lib/sway.rb
98
+ - lib/sway/base.rb
99
+ - lib/sway/csv.rb
100
+ - lib/sway/extensions.rb
101
+ - lib/sway/file.rb
102
+ - lib/sway/json.rb
103
+ - lib/sway/mashable.rb
104
+ - lib/sway/version.rb
105
+ - lib/sway/yaml.rb
106
+ - spec/fixtures/array.csv
107
+ - spec/fixtures/array.json
108
+ - spec/fixtures/array.yaml
109
+ - spec/fixtures/array.yml
110
+ - spec/fixtures/hash.json
111
+ - spec/fixtures/hash.yaml
112
+ - spec/fixtures/hash.yml
113
+ - spec/lib/sway/file_spec.rb
114
+ - spec/spec_helper.rb
115
+ - sway.gemspec
116
+ homepage: https://github.com/blp1526/sway
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.0.3
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Creates Hashie::Mash objects from YAML, JSON, CSV
140
+ test_files:
141
+ - spec/fixtures/array.csv
142
+ - spec/fixtures/array.json
143
+ - spec/fixtures/array.yaml
144
+ - spec/fixtures/array.yml
145
+ - spec/fixtures/hash.json
146
+ - spec/fixtures/hash.yaml
147
+ - spec/fixtures/hash.yml
148
+ - spec/lib/sway/file_spec.rb
149
+ - spec/spec_helper.rb