active_value 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e1838aa9aae9d65c7b51fe06cfe5082f4fd4015b6ce1100fa32288a3a36edfae
4
+ data.tar.gz: e2f73bf570adfbd1d8d917281ef7219b4284e86803457b4a85431643130c10eb
5
+ SHA512:
6
+ metadata.gz: 5725bb8b6edf0a16bddc0edcbed37400bc62b46c4a3d3fe084c55ba8016229a5bfcb833aa83d55b32510396a14b8b9eb9cd775ec09c9f06046b670e2971f587a
7
+ data.tar.gz: 9f2d679643eb240feaa6ff0e1d57750f3961de2745f99611e74de55e90984b162e71e7a8e168fb5322abdbededab8c9d35856cd1fab38da8419e219a36a861a0
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_value.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Taiki Hiramatsu
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,49 @@
1
+ # ActiveValue
2
+
3
+ Model for non database. (ValueObject)
4
+ However like ActiveRecord.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'active_value'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install active_value
22
+
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ class QuestionType < ActiveValue::Base
28
+
29
+ attr_accessor :id, :symbol, :name
30
+
31
+ Checkbox = new id: 1, symbol: :checkbox, name: "Check Box"
32
+ Radio = new id: 2, symbol: :radio, name: "Radio Button"
33
+ Selectbox = new id: 3, symbol: :select, name: "Select Box"
34
+ Text = new id: 4, symbol: :text, name: "Text Box"
35
+ TextArea = new id: 5, symbol: :textarea, name: "Text Area"
36
+
37
+ end
38
+ ```
39
+
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hiratai/active_value.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
49
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_value/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_value"
8
+ spec.version = ActiveValue::VERSION
9
+ spec.authors = ["Taiki Hiramatsu"]
10
+ spec.email = ["ta.hi.samba@gmail.com"]
11
+
12
+ spec.summary = %q{Model for non database.(ValueObject) However like ActiveRecord.}
13
+ #spec.description = %q{}
14
+ spec.homepage = "https://github.com/hiratai/active_value"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.13"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "minitest", "~> 5.0"
36
+
37
+ spec.add_dependency "activesupport"
38
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_value"
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
@@ -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,8 @@
1
+ require "active_support/core_ext"
2
+ require "active_support/dependencies"
3
+ require "active_value/version"
4
+
5
+ module ActiveValue
6
+ extend ActiveSupport::Autoload
7
+ autoload :Base
8
+ end
@@ -0,0 +1,101 @@
1
+ require "active_support/core_ext/hash/keys"
2
+
3
+ module ActiveValue
4
+ # TODO: English Translation
5
+ # ActiveRecordライクに定数を定義するための基底クラス
6
+ # このクラスを継承したクラス内で宣言した定数をレコードとして扱うことができる。
7
+ # 継承先のクラスでattr_accessorで要素を定義することで、その要素をDBのカラムのように扱うことができる。
8
+ #
9
+ # いくつかの要素名は予約されていて、定義があれば対応したメソッドが使用可能。
10
+ # 要素名 : メソッド
11
+ # id : find
12
+ # symbol : define_question_methods
13
+ #
14
+ class Base
15
+
16
+ # 未定義のメソッド呼び出しはall(Array)に委譲
17
+ def self.method_missing(method, *args, &block)
18
+ all.public_send(method, *args, &block)
19
+ end
20
+
21
+ # ActiveRecordライクに使えるfind, find_by, all, pluckを定義
22
+ def self.find(index); find_by(id: index); end
23
+ def self.find_by(condition); all.find { |object| object.public_send(condition.keys.first) == condition.values.first }; end
24
+ def self.all
25
+ constants.collect { |name| const_get(name) }.sort
26
+ end
27
+ def self.pluck(*accessors)
28
+ map { |record| accessors.size > 1 ? Array(accessors).map { |accessor| record.public_send(accessor) } : record.public_send(accessors.first) }
29
+ end
30
+
31
+ # symbol要素の定義があれば、symbol名 + ? でインスタンスが同一のものか判別するメソッドを定義
32
+ def self.define_question_methods(attr_name = :symbol)
33
+ constants.collect { |name| const_get(name) }.each do |object|
34
+ define_method(object.public_send(attr_name).to_s + '?') { self == object } if object.respond_to?(attr_name)
35
+ end
36
+ end
37
+
38
+ # 定義したaccessorを順序を保ったまま保持したいためオーバーライド。従来のattr_accessorの挙動は変更しない。
39
+ def self.attr_accessor(*several_variants)
40
+ @accessors = *several_variants
41
+ super
42
+ end
43
+
44
+ # accessor一覧を取得するメソッド
45
+ def self.accessors
46
+ readers = instance_methods.reject { |attr| attr.to_s[-1] == '=' }
47
+ writers = instance_methods.select { |attr| attr.to_s[-1] == '=' }.map { |attr| attr.to_s.chop.to_sym }
48
+ accessors = readers & writers - [:!]
49
+ Array(@accessors) | accessors.reverse!
50
+ end
51
+
52
+ # ActiveRecordのnewのようにハッシュで初期化を行える機能と
53
+ # コピーコンストラクタ(に似た何か)を定義(Shallowコピー)
54
+ def initialize(attributes = {})
55
+ case attributes
56
+ when self.class then self.class.accessors.stringify_keys.each { |attribute| public_send(attribute + '=', attributes.public_send(attribute)) }
57
+ when Hash then attributes.stringify_keys.each { |key, value| public_send(key + '=', value) if respond_to?(key + '=') }
58
+ end
59
+ end
60
+
61
+ # ActiveRecordと同様に要素へハッシュアクセスで取得できるようにアクセサを定義
62
+ #def [](attribute); public_send(attribute); end
63
+ #def []=(attribute, value); public_send(attribute.to_s + '=', value) end
64
+
65
+ # Hash型へ浅い変換を行う。値がコンテナ(Hash, Array)を含んでいた場合もその中の探索は行わずそのまま出力をする。
66
+ def to_shallow_hash
67
+ self.class.accessors.inject(Hash.new) { |hash, key| hash[key] = public_send(key); hash }.reject { |key, value| value.nil? }
68
+ end
69
+
70
+ # Hash型へ深い変換を行う。値がコンテナ(Hash, Array)を含んでいた場合、このクラスが含まれなくなるまで探索とHashへの変換を再帰的に行う。
71
+ def to_deep_hash
72
+ scan = ->(value) do
73
+ case value
74
+ when Hash then value.inject(Hash.new) { |h, (k, v)| h[k] = scan.call(v); h }
75
+ when Array then value.map { |v| scan.call(v) }
76
+ when ConstantRecord then scan.call(value.to_shallow_hash)
77
+ else value
78
+ end
79
+ end
80
+ self.class.accessors.inject(Hash.new) { |hash, key| hash[key] = scan.call(public_send(key)); hash }
81
+ end
82
+ alias_method :to_h, :to_deep_hash
83
+
84
+ def to_json
85
+ to_h.to_json
86
+ end
87
+
88
+ def inspect
89
+ hash = to_shallow_hash
90
+ Hash === hash ? '#<' << self.class.name.split('::').last << ' ' << hash.map { |key, value| key.to_s << ': ' << value.inspect }.join(', ') << '>' : hash.inspect
91
+ end
92
+
93
+ # Spaceship Operatorを定義、id等の比較可能な識別子が最初に定義されることが前提、存在しなければobject_idを参照する。
94
+ # TODO: ソート順序の基準となるattrの宣言を行うメソッドの実装
95
+ def <=>(another)
96
+ attr = self.class.accessors.first || :object_id
97
+ public_send(attr) <=> another.public_send(attr)
98
+ end
99
+
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveValue
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_value
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Taiki Hiramatsu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-21 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.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: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - ta.hi.samba@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - active_value.gemspec
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/active_value.rb
86
+ - lib/active_value/base.rb
87
+ - lib/active_value/version.rb
88
+ homepage: https://github.com/hiratai/active_value
89
+ licenses:
90
+ - MIT
91
+ metadata:
92
+ allowed_push_host: https://rubygems.org
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.0.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Model for non database.(ValueObject) However like ActiveRecord.
112
+ test_files: []