Browse files

improve file tree & add yard

  • Loading branch information...
1 parent 378e75b commit 2ee322aaf50a7c8f77d453e8414cd5fbb9908231 @kachick committed Aug 21, 2012
View
2 LICENSE
@@ -1,4 +1,4 @@
-(The MIT/X11 License)
+(The MIT X License)
Copyright (c) 2012 Kenichi Kamiya
View
5 Manifest.txt
@@ -4,6 +4,9 @@ History.rdoc
Manifest.txt
Rakefile
lib/instancevalue.rb
+lib/instancevalue/version.rb
+lib/instancevalue/classmethods.rb
+lib/instancevalue/singletonclass.rb
test/test_helper.rb
test/test_instancevalue.rb
-example/example.rb
+examples/README.rb
View
4 example/example.rb → examples/README.rb
@@ -3,6 +3,7 @@
require_relative '../lib/instancevalue'
class Person
+
include InstanceValue
value_reader :birthday
@@ -14,9 +15,10 @@ def initialize(birthday)
def age
(Time.now - birthday) / (60 * 60 * 24 * 365)
end
+
end
person = Person.new Time.at 8888
p person.birthday
-p person.age #=> age at runtime
+p person.age #=> age at runtime
#~ person.instance_eval{val :birthday, Time.now} #=> Exception
View
43 lib/instancevalue.rb
@@ -1,6 +1,10 @@
# Instance Value
# Set constant values for each instance.
-# Copyright (C) 2012 Kenichi Kamiya
+# Copyright (C) 2012 Kenichi Kamiya
+
+require_relative 'instancevalue/version'
+require_relative 'instancevalue/classmethods'
+require_relative 'instancevalue/singletonclass'
# @example Overview
# class Person
@@ -23,51 +27,34 @@
# person.instance_eval{val :birthday, Time.now} #=> Exception
module InstanceValue
- VERSION = '0.0.3'.freeze
VALUES_KEYSTORE_NAME = :VALUES_KEYSTORE
- 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
-
+ # @param [Symbol, String] name
def instance_value_defined?(name)
- unless name.instance_of?(Symbol) or name.respond_to?(:to_str)
- raise TypeError
- end
+ raise TypeError unless name.instance_of?(Symbol) or name.respond_to?(:to_str)
_values.has_key? name.to_sym
end
+ # @param [Symbol, String] name
def instance_value_get(name)
instance_value_defined?(name) ? _values[name.to_sym] : nil
end
+ # @param [Symbol, String] name
+ # @return [value]
def instance_value_set(name, value)
- if instance_value_defined? name
- raise "value(#{name}) was already bound"
- else
- _values[name.to_sym] = value
- end
+ raise "value(#{name}) was already bound" if instance_value_defined? name
+
+ _values[name.to_sym] = value
end
+ # @return [Array<Symbol>]
def instance_values
_values.keys
end
+ # @return [String]
def inspect
super.sub(/>\z/){
_values.map{|name, value|
View
15 lib/instancevalue/classmethods.rb
@@ -0,0 +1,15 @@
+module InstanceValue
+
+ module ClassMethods
+
+ private
+
+ def value_reader(name)
+ define_method name do
+ instance_value_get name
+ end
+ end
+
+ end
+
+end
View
13 lib/instancevalue/singletonclass.rb
@@ -0,0 +1,13 @@
+module InstanceValue
+
+ class << self
+
+ private
+
+ def included(mod)
+ mod.extend ClassMethods
+ end
+
+ end
+
+end
View
5 lib/instancevalue/version.rb
@@ -0,0 +1,5 @@
+module InstanceValue
+
+ VERSION = '0.0.3'.freeze
+
+end

0 comments on commit 2ee322a

Please sign in to comment.