active_model-attribute 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d95747a10423b764cf2bf709a6fd40ac575da3cf
4
- data.tar.gz: 4f58b52e8e850770d93bbb6792394e0d488c45f9
3
+ metadata.gz: 1ed7ef5bddca113e053cd2edf3856bd665f37c52
4
+ data.tar.gz: de80657a0582837da4417a871004411323b2427f
5
5
  SHA512:
6
- metadata.gz: 704fc0632f75dbf3c28c93ab0580e2b2e3f16bb8da6cfdba0d0171ec889b22865e72a78878cb214e89ce90f38768b2b984d03812f071075d416fd58aae7b63f7
7
- data.tar.gz: b0d7700ceb94fb6ba6ba6cb0e81b23f3ea92032e80dfc693d3b3475b5cc97c3f0ac7b6fda2e3c0b2bc4302c7c7364ba9cc4fec8ed96851c60465bda5931fcc7b
6
+ metadata.gz: 1e632f92ade18c2728bdd8596c498e6aaae8beb01d61a5b0753288383e29ea8a8e71ef7b977e942a5839ad9cf8c611d9e0700e439f20b3a80db306d1a64d28d5
7
+ data.tar.gz: a74d10d8d2c23e61a5bd18406d21daff642accce9167cca61645a8157ed1f84a521b92d0fd1b12abbc8753d76aedc2642e813b7648cf36110921bfe6133f7b54
data/README.md CHANGED
@@ -0,0 +1,57 @@
1
+ # ActiveModel::Attribute [![Build Status](https://travis-ci.org/frodsan/active_model-attribute.svg)](https://travis-ci.org/frodsan/active_model-attribute)
2
+
3
+ Declares attributes for Active Model objects.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "active_model-attribute"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_model-attribute
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ class User
25
+ include ActiveModel::Attribute
26
+
27
+ attribute :name, :string
28
+ attribute :position, :string
29
+ attribute :age, :integer
30
+ attribute :active, :boolean
31
+
32
+ alias_attribute :full_name, :name
33
+ end
34
+
35
+ user = User.new(
36
+ name: 'Bob',
37
+ position: 'Developer',
38
+ age: '42',
39
+ active: false
40
+ )
41
+
42
+ user.age
43
+ # => 42
44
+
45
+ user.age.class
46
+ # => Integer
47
+
48
+ user.full_name
49
+ # => 'Bob'
50
+
51
+ user.active?
52
+ # => false
53
+ ```
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_support/concern"
4
- require "active_model/forbidden_attributes_protection"
5
- require "active_model/attribute_assignment"
4
+ require "active_model"
6
5
  require "active_model/type"
7
6
 
8
7
  module ActiveModel
@@ -10,42 +9,35 @@ module ActiveModel
10
9
  extend ActiveSupport::Concern
11
10
 
12
11
  included do
12
+ include ActiveModel::AttributeMethods
13
13
  include ActiveModel::AttributeAssignment
14
- end
15
14
 
16
- class_methods do
17
- def inherited(subclass)
18
- subclass.attributes.replace(attributes)
19
- end
15
+ class_attribute :attributes, :attribute_registry,
16
+ instance_accessor: false,
17
+ instance_predicate: false
20
18
 
21
- def attributes
22
- @attributes ||= []
23
- end
19
+ self.attributes = []
20
+ self.attribute_registry = {}
24
21
 
25
- def attribute(name, type, **options)
26
- attributes << name unless attributes.include?(name)
22
+ attribute_method_suffix '='
23
+ attribute_method_suffix '?'
24
+ end
25
+
26
+ class_methods do
27
+ def attribute(name, type, options = {})
28
+ name = name.to_s
27
29
 
28
- getter = name.to_sym
29
- setter = :"#{name}="
30
+ self.attribute_registry = attribute_registry.merge(name => [type, options])
30
31
 
31
- define_method(getter) do
32
- @attributes[getter]
33
- end
32
+ self.attributes = attribute_registry.keys
34
33
 
35
- define_method(setter) do |value|
36
- @attributes[getter] = ActiveModel::Type.lookup(type, **options).cast(value)
37
- end
38
- end
34
+ define_attribute_method(name)
39
35
 
40
- def alias_attribute(new_name, old_name)
41
- alias_method new_name, old_name
42
- alias_method :"#{new_name}=", :"#{old_name}="
36
+ attr_reader(name)
43
37
  end
44
38
  end
45
39
 
46
40
  def initialize(attrs = {})
47
- @attributes = {}
48
-
49
41
  assign_attributes(attrs) if attrs
50
42
 
51
43
  super()
@@ -60,5 +52,19 @@ module ActiveModel
60
52
  hash[name] = send(name)
61
53
  end
62
54
  end
55
+
56
+ private
57
+
58
+ def attribute=(attr, value)
59
+ type, options = self.class.attribute_registry[attr.to_s]
60
+
61
+ value = ActiveModel::Type.lookup(type, **options).cast(value)
62
+
63
+ instance_variable_set(:"@#{attr}", value)
64
+ end
65
+
66
+ def attribute?(attr)
67
+ send(attr).present?
68
+ end
63
69
  end
64
70
  end
@@ -17,6 +17,8 @@ class User
17
17
  attribute :description, :text
18
18
  attribute :age, :integer
19
19
  attribute :active, :boolean
20
+
21
+ alias_attribute :full_name, :name
20
22
  end
21
23
 
22
24
  class Admin < User
@@ -25,31 +27,51 @@ end
25
27
 
26
28
  class ActiveModelAttributeTest < Minitest::Test
27
29
  def test_class_attributes
28
- assert_equal User.attributes, [:name, :description, :age, :active]
30
+ assert_equal User.attributes, %w[name description age active]
29
31
  end
30
32
 
31
33
  def test_inheritance
32
- assert_equal Admin.attributes, User.attributes + [:sudo]
34
+ assert_equal Admin.attributes, User.attributes + %w[sudo]
33
35
  end
34
36
 
35
37
  def test_casting
36
38
  user = User.new(
37
- name: 'Francesco',
38
- description: 'Developer',
39
- age: '322',
40
- active: 'true'
39
+ name: "Francesco",
40
+ description: "Developer",
41
+ age: "322",
42
+ active: "true"
41
43
  )
42
44
 
43
- assert_equal 'Francesco', user.name
44
- assert_equal 'Developer', user.description
45
+ assert_equal "Francesco", user.name
46
+ assert_equal "Developer", user.description
45
47
  assert_equal 322, user.age
46
48
  assert_equal true, user.active
47
49
  end
48
50
 
49
51
  def test_attributes
50
- user = User.new(name: 'Francesco', description: 'Developer')
52
+ user = User.new(name: "Francesco", description: "Developer")
53
+
54
+ assert_equal "Francesco", user.attributes["name"]
55
+ assert_equal "Developer", user.attributes["description"]
56
+ end
57
+
58
+ def test_predicates
59
+ user = User.new(active: false)
60
+
61
+ refute user.active?
62
+ refute user.name?
63
+
64
+ user.name = "Francesco"
65
+ user.active = true
66
+
67
+ assert user.name?
68
+ assert user.active?
69
+ end
70
+
71
+ def test_alias
72
+ user = User.new(full_name: 'Francesco')
51
73
 
52
- assert_equal 'Francesco', user.attributes[:name]
53
- assert_equal 'Developer', user.attributes[:description]
74
+ assert user.full_name?
75
+ assert_equal user.full_name, user.name
54
76
  end
55
77
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model-attribute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francesco Rodríguez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-14 00:00:00.000000000 Z
11
+ date: 2017-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  version: '0'
110
110
  requirements: []
111
111
  rubyforge_project:
112
- rubygems_version: 2.6.11
112
+ rubygems_version: 2.6.13
113
113
  signing_key:
114
114
  specification_version: 4
115
115
  summary: Declares attribute for Active Model objects