tainbox 0.1.0

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: 5481509dcffafe5133286d95ed7bf4248db07c0e
4
+ data.tar.gz: 420840720a69c64f377a6e3b2f0dc68a7e4cdba9
5
+ SHA512:
6
+ metadata.gz: 73a294309162a0ca5babca933f90388ebb0f24fd5f15728534f71af907fc091961352a054fb13a2794cc2bfee74f2d3657183099743b53f5a5a5dc1250ff9425
7
+ data.tar.gz: 48b7b7c3cfb610b9d62bacbe923856453f39bb6b16fcb25cbbe0c575730778110a39dcc7e536b0718fe9ba177476b8c2becd162b0c2ff08b383cfc52c67f8857
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/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Dmitry Gubitskiy
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/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'tainbox'
5
+
6
+ require 'pry'
7
+ Pry.start
@@ -0,0 +1,56 @@
1
+ require 'active_support/core_ext/object/deep_dup'
2
+
3
+ require_relative 'type_converter'
4
+ require_relative 'extensions'
5
+
6
+ class Tainbox::AttributeDefiner
7
+
8
+ def initialize(klass, attribute_name, requested_type, requested_args)
9
+ @klass = klass
10
+ @attribute_name = attribute_name.to_sym
11
+ @requested_type = requested_type
12
+ @requested_args = requested_args
13
+ end
14
+
15
+ def define_getter
16
+ klass.tainbox_register_attribute(attribute_name)
17
+ attribute = attribute_name
18
+
19
+ klass.tainbox_layer.instance_eval do
20
+ define_method(attribute) do
21
+ instance_variable_get(:"@#{attribute}")
22
+ end
23
+ end
24
+ end
25
+
26
+ def define_setter
27
+ klass.tainbox_register_attribute(attribute_name)
28
+
29
+ attribute = attribute_name
30
+ args = requested_args
31
+ type = requested_type
32
+
33
+ klass.tainbox_layer.instance_eval do
34
+
35
+ define_method("#{attribute}=") do |value|
36
+ tainbox_register_attribute_provided(attribute)
37
+ value = Tainbox::TypeConverter.new(type, value, options: args).convert if type
38
+ instance_variable_set(:"@#{attribute}", value)
39
+ end
40
+
41
+ define_method("tainbox_set_default_#{attribute}") do
42
+ if args.has_key?(:default)
43
+ tainbox_register_attribute_provided(attribute)
44
+ instance_variable_set(:"@#{attribute}", args[:default].deep_dup)
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ attr_reader :klass
53
+ attr_reader :attribute_name
54
+ attr_reader :requested_type
55
+ attr_reader :requested_args
56
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'attribute_definer'
2
+ require_relative 'extensions'
3
+
4
+ module Tainbox::ClassMethods
5
+
6
+ def inherited(subclass)
7
+ subclass.tainbox_attributes = tainbox_attributes.dup
8
+ end
9
+
10
+ private
11
+
12
+ def attribute(name, type = nil, **args)
13
+ args = args.dup
14
+ readonly, writeonly = args.delete(:writeonly), args.delete(:readonly)
15
+ definer = Tainbox::AttributeDefiner.new(self, name, type, args)
16
+ definer.define_getter unless writeonly
17
+ definer.define_setter unless readonly
18
+ end
19
+ end
@@ -0,0 +1,35 @@
1
+ class Class
2
+
3
+ attr_writer :tainbox_attributes
4
+
5
+ def tainbox_layer
6
+ unless @tainbox_layer
7
+ @tainbox_layer = Module.new
8
+ include(@tainbox_layer)
9
+ end
10
+ @tainbox_layer
11
+ end
12
+
13
+ def tainbox_attributes
14
+ @tainbox_attributes ||= []
15
+ end
16
+
17
+ def tainbox_register_attribute(attribute)
18
+ tainbox_attributes << attribute
19
+ tainbox_attributes.uniq!
20
+ end
21
+ end
22
+
23
+ class Object
24
+
25
+ private
26
+
27
+ def tainbox_provided_attributes
28
+ @tainbox_provided_attributes ||= []
29
+ end
30
+
31
+ def tainbox_register_attribute_provided(attribute)
32
+ tainbox_provided_attributes << attribute
33
+ tainbox_provided_attributes.uniq!
34
+ end
35
+ end
@@ -0,0 +1,36 @@
1
+ require 'active_support/core_ext/hash/keys'
2
+
3
+ require_relative 'extensions'
4
+
5
+ module Tainbox::InstanceMethods
6
+
7
+ def initialize(attributes = {})
8
+ self.attributes = attributes
9
+ end
10
+
11
+ def attributes
12
+ self.class.tainbox_attributes.map do |attribute|
13
+ [attribute, send(attribute)] if respond_to?(attribute, true)
14
+ end.compact.to_h
15
+ end
16
+
17
+ # TODO Reset attributes that are missing in parameter hash
18
+ def attributes=(attributes)
19
+ attributes = attributes.symbolize_keys
20
+ self.class.tainbox_attributes.each do |attribute|
21
+
22
+ if attributes.has_key?(attribute)
23
+ method_name = "#{attribute}="
24
+ send(method_name, attributes[attribute]) if respond_to?(method_name, true)
25
+
26
+ else
27
+ method_name = "tainbox_set_default_#{attribute}"
28
+ send(method_name) if respond_to?(method_name, true)
29
+ end
30
+ end
31
+ end
32
+
33
+ def attribute_provided?(attribute)
34
+ tainbox_provided_attributes.include?(attribute.to_sym)
35
+ end
36
+ end
@@ -0,0 +1,63 @@
1
+ require 'active_support/values/time_zone'
2
+
3
+ class Tainbox::TypeConverter
4
+
5
+ def self.define_converter(type, block)
6
+ method_name = conversion_method_name_for(type)
7
+ raise "Converter for #{type} already exists" if instance_methods.include?(method_name)
8
+ define_method(method_name, block)
9
+ end
10
+
11
+ def self.conversion_method_name_for(type)
12
+ "convert_to_#{type.to_s.downcase}".to_sym
13
+ end
14
+
15
+ def initialize(type, value, options: {})
16
+ @type = type
17
+ @value = value
18
+ @options = options
19
+ end
20
+
21
+ def convert
22
+ method_name = self.class.conversion_method_name_for(type)
23
+ raise "Type not supported: #{type}" unless respond_to?(method_name)
24
+ send(method_name)
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :type
30
+ attr_reader :value
31
+ attr_reader :options
32
+ end
33
+
34
+ Tainbox.define_converter(Integer) do
35
+ Integer(value) rescue nil
36
+ end
37
+
38
+ Tainbox.define_converter(Float) do
39
+ Float(value) rescue nil
40
+ end
41
+
42
+ Tainbox.define_converter(String) do
43
+ value.to_s
44
+ end
45
+
46
+ Tainbox.define_converter(Symbol) do
47
+ value.to_sym rescue nil
48
+ end
49
+
50
+ Tainbox.define_converter(Time) do
51
+ value.is_a?(Time) ? value : (Time.zone.parse(value) rescue nil)
52
+ end
53
+
54
+ Tainbox.define_converter(:Boolean) do
55
+ strict = options.fetch(:strict, true)
56
+ if !strict && %w(true on 1).include?(value)
57
+ true
58
+ elsif !strict && %w(false off 0).include?(value)
59
+ false
60
+ else
61
+ !!value
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module Tainbox
2
+ VERSION = '0.1.0'
3
+ end
data/lib/tainbox.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Tainbox
2
+
3
+ def self.included(klass)
4
+ klass.extend(Tainbox::ClassMethods)
5
+ klass.include(Tainbox::InstanceMethods)
6
+ end
7
+
8
+ def self.define_converter(type, &block)
9
+ Tainbox::TypeConverter.define_converter(type, block)
10
+ end
11
+ end
12
+
13
+ require_relative 'tainbox/class_methods'
14
+ require_relative 'tainbox/instance_methods'
15
+ require_relative 'tainbox/type_converter'
data/tainbox.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'tainbox/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'tainbox'
7
+ spec.version = Tainbox::VERSION
8
+ spec.authors = ['Dmitry Gubitskiy']
9
+ spec.email = ['d.gubitskiy@gmail.com']
10
+
11
+ # TODO Proper summary
12
+ spec.summary = 'Tainbox summary'
13
+ spec.homepage = 'https://github.com/enthrops/tainbox'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_runtime_dependency 'activesupport'
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.8'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'pry'
24
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tainbox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Gubitskiy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
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
+ - d.gubitskiy@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - bin/console
81
+ - lib/tainbox.rb
82
+ - lib/tainbox/attribute_definer.rb
83
+ - lib/tainbox/class_methods.rb
84
+ - lib/tainbox/extensions.rb
85
+ - lib/tainbox/instance_methods.rb
86
+ - lib/tainbox/type_converter.rb
87
+ - lib/tainbox/version.rb
88
+ - tainbox.gemspec
89
+ homepage: https://github.com/enthrops/tainbox
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
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
+ rubyforge_project:
109
+ rubygems_version: 2.4.6
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Tainbox summary
113
+ test_files: []
114
+ has_rdoc: