entasis 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in entasis.gemspec
4
+ gemspec
data/HISTORY ADDED
@@ -0,0 +1,2 @@
1
+ == 0.1.0
2
+ Initial release
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Elabs AB
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ entasis
2
+ -------
3
+
4
+ Entasis provides a few neat methods for building a basic class. Handy for models without a database.
5
+
6
+ Example:
7
+
8
+ class Person < Entasis::Base
9
+ attributes :name, :age, :city
10
+
11
+ def age=(years)
12
+ @age = years.to_i
13
+ end
14
+ end
15
+
16
+ hilda = Person.new(:name => 'Hilda', :age => '23', :city => 'Berlin')
17
+ hilda.attribute_names # => ["name", "age", "city"]
18
+ hilda.attributes # => {"name"=>"Hilda", "age"=>23, "city"=>"Berlin"}
19
+
20
+
21
+ Note on Patches/Pull Requests
22
+ -----------------------------
23
+
24
+ * Fork the project.
25
+ * Make your feature addition or bug fix.
26
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
27
+ * Commit, do not mess with rakefile, version, or history.
28
+ * Send me a pull request.
29
+
30
+
31
+ Author
32
+ ------
33
+
34
+ Ingemar Edsborn (ingemar@elabs.se)
35
+
36
+
37
+ Copyright
38
+ ---------
39
+
40
+ Copyright (c) 2010 Elabs AB (http://www.elabs.se). See LICENSE for details
41
+
data/entasis.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "entasis/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "entasis"
7
+ s.version = Entasis::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Ingemar Edsborn"]
10
+ s.email = ["ingemar@elabs.se"]
11
+ s.homepage = "http://github.com/ingemar/entasis"
12
+ s.summary = %q{A few neat methods for a basic class}
13
+ s.description = %q{Entasis provides a few very useful instance methods for Struct based classes. Handy for models without a database.}
14
+
15
+ s.files = Dir.glob("lib/**/*") + %w(Gemfile entasis.gemspec HISTORY README.md LICENSE)
16
+ s.test_files = Dir.glob('spec/*')
17
+ end
data/lib/entasis.rb ADDED
@@ -0,0 +1,34 @@
1
+ module Entasis
2
+ class Base
3
+ class << self
4
+ # Takes a list of symbolized attribute names and defines them
5
+ def attributes(*attrs)
6
+ @@attribute_names = attrs.map(&:to_s)
7
+ attr_accessor(*attrs)
8
+ end
9
+ end
10
+
11
+ # Takes a hash and assigns keys and values to it's attributes members
12
+ def initialize(*args)
13
+ if args.first.is_a?(Hash) then set_attributes_from_hash(args.first) else super(*args) end
14
+ end
15
+
16
+ def attribute_names
17
+ @@attribute_names
18
+ end
19
+
20
+ # Returns an attributes hash
21
+ def attributes
22
+ attrs = {}
23
+ attribute_names.each { |name| attrs[name] = send(name) }
24
+ attrs
25
+ end
26
+
27
+ private
28
+ # Takes each key and value from the hash and assigns it to it's attribute.
29
+ def set_attributes_from_hash(hash)
30
+ hash.each_pair { |key, value| self.send("#{key}=", value) }
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,3 @@
1
+ module Entasis
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/person')
3
+
4
+ describe "Entasis::Base" do
5
+ let(:hilda) { hilda = Person.new(:name => 'Hilda', :age => '23', :city => 'Berlin') }
6
+
7
+ describe "#new" do
8
+ it "sets the attributes from the given hash" do
9
+ hilda.name.should == 'Hilda'
10
+ hilda.age.should == 23
11
+ hilda.city.should == 'Berlin'
12
+ end
13
+ end
14
+
15
+ describe "#attribute_names" do
16
+ it 'returns a list of attribute names' do
17
+ hilda.attribute_names.should == %w[name age city]
18
+ end
19
+ end
20
+
21
+ describe "#attributes" do
22
+ it 'returns a hash of attributes names and values' do
23
+ hilda.attributes.should == {"name"=>"Hilda", "age"=>23, "city"=>"Berlin"}
24
+ end
25
+ end
26
+ end
data/spec/person.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Person < Entasis::Base
2
+ attributes :name, :age, :city
3
+
4
+ def age=(years)
5
+ @age = years.to_i
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'entasis'
4
+ require 'rspec'
5
+
6
+ Rspec.configure do |config|
7
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: entasis
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Ingemar Edsborn
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-27 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Entasis provides a few very useful instance methods for Struct based classes. Handy for models without a database.
23
+ email:
24
+ - ingemar@elabs.se
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - lib/entasis/version.rb
33
+ - lib/entasis.rb
34
+ - Gemfile
35
+ - entasis.gemspec
36
+ - HISTORY
37
+ - README.md
38
+ - LICENSE
39
+ - spec/entasis_spec.rb
40
+ - spec/person.rb
41
+ - spec/spec_helper.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/ingemar/entasis
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.7
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A few neat methods for a basic class
76
+ test_files:
77
+ - spec/entasis_spec.rb
78
+ - spec/person.rb
79
+ - spec/spec_helper.rb