instancevalue 0.0.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of instancevalue might be problematic. Click here for more details.

checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4f81328e88341374d8d003880edb5b974cf044c8
4
+ data.tar.gz: 11459b8b24cf2cd869fbb9035e7a8e0596ac30ad
5
+ SHA512:
6
+ metadata.gz: 7407e6585545ef49ca78c4ee0ff264c6c8fbcdd1e4df06447113cda66063ab17a7ae964b0b637651b0722e3030d3f5bdb41d07ba9b6d18673c0ec7ce0e2bdabf
7
+ data.tar.gz: 5423c9efaf3ab8539deabe145f471d11f814a660245f55e724de974b9e50bdcaee6a934e998d1ce70afec90ed45e4cdc07047eb15948a70da2478c6b8dbf5d5d
data/.gitignore ADDED
@@ -0,0 +1,37 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .ruby-version
6
+ coverage
7
+ coverage.data
8
+ InstalledFiles
9
+ lib/bundler/man
10
+ pkg
11
+ rdoc
12
+ spec/reports
13
+ test/tmp
14
+ test/version_tmp
15
+ tmp
16
+
17
+ # bundler
18
+ vendor/
19
+
20
+ # YARD artifacts
21
+ .yardoc
22
+ _yardoc
23
+ doc/
24
+
25
+ # tmp-old
26
+ .old
27
+
28
+ # editor
29
+ *~
30
+ .redcar
31
+
32
+ # other
33
+ *.lock
34
+ *.bak
35
+ tool/
36
+ *\#*
37
+ .rbx/
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+ rvm:
3
+ - ruby-head
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - 1.9.2
7
+ - rbx-19mode
8
+ - jruby-head
9
+ - jruby-19mode
10
+ matrix:
11
+ allow_failures:
12
+ - rvm: ruby-head
13
+ - rvm: rbx-19mode
14
+ - rvm: jruby-head
15
+ - rvm: jruby-19mode
16
+ before_install:
17
+ - gem install bundler
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --readme README.md lib/**/*.rb
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'rake'
7
+ end
8
+
9
+ group :test do
10
+ gem 'rake'
11
+ end
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT X License)
2
+
3
+ Copyright (c) 2012 Kenichi Kamiya
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ instancevalue
2
+ =============
3
+
4
+ [![Build Status](https://secure.travis-ci.org/kachick/instancevalue.png)](http://travis-ci.org/kachick/instancevalue)
5
+ [![Gem Version](https://badge.fury.io/rb/instancevalue.png)](http://badge.fury.io/rb/instancevalue)
6
+
7
+ Description
8
+ -----------
9
+
10
+ Constant values for each instance.
11
+
12
+ Features
13
+ --------
14
+
15
+ ### instance_variable like API
16
+
17
+ * .value_reader
18
+ * #instance_value_defined?
19
+ * #instance_value_get
20
+ * #instance_value_set
21
+ * #instance_values
22
+ * #remove_instance_value
23
+
24
+ ### Aliased shortname
25
+
26
+ * #val
27
+
28
+ Usage
29
+ -----
30
+
31
+ ```ruby
32
+ require 'instancevalue'
33
+
34
+ class Person
35
+
36
+ include InstanceValue
37
+
38
+ value_reader :birthday
39
+
40
+ def initialize(birthday)
41
+ val :birthday, birthday
42
+ end
43
+
44
+ def age
45
+ (Time.now - birthday) / (60 * 60 * 24 * 365)
46
+ end
47
+
48
+ end
49
+
50
+ person = Person.new Time.at YOUR_BIRTHDAY
51
+ person.birthday #=> YOUR_BIRTHDAY
52
+ person.age #=> age at runtime
53
+ person.instance_eval{val :birthday, Time.now} #=> Exception
54
+ ```
55
+
56
+ Requirements
57
+ -------------
58
+
59
+ * [Ruby 1.9.2 or later](http://travis-ci.org/#!/kachick/instancevalue)
60
+
61
+ Install
62
+ -------
63
+
64
+ ```shell
65
+ gem install instancevalue
66
+ ```
67
+
68
+ Development
69
+ ----
70
+
71
+ ```shell
72
+ bundle exec rake
73
+ ```
74
+
75
+ Link
76
+ ----
77
+
78
+ * [Home](http://kachick.github.com/instancevalue/)
79
+ * [code](https://github.com/kachick/instancevalue)
80
+ * [API](http://kachick.github.com/instancevalue/yard/frames.html)
81
+ * [issues](https://github.com/kachick/instancevalue/issues)
82
+ * [CI](http://travis-ci.org/#!/kachick/instancevalue)
83
+ * [gem](https://rubygems.org/gems/instancevalue)
84
+
85
+ License
86
+ --------
87
+
88
+ The MIT X11 License
89
+ Copyright (c) 2012 Kenichi Kamiya
90
+ See MIT-LICENSE for further details.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rake/testtask'
5
+
6
+ task default: [:test]
7
+
8
+ Rake::TestTask.new do |tt|
9
+ tt.verbose = true
10
+ tt.warning = true
11
+ end
@@ -0,0 +1,24 @@
1
+ $VERBOSE = true
2
+
3
+ require_relative '../lib/instancevalue'
4
+
5
+ class Person
6
+
7
+ include InstanceValue
8
+
9
+ value_reader :birthday
10
+
11
+ def initialize(birthday)
12
+ val :birthday, birthday
13
+ end
14
+
15
+ def age
16
+ (Time.now - birthday) / (60 * 60 * 24 * 365)
17
+ end
18
+
19
+ end
20
+
21
+ person = Person.new Time.at 8888
22
+ p person.birthday
23
+ p person.age #=> age at runtime
24
+ #~ person.instance_eval{val :birthday, Time.now} #=> Exception
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../lib/instancevalue/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ['Kenichi Kamiya']
5
+ gem.email = ['kachick1+ruby@gmail.com']
6
+ gem.description = %q{Constant values for each instance.}
7
+ gem.summary = %q{Constant values for each instance.}
8
+ gem.homepage = 'http://kachick.github.com/instancevalue'
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features|declare)/})
13
+ gem.name = 'instancevalue'
14
+ gem.require_paths = ['lib']
15
+ # dup for https://github.com/rubygems/rubygems/commit/48f1d869510dcd325d6566df7d0147a086905380#-P0
16
+ gem.version = InstanceValue::VERSION.dup
17
+
18
+ gem.required_ruby_version = '>=2.3.3'
19
+
20
+ gem.add_development_dependency 'yard', '>= 0.9.8', '< 2'
21
+ gem.add_development_dependency 'rake', '>= 12', '< 20'
22
+ gem.add_development_dependency 'bundler', '>= 1.14.3', '< 2'
23
+ gem.add_development_dependency 'test-unit', '>= 3.2.3', '< 4'
24
+ end
@@ -0,0 +1,103 @@
1
+ # instancevalue - Constant values for each instance.
2
+ # Copyright (C) 2012 Kenichi Kamiya
3
+
4
+ require_relative 'instancevalue/version'
5
+ require_relative 'instancevalue/classmethods'
6
+ require_relative 'instancevalue/singleton_class'
7
+
8
+ # @example Overview
9
+ # class Person
10
+ # include InstanceValue
11
+ #
12
+ # value_reader :birthday
13
+ #
14
+ # def initialize(birthday)
15
+ # val :birthday, birthday
16
+ # end
17
+ #
18
+ # def age
19
+ # (Time.now - birthday) / (60 * 60 * 24 * 365)
20
+ # end
21
+ # end
22
+ #
23
+ # person = Person.new YOUR_BIRTHDAY
24
+ # person.birthday #=> YOUR_BIRTHDAY
25
+ # person.age #=> age at runtime
26
+ # person.instance_eval{val :birthday, Time.now} #=> Exception
27
+ module InstanceValue
28
+
29
+ VALUES_KEYSTORE_NAME = :VALUES_KEYSTORE
30
+
31
+ # @param [Symbol, String] name
32
+ def instance_value_defined?(name)
33
+ raise TypeError unless name.instance_of?(Symbol) or name.respond_to?(:to_str)
34
+
35
+ _values.has_key? name.to_sym
36
+ end
37
+
38
+ # @param [Symbol, String] name
39
+ def instance_value_get(name)
40
+ instance_value_defined?(name) ? _values[name.to_sym] : nil
41
+ end
42
+
43
+ # @param [Symbol, String] name
44
+ # @return [value]
45
+ def instance_value_set(name, value)
46
+ raise "value(#{name}) was already bound" if instance_value_defined? name
47
+
48
+ _values[name.to_sym] = value
49
+ end
50
+
51
+ # @return [Array<Symbol>]
52
+ def instance_values
53
+ _values.keys
54
+ end
55
+
56
+ # @return [String]
57
+ def inspect
58
+ super.sub(/>\z/){
59
+ _values.map{|name, value|
60
+ " #{name}=#{value.inspect}"
61
+ }.join + '>'
62
+ }
63
+ end
64
+
65
+ protected
66
+
67
+ def _values
68
+ if singleton_class.const_defined? VALUES_KEYSTORE_NAME
69
+ singleton_class.const_get VALUES_KEYSTORE_NAME
70
+ else
71
+ singleton_class.const_set VALUES_KEYSTORE_NAME, {}
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def initialize_copy(original)
78
+ singleton_class.const_set VALUES_KEYSTORE_NAME, original._values.dup
79
+ end
80
+
81
+ def remove_instance_value(name)
82
+ if instance_value_defined? name
83
+ _values.delete name.to_sym
84
+ else
85
+ raise NameError
86
+ end
87
+ end
88
+
89
+ # @example
90
+ # val(name) #=> get
91
+ # val(name, value) #=> set
92
+ def val(name, *values)
93
+ case values.length
94
+ when 0
95
+ instance_value_get name
96
+ when 1
97
+ instance_value_set name, values.first
98
+ else
99
+ raise ArgumentError, "wrong number of Argument #{values.length} for 1 or 2"
100
+ end
101
+ end
102
+
103
+ end
@@ -0,0 +1,15 @@
1
+ module InstanceValue
2
+
3
+ module ClassMethods
4
+
5
+ private
6
+
7
+ def value_reader(name)
8
+ define_method name do
9
+ instance_value_get name
10
+ end
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,13 @@
1
+ module InstanceValue
2
+
3
+ class << self
4
+
5
+ private
6
+
7
+ def included(mod)
8
+ mod.extend ClassMethods
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,5 @@
1
+ module InstanceValue
2
+
3
+ VERSION = '0.0.4'.freeze
4
+
5
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require_relative '../lib/instancevalue'
@@ -0,0 +1,70 @@
1
+ require_relative 'helper'
2
+
3
+ class TestBasicCase < Test::Unit::TestCase
4
+ class Person
5
+ include InstanceValue
6
+
7
+ value_reader :birthday
8
+
9
+ def initialize(birthday)
10
+ val :birthday, birthday
11
+ end
12
+
13
+ def age
14
+ (Time.now - birthday) / (60 * 60 * 24 * 365)
15
+ end
16
+ end
17
+
18
+ def test_normaly
19
+ person = Person.new Time.at 8888
20
+ assert_equal Time.at(8888), person.birthday
21
+ assert_kind_of Float, person.age
22
+ assert_equal [:birthday], person.instance_values
23
+
24
+ assert_raises RuntimeError do
25
+ person.instance_eval{val :birthday, Time.now}
26
+ end
27
+
28
+ assert_equal 'Ken', person.instance_eval{val :name, 'Ken'}
29
+ assert_equal [:birthday, :name], person.instance_values
30
+
31
+ assert_equal 'Ken', person.instance_eval{val :name}
32
+ assert_equal 'Ken', person.instance_eval{remove_instance_value :name}
33
+ assert_equal [:birthday], person.instance_values
34
+
35
+ assert_equal nil, person.instance_eval{val :name}
36
+ assert_equal 'John', person.instance_eval{val :name, 'John'}
37
+
38
+ assert_raises NoMethodError do
39
+ person.name
40
+ end
41
+ end
42
+
43
+ def test_dup
44
+ person = Person.new Time.now
45
+ person2 = person.dup
46
+
47
+ assert_equal person.instance_values, person2.instance_values
48
+
49
+ person2.instance_value_set :newer, 123
50
+
51
+ assert_same false, (person.instance_values == person2.instance_values)
52
+ end
53
+
54
+ def test_clone
55
+ person = Person.new Time.now
56
+ person2 = person.clone
57
+
58
+ assert_equal person.instance_values, person2.instance_values
59
+
60
+ person2.instance_value_set :newer, 123
61
+
62
+ assert_same false, (person.instance_values == person2.instance_values)
63
+ end
64
+
65
+ def test_inspect
66
+ person = Person.new 9753
67
+ person.instance_variable_set :@var, 1234
68
+ assert_equal true, !!(/\A#<TestBasicCase::Person:\w+ @var=1234 birthday=9753>\z/ =~ person.inspect)
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: instancevalue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Kenichi Kamiya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.8
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.8
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '12'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '20'
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '12'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '20'
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 1.14.3
60
+ - - "<"
61
+ - !ruby/object:Gem::Version
62
+ version: '2'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.14.3
70
+ - - "<"
71
+ - !ruby/object:Gem::Version
72
+ version: '2'
73
+ - !ruby/object:Gem::Dependency
74
+ name: test-unit
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 3.2.3
80
+ - - "<"
81
+ - !ruby/object:Gem::Version
82
+ version: '4'
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 3.2.3
90
+ - - "<"
91
+ - !ruby/object:Gem::Version
92
+ version: '4'
93
+ description: Constant values for each instance.
94
+ email:
95
+ - kachick1+ruby@gmail.com
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files: []
99
+ files:
100
+ - ".gitignore"
101
+ - ".travis.yml"
102
+ - ".yardopts"
103
+ - Gemfile
104
+ - MIT-LICENSE
105
+ - README.md
106
+ - Rakefile
107
+ - examples/README.rb
108
+ - instancevalue.gemspec
109
+ - lib/instancevalue.rb
110
+ - lib/instancevalue/classmethods.rb
111
+ - lib/instancevalue/singleton_class.rb
112
+ - lib/instancevalue/version.rb
113
+ - test/helper.rb
114
+ - test/test_instancevalue.rb
115
+ homepage: http://kachick.github.com/instancevalue
116
+ licenses: []
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 2.3.3
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.5.2
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Constant values for each instance.
138
+ test_files:
139
+ - test/helper.rb
140
+ - test/test_instancevalue.rb