Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 75de4f7ced6b46ed58ad7386b663a60e935b9bef @kachick committed Apr 16, 2012
Showing with 625 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +5 −0 History.rdoc
  3. +22 −0 LICENSE
  4. +12 −0 Manifest.txt
  5. +66 −0 README.rdoc
  6. +15 −0 Rakefile
  7. +25 −0 example/example.rb
  8. +27 −0 lib/structable.rb
  9. +110 −0 lib/structable/classmethods.rb
  10. +186 −0 lib/structable/instancemethods.rb
  11. +3 −0 lib/structable/version.rb
  12. +2 −0 test/test_helper.rb
  13. +151 −0 test/test_structable.rb
@@ -0,0 +1 @@
+*~
@@ -0,0 +1,5 @@
+=== 0.0.1 2012-04-17
+
+* 1 major enhancement:
+ * Initial release
+
@@ -0,0 +1,22 @@
+(The MIT/X11 License)
+
+Copyright (c) 2011-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,12 @@
+README.rdoc
+LICENSE
+History.rdoc
+Manifest.txt
+Rakefile
+lib/structable.rb
+lib/structable/classmethods.rb
+lib/structable/instancemethods.rb
+lib/structable/version.rb
+test/test_helper.rb
+test/test_structable.rb
+example/example.rb
@@ -0,0 +1,66 @@
+= Structable
+
+* http://github.com/kachick/structable
+
+== Description
+
+Get Struct like APIs in your class.
+
+== Features
+
+* API looks Ruby's Struct
+* Member aliasing
+* Inheritable
+
+* Note:
+
+This library forked from below projects.
+
+ * https://github.com/kachick/structus
+ * https://github.com/kachick/striuct
+
+== Usage
+
+* Setup
+
+ require 'structable'
+
+* Overview
+
+ class Drink
+ include Structable
+
+ member :taste
+ end
+
+ class Tea < Drink
+ member :leaf
+ alias_member :type, :leaf
+ end
+
+ tea = Tea.new :bitter, :green
+ tea.members #=> [:taste, :leaf]
+ tea.members(true) #=> [:taste, :leaf, :type]
+ tea.member? :type #=> true
+ tea.type = 'Special Flavor'
+ tea.taste #=> :bitter
+ tea.leaf #=> 'Special Flavor'
+
+== Requirements
+
+* Ruby 1.9.2 or later
+
+ tested release versions
+
+ * 1.9.3-p125
+ * 1.9.2-p290
+
+== Installation
+
+* gem install structable
+
+== License
+
+Copyright (C) 2011-2012 Kenichi Kamiya
+
+The MIT/X11 License (See the file LICENSE.)
@@ -0,0 +1,15 @@
+gem 'hoe', '~> 3.0.3'
+require 'hoe'
+require 'fileutils'
+
+Hoe.plugin :newgem
+
+$hoe = Hoe.spec 'structable' 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,25 @@
+#!/usr/local/bin/ruby -w
+
+$VERBOSE = true
+
+require_relative '../lib/structable'
+
+class Drink
+ include Structable
+
+ member :taste
+end
+
+class Tea < Drink
+ member :leaf
+ alias_member :type, :leaf
+end
+
+tea = Tea.new :bitter, :green
+p tea.members #=> [:taste, :leaf]
+p tea.members(true) #=> [:taste, :leaf, :type]
+p tea.member? :type #=> true
+tea.type = 'Special Flavor'
+p tea.taste #=> :bitter
+p tea.leaf #=> 'Special Flavor'
+p tea[0] #=> :bitter
@@ -0,0 +1,27 @@
+# Copyright (C) 2011-2012 Kenichi Kamiya
+
+require_relative 'structable/version'
+require_relative 'structable/classmethods'
+require_relative 'structable/instancemethods'
+
+module Structable; class << self
+
+ private
+
+ def included(mod)
+ eigen = self
+
+ mod.module_eval do
+ if eigen.equal? ::Structable
+ extend ::Structable::ClassMethods
+ include ::Structable::InstanceMethods
+ attrs = {}
+ else
+ attrs = eigen::MEMBER_DEFINES.dup
+ end
+
+ const_set :MEMBER_DEFINES, attrs
+ end
+ end
+
+end; end
@@ -0,0 +1,110 @@
+require 'forwardable'
+
+module Structable::ClassMethods
+ extend Forwardable
+
+ def each_member(&block)
+ return to_enum(__method__) unless block_given?
+ _attrs.each_key(&block)
+ self
+ end
+
+ alias_method :each_key, :each_member
+
+ def members(aliased=false)
+ (aliased ? _attrs : _attrs.select{|k, v|v.respond_to? :each_pair}).keys
+ end
+
+ alias_method :keys, :members
+
+ def length
+ _attrs.length
+ end
+
+ alias_method :size, :length
+
+ def has_member?(name)
+ _attrs.has_key? name
+ end
+
+ alias_method :member?, :has_member?
+ alias_method :has_key?, :has_member?
+ alias_method :key?, :has_member?
+
+ def alias_member(aliased, original)
+ _attrs[aliased] = original
+ (_attrs(original)[:aliases] ||= []) << aliased
+
+ alias_method aliased, original
+ alias_method :"#{aliased}=", :"#{original}="
+ end
+
+ def freeze
+ close
+ super
+ end
+
+ # @param [Symbol, String, #to_str] name
+ def autonym(name)
+ name = name.to_sym
+ if _attrs.has_key? name
+ (linked = _attrs[name]).kind_of?(Symbol) ? linked : name
+ else
+ raise NameError
+ end
+ end
+
+ # @group Constructor
+
+ def_delegator :self, :new, :[]
+
+ # @param [#each_pair, #keys] pairs ex: Hash, Struct
+ def load_pairs(pairs)
+ unless pairs.respond_to?(:each_pair) and pairs.respond_to?(:keys)
+ raise TypeError, 'no pairs object'
+ end
+
+ raise ArgumentError, "different members" unless (pairs.keys - keys).empty?
+
+ new.tap {|instance|
+ pairs.each_pair do |name, value|
+ instance[name] = value
+ end
+ }
+ end
+
+ # @endgroup
+
+ private
+
+ def member(name)
+ name = name.to_sym
+ raise NameError, 'Already defined' if _attrs.has_key? name
+
+ define_method name do
+ _get! name
+ end
+
+ define_method :"#{name}=" do |value|
+ _set! name, value
+ end
+
+ _attrs[name] = {}
+
+ nil
+ end
+
+ def _attrs(name=nil)
+ name ? self::MEMBER_DEFINES[autonym name] : self::MEMBER_DEFINES
+ end
+
+ def inherited(subclass)
+ eigen = self
+ super subclass
+
+ subclass.module_eval do
+ const_set :MEMBER_DEFINES, eigen::MEMBER_DEFINES.dup
+ end
+ end
+
+end
Oops, something went wrong. Retry.

0 comments on commit 75de4f7

Please sign in to comment.