Permalink
Browse files

first release

  • Loading branch information...
0 parents commit ea14fa068518f849981a36e7d343d7d4085514b9 @kachick committed Apr 5, 2012
Showing with 234 additions and 0 deletions.
  1. +5 −0 History.rdoc
  2. +22 −0 LICENSE
  3. +7 −0 Manifest.txt
  4. +68 −0 README.rdoc
  5. +15 −0 Rakefile
  6. +22 −0 example/example.rb
  7. +95 −0 lib/instancevalue.rb
@@ -0,0 +1,5 @@
+=== 0.0.1 2012-04-06
+
+* 1 major enhancement:
+ * Initial release
+
22 LICENSE
@@ -0,0 +1,22 @@
+(The MIT/X11 License)
+
+Copyright (c) 2012 Kenichi Kamiya
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
+README.rdoc
+LICENSE
+History.rdoc
+Manifest.txt
+Rakefile
+lib/instancevalue.rb
+example/example.rb
@@ -0,0 +1,68 @@
+= Instance Value
+
+* http://github.com/kachick/instancevalue
+* http://rubygems.org/gems/instancevalue
+* http://rubyforge.org/projects/instancevalue
+
+== Description
+
+Set constant values for each instance.
+
+== Usage
+
+* setup
+
+ require 'instancevalue'
+
+* overview
+
+ class Person
+ include InstanceValue
+
+ value_reader :birthday
+
+ def initialize(birthday)
+ val :birthday, birthday
+ end
+
+ def age
+ (Time.now - val(:birthday)) / (60 * 60 * 24 * 365)
+ end
+ end
+
+ person = Person.new Time.at 8888
+ p person.birthday
+ p person.age #=> age at runtime
+ person.instance_eval{val :birthday, Time.now} #=> Exception
+
+* It has instance_variable like API
+
+ * .value_reader
+ * #instance_value_defined?
+ * #instance_value_get
+ * #instance_value_set
+ * #instance_values
+ * #remove_instance_value
+
+* Aliased shortname
+
+ * #val
+
+== Requirements
+
+* Ruby 1.9.2 or later
+
+ target release versions
+
+ * 1.9.3-p125
+ * 1.9.2-p290
+
+== Installation
+
+* gem install instancevalue
+
+== License
+
+Copyright (C) 2012 Kenichi Kamiya
+
+The MIT/X11 License (See the file LICENSE)
@@ -0,0 +1,15 @@
+gem 'hoe', '~> 3.0.1'
+require 'hoe'
+require 'fileutils'
+
+Hoe.plugin :newgem
+
+$hoe = Hoe.spec 'instancevalue' do
+ developer 'Kenichi Kamiya', 'kachick1+ruby@gmail.com'
+ self.rubyforge_name = name
+ require_ruby_version '>= 1.9.2'
+ dependency 'yard', '~> 0.7.5', :development
+end
+
+require 'newgem/tasks'
+Dir['tasks/**/*.rake'].each { |t| load t }
@@ -0,0 +1,22 @@
+$VERBOSE = true
+
+require_relative '../lib/instancevalue'
+
+class Person
+ include InstanceValue
+
+ value_reader :birthday
+
+ def initialize(birthday)
+ val :birthday, birthday
+ end
+
+ def age
+ (Time.now - val(:birthday)) / (60 * 60 * 24 * 365)
+ end
+end
+
+person = Person.new Time.at 8888
+p person.birthday
+p person.age #=> age at runtime
+#~ person.instance_eval{val :birthday, Time.now} #=> Exception
@@ -0,0 +1,95 @@
+# Instance Value
+# Set constant values for each instance.
+# Copyright (C) 2012 Kenichi Kamiya
+
+# @example Overview
+# class Person
+# include InstanceValue
+#
+# value_reader :birthday
+#
+# def initialize(birthday)
+# val :birthday, birthday
+# end
+#
+# def age
+# (Time.now - val(:birthday)) / (60 * 60 * 24 * 365)
+# end
+# end
+#
+# person = Person.new YOUR_BIRTHDAY
+# person.birthday
+# person.age #=> age at runtime
+# person.instance_eval{val :birthday, Time.now} #=> Exception
+module InstanceValue
+ VERSION = '0.0.1'.freeze
+
+ module Eigen
+ private
+
+ def value_reader(name)
+ define_method name do
+ instance_value_get name
+ end
+ end
+ end
+
+ class << self
+ private
+
+ def included(mod)
+ mod.extend Eigen
+ end
+ end
+
+ # @example
+ # val(name) #=> get
+ # val(name, const_value) #=> set
+ def val(name, *values)
+ case values.length
+ when 0
+ instance_value_get name
+ when 1
+ instance_value_set name, values.first
+ else
+ raise ArgumentError, "wrong number of Argument #{values.length} for 1 or 2"
+ end
+ end
+
+ def instance_value_defined?(name)
+ unless name.instance_of?(Symbol) or name.respond_to?(:to_str)
+ raise TypeError
+ end
+
+ if singleton_class.const_defined? :VALUES
+ singleton_class::VALUES.has_key? name.to_sym
+ else
+ singleton_class.const_set :VALUES, {}
+ false
+ end
+ end
+
+ def instance_value_get(name)
+ instance_value_defined?(name) ? singleton_class::VALUES[name.to_sym] : nil
+ end
+
+ def instance_value_set(name, value)
+ if instance_value_defined? name
+ raise "value #{} was already binded"
+ else
+ singleton_class::VALUES[name.to_sym] = value
+ end
+ end
+
+ def instance_values
+ singleton_class::VALUES.keys
+ end
+
+ def remove_instance_value(name)
+ if instance_value_defined? name
+ singleton_class::VALUES.delete_key name.to_sym
+ else
+ raise NameError
+ end
+ end
+end

0 comments on commit ea14fa0

Please sign in to comment.