green_monster 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f6adace53c29bfc6d68431b199d7a2e187e8b9af
4
+ data.tar.gz: 7ce8a37082a88b0c8faa76199907f39c94ac8ca6
5
+ SHA512:
6
+ metadata.gz: dd9c5b117627f04156912f085830fbf989f619dee1a9331030a5b799e30b26710c83eb3d1d8c599bc0e8c5c2917ebd152ba011eeee017bdffbe4b29330d0ab27
7
+ data.tar.gz: cd017302a1cb9bef1c2c7ee9d557824bb25883d4151f795c51617ad5a3c030b9bb5715b49732316a01fa45c3f0bf25f3d2f9a527c8b243324f3cf0c700c8d543
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in green_monster.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 mesiobass
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
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.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # GreenMonster
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/green_monster`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'green_monster'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install green_monster
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/green_monster.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "green_monster"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'green_monster/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "green_monster"
8
+ spec.version = GreenMonster::VERSION
9
+ spec.authors = ["mesiobass"]
10
+ spec.email = ["mes10bass@gmail.com"]
11
+
12
+ spec.summary = %q{GreenMonster is excel, csv converter.}
13
+ spec.description = %q{GreenMonster is excel, csv converter.}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+
26
+ spec.add_dependency 'activesupport', '>= 3.2'
27
+ end
@@ -0,0 +1,7 @@
1
+ require "green_monster/version"
2
+ require "green_monster/schema"
3
+
4
+ module GreenMonster
5
+ autoload :Attribute , 'green_monster/attribute'
6
+ autoload :ActiveRecordExtension, 'green_monster/active_record_extension'
7
+ end
@@ -0,0 +1,5 @@
1
+ module GreenMonster
2
+ module ActiveRecordExtension
3
+ autoload :RubyXLConverter, 'green_monster/active_record_extension/ruby_xl_converter'
4
+ end
5
+ end
@@ -0,0 +1,52 @@
1
+ require "green_monster/decoder"
2
+ require "green_monster/encoder"
3
+ require "green_monster/value_ruby_xl"
4
+
5
+ module GreenMonster
6
+ module ActiveRecordExtension
7
+ module RubyXLConverter
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ class << self
12
+ attr_reader :schema
13
+ attr_reader :after_decode_callback
14
+
15
+ # スキーマ定義
16
+ def schema_is
17
+ @schema = Schema.new
18
+ yield(@schema)
19
+ end
20
+
21
+ # ファイルから読み込んだ後に実行するコールバック
22
+ # TODO: 分離予定、設計しなおし予定
23
+ def after_decode(&block)
24
+ @after_decode_callback = block if block_given?
25
+ end
26
+
27
+ # 外部ファイルから読み込んだ内容でオブジェクトを生成
28
+ def find_or_build_from_row(row)
29
+ decoder = Decoder.new(self.schema)
30
+ .extend(ValueRubyXL)
31
+ attrs = if self.after_decode_callback.present?
32
+ decoder.convert(row, &self.after_decode_callback)
33
+ else
34
+ decoder.convert(row)
35
+ end
36
+
37
+ obj = self.find_by("#{self.schema.index_key}" => attrs[self.schema.index_key]) || self.new
38
+ obj.assign_attributes(attrs)
39
+
40
+ obj
41
+ end
42
+ end
43
+
44
+ # 外部ファイル用にモデルデータを変換
45
+ def to_row
46
+ Encoder.new(self.class.schema).convert(self)
47
+ end
48
+
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,11 @@
1
+ module GreenMonster
2
+ module Attribute
3
+ autoload :Abstract , 'green_monster/attribute/abstract'
4
+ autoload :Encodable , 'green_monster/attribute/encodable'
5
+ autoload :Decodable , 'green_monster/attribute/decodable'
6
+ autoload :Reversible, 'green_monster/attribute/reversible'
7
+ autoload :Integer , 'green_monster/attribute/integer'
8
+ autoload :String , 'green_monster/attribute/string'
9
+ autoload :Date , 'green_monster/attribute/date'
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module GreenMonster
2
+ module Attribute
3
+ class Abstract
4
+ attr_reader :name, :options
5
+
6
+ # 初期化
7
+ def initialize(name, options = {})
8
+ @name = name
9
+ @options = options
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ module GreenMonster
2
+ module Attribute
3
+ class Date < Reversible
4
+ end
5
+ end
6
+ end
7
+
@@ -0,0 +1,8 @@
1
+ module GreenMonster
2
+ module Attribute
3
+ module Decodable
4
+ def decode(value); value; end
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,7 @@
1
+ module GreenMonster
2
+ module Attribute
3
+ module Encodable
4
+ def encode(value); value; end
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ module GreenMonster
2
+ module Attribute
3
+ class Integer < Reversible
4
+ end
5
+ end
6
+ end
7
+
8
+
@@ -0,0 +1,8 @@
1
+ module GreenMonster
2
+ module Attribute
3
+ class Reversible < Abstract
4
+ include Encodable
5
+ include Decodable
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ module GreenMonster
2
+ module Attribute
3
+ class String < Reversible
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,29 @@
1
+ module GreenMonster
2
+ class Decoder
3
+ # 初期化
4
+ def initialize(schema)
5
+ @schema = schema
6
+ end
7
+
8
+ # 変換
9
+ def convert(row, &block)
10
+ attrs = {}
11
+ @schema.attributes.each do |attribute|
12
+ attrs.update(
13
+ attribute.name => attribute.decode(fetch_value(row[attribute.options[:col_num]]))
14
+ )
15
+ end
16
+
17
+ block.call(attrs) if block_given?
18
+
19
+ attrs
20
+ end
21
+
22
+ private
23
+ # 使用するデータを抽出する
24
+ def fetch_value(data)
25
+ data
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,15 @@
1
+ module GreenMonster
2
+ class Encoder
3
+ # 初期化
4
+ def initialize(schema)
5
+ @schema = schema
6
+ end
7
+
8
+ # 変換
9
+ def convert(model)
10
+ @schema.attributes.map do |attr|
11
+ [attr.name, attr.encode(model.send(attr.name)), attr.options[:col_num]]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ module GreenMonster
2
+ class Schema
3
+ attr_reader :attributes, :index_key
4
+
5
+ def initialize()
6
+ end
7
+
8
+ def attribute(name, type, options = {})
9
+ @attributes ||= []
10
+ @column_number ||= 0
11
+
12
+ if options[:col_num].present?
13
+ @column_number = options[:col_num]
14
+ else
15
+ options.update(col_num: @column_number)
16
+ end
17
+ @column_number += 1
18
+
19
+ @attributes << type.new(name, options)
20
+ end
21
+
22
+ def key(key)
23
+ @index_key = key
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ module GreenMonster
2
+ module ValueRubyXL
3
+ def fetch_value(data)
4
+ data.value
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module GreenMonster
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: green_monster
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - mesiobass
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-16 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ description: GreenMonster is excel, csv converter.
70
+ email:
71
+ - mes10bass@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - green_monster.gemspec
87
+ - lib/green_monster.rb
88
+ - lib/green_monster/active_record_extension.rb
89
+ - lib/green_monster/active_record_extension/ruby_xl_converter.rb
90
+ - lib/green_monster/attribute.rb
91
+ - lib/green_monster/attribute/abstract.rb
92
+ - lib/green_monster/attribute/date.rb
93
+ - lib/green_monster/attribute/decodable.rb
94
+ - lib/green_monster/attribute/encodable.rb
95
+ - lib/green_monster/attribute/integer.rb
96
+ - lib/green_monster/attribute/reversible.rb
97
+ - lib/green_monster/attribute/string.rb
98
+ - lib/green_monster/decoder.rb
99
+ - lib/green_monster/encoder.rb
100
+ - lib/green_monster/schema.rb
101
+ - lib/green_monster/value_ruby_xl.rb
102
+ - lib/green_monster/version.rb
103
+ homepage: ''
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.6
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: GreenMonster is excel, csv converter.
127
+ test_files: []