entasis 0.2.2 → 0.3.0.pre1

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/HISTORY CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.3.0
2
+ Refactored to dependency injection
3
+ Only set known attributes
4
+
1
5
  == 0.2.1
2
6
  Introduced ActiveModel validations
3
7
 
@@ -6,4 +10,4 @@ Moved away from Struct and started to use attr_accessor as boiler plate.
6
10
  Added Base.attributes method to define attributes
7
11
 
8
12
  == 0.1.0
9
- Initial release
13
+ Initial release
data/README.md CHANGED
@@ -5,39 +5,22 @@ Entasis provides a few neat methods for building a basic class. Handy for models
5
5
 
6
6
  Example:
7
7
 
8
- class Person < Entasis::Base
9
- attributes :name, :age, :city
8
+ ```ruby
9
+ class Person
10
+ include Entasis::Model
10
11
 
11
- def age=(years)
12
- @age = years.to_i
13
- end
14
- end
12
+ attributes :name, :age, :city
15
13
 
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
- anon = Person.new
20
- anon.valid? # => false
21
- anon.errors # => {:name=>["can't be blank"]}>
14
+ def age=(years)
15
+ @age = years.to_i
16
+ end
17
+ end
22
18
 
19
+ hilda = Person.new(:name => 'Hilda', :age => '23', :city => 'Berlin')
20
+ hilda.attribute_names # => ["name", "age", "city"]
21
+ hilda.attributes # => {"name"=>"Hilda", "age"=>23, "city"=>"Berlin"}
23
22
 
24
- Note on Patches/Pull Requests
25
- -----------------------------
26
-
27
- * Fork the project.
28
- * Make your feature addition or bug fix.
29
- * Add tests for it. This is important so I don't break it in a future version unintentionally.
30
- * Commit, do not mess with rakefile, version, or history.
31
- * Send me a pull request.
32
-
33
-
34
- Author
35
- ------
36
-
37
- Ingemar Edsborn (ingemar@xox.se)
38
-
39
-
40
- Copyright
41
- ---------
42
-
43
- Copyright (c) 2010 Ingemar Edsborn. See LICENSE for details
23
+ anon = Person.new
24
+ anon.valid? # => false
25
+ anon.errors # => {:name=>["can't be blank"]}>
26
+ ```
data/lib/entasis/base.rb CHANGED
@@ -1,36 +1,5 @@
1
1
  module Entasis
2
2
  class Base
3
- include ActiveModel::Validations
4
-
5
- class << self
6
- # Takes a list of symbolized attribute names and defines them
7
- def attributes(*attrs)
8
- @@attribute_names = attrs.map(&:to_s)
9
- attr_accessor(*attrs)
10
- end
11
- end
12
-
13
- # Takes a hash and assigns keys and values to it's attributes members
14
- def initialize(*args)
15
- if args.first.is_a?(Hash) then set_attributes_from_hash(args.first) else super(*args) end
16
- end
17
-
18
- def attribute_names
19
- @@attribute_names
20
- end
21
-
22
- # Returns an attributes hash
23
- def attributes
24
- attrs = {}
25
- attribute_names.each { |name| attrs[name] = send(name) }
26
- attrs
27
- end
28
-
29
- private
30
- # Takes each key and value from the hash and assigns it to it's attribute.
31
- def set_attributes_from_hash(hash)
32
- hash.each_pair { |key, value| self.send("#{key}=", value) }
33
- end
3
+ include Entasis::Model
34
4
  end
35
5
  end
36
-
@@ -0,0 +1,46 @@
1
+ module Entasis
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ include ActiveModel::Validations
7
+
8
+ self.class_eval 'class UnknownAttributeError < StandardError; end'
9
+ end
10
+
11
+ module ClassMethods
12
+ # Takes a list of symbolized attribute names and defines them
13
+ def attributes(*attrs)
14
+ class_variable_set :@@attribute_names, attrs.map(&:to_s)
15
+
16
+ attr_accessor *attrs
17
+ end
18
+ end
19
+
20
+ # Takes a hash and assigns keys and values to it's attributes members
21
+ def initialize(hash)
22
+ self.attributes = hash
23
+ end
24
+
25
+ def attribute_names
26
+ self.class.class_variable_get :@@attribute_names
27
+ end
28
+
29
+ def attributes=(hash)
30
+ hash.each do |name, value|
31
+ if attribute_names.include?(name.to_s) || self.respond_to?(name)
32
+ self.send("#{name}=", value)
33
+ else
34
+ raise self.class::UnknownAttributeError, "unkown attribute \"#{name}\""
35
+ end
36
+ end
37
+ end
38
+
39
+ # Returns an attributes hash
40
+ def attributes
41
+ attrs = {}
42
+ attribute_names.each { |name| attrs[name] = send(name) }
43
+ attrs
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module Entasis
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0.pre1"
3
3
  end
data/lib/entasis.rb CHANGED
@@ -1,5 +1,7 @@
1
+ require 'active_support'
1
2
  require 'active_model'
2
3
 
3
4
  module Entasis
5
+ autoload :Model, 'entasis/model'
4
6
  autoload :Base, 'entasis/base'
5
7
  end
data/spec/entasis_spec.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
  require File.expand_path(File.dirname(__FILE__) + '/person')
3
3
 
4
- describe "Entasis::Base" do
4
+ describe "Entasis::Model" do
5
5
  let(:hilda) { hilda = Person.new(:name => 'Hilda', :age => '23', :city => 'Berlin') }
6
6
 
7
7
  describe "#new" do
@@ -10,6 +10,12 @@ describe "Entasis::Base" do
10
10
  hilda.age.should == 23
11
11
  hilda.city.should == 'Berlin'
12
12
  end
13
+
14
+ it "raises an error if attribute is unknown" do
15
+ expect {
16
+ Person.new(undefined: 'value')
17
+ }.to raise_error Person::UnknownAttributeError, 'unkown attribute "undefined"'
18
+ end
13
19
  end
14
20
 
15
21
  describe "#attribute_names" do
data/spec/person.rb CHANGED
@@ -1,4 +1,6 @@
1
- class Person < Entasis::Base
1
+ class Person
2
+ include Entasis::Model
3
+
2
4
  attributes :name, :age, :city
3
5
 
4
6
  validates_presence_of :name
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: entasis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
5
- prerelease:
4
+ version: 0.3.0.pre1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ingemar Edsborn
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-24 00:00:00.000000000 Z
12
+ date: 2012-08-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -52,6 +52,7 @@ extensions: []
52
52
  extra_rdoc_files: []
53
53
  files:
54
54
  - lib/entasis/base.rb
55
+ - lib/entasis/model.rb
55
56
  - lib/entasis/version.rb
56
57
  - lib/entasis.rb
57
58
  - Gemfile
@@ -77,9 +78,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
78
  required_rubygems_version: !ruby/object:Gem::Requirement
78
79
  none: false
79
80
  requirements:
80
- - - ! '>='
81
+ - - ! '>'
81
82
  - !ruby/object:Gem::Version
82
- version: '0'
83
+ version: 1.3.1
83
84
  requirements: []
84
85
  rubyforge_project:
85
86
  rubygems_version: 1.8.23