yukata 1.2.0 → 1.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d7b57e16a64efce928a8711ebd9d5e5eb365b369
4
- data.tar.gz: 0d2286cb23cc7d2f990dbe80bf76320186a74bab
3
+ metadata.gz: 47de67ec0d74b7c728b0dc79e75593a28e13b6e0
4
+ data.tar.gz: 6cd1408ee0ae9f0a16c6ac9001a9ed44feb9f6e2
5
5
  SHA512:
6
- metadata.gz: f73c161f1afbead3c48916976e0bc775c2741bdce4a94e03a737ecbee2d26ed30b12fd27c358e561f892543aeb7f5e1466363a185e67b9749eda1df5043a895a
7
- data.tar.gz: 68749b61090828c39497aac9bb26d281b5ece1ad4cd19faa9810782591e3962610a158bb3100d749499562b541e11294841a9a3b439c277566615938893b4f4b
6
+ metadata.gz: 4cbe5ea69916821c14b0206237c6ec6b127067fae342043bb0a6a0b9000827479a06401ca16b69f6b371de7b82fc481dbaaeafef5baeeaa3b2ce38f1b7cd99ad
7
+ data.tar.gz: 413d548b175d35a8b8cda4e7cde9c321716d367d610eb1f7bba0bcdf29c77cedd67fdd23b7b36885354e3f58397435c27d2596a9d1e4939afdf57fe1c60b987f
data/README.md CHANGED
@@ -13,9 +13,13 @@ class Author < Yukata::Base
13
13
  end
14
14
 
15
15
  class Book < Yukata::Base
16
- attribute :title, String
17
- attribute :author, Author
18
- attribute :created_at, Date, default: -> { Date.today }
16
+ attribute :title, String
17
+ attribute :author, Author, coerce: false
18
+ attribute :created_at, Date, default: -> { Date.today }, writer: false
19
+
20
+ def author=(value)
21
+ @author = Author.new(value.to_h)
22
+ end
19
23
  end
20
24
 
21
25
  book = Book.new({
@@ -22,7 +22,7 @@ module Yukata
22
22
  def attributes
23
23
  hash = {}
24
24
  self.class.attributes.keys.each do |k|
25
- hash[k] = self.send(k)
25
+ hash[k] = send(k) if respond_to?(k)
26
26
  end
27
27
  hash
28
28
  end
@@ -35,30 +35,51 @@ module Yukata
35
35
  @attributes ||= {}
36
36
  end
37
37
 
38
+ # @override
39
+ def self.attr_accessor(*args)
40
+ args.each { |name| self.attributes[name] = Attribute.new(Object) }
41
+ super
42
+ end
43
+
38
44
  # Declares an attribute on the model
39
45
  #
40
46
  # @param name [String]
41
47
  # @param type [Class] a class that represents the type
42
48
  # @param options [Hash] extra options to apply to the attribute
43
49
  def self.attribute(name, type=String, options={})
50
+ config = { writer: true, reader: true, coerce: true }.merge(options)
51
+
44
52
  attr = Attribute.new(type, options)
45
53
  self.attributes[name] = attr
46
54
  variable = "@#{name}"
47
55
 
48
- define_method("#{name}=") do |object|
49
- val = Yukata.coercer.coerce(object, attr.type)
50
- instance_variable_set(variable, val)
56
+ # -------------
57
+ # Define Writer
58
+ # -------------
59
+ if config[:writer]
60
+ define_method("#{name}=") do |object|
61
+ if config[:coerce]
62
+ val = Yukata.coercer.coerce(object, attr.type)
63
+ instance_variable_set(variable, val)
64
+ else
65
+ instance_variable_set(variable, object)
66
+ end
67
+ end
51
68
  end
52
69
 
53
- define_method(name) do
54
- val = instance_variable_get(variable)
55
- unless val
56
- val = attr.default
57
- instance_variable_set(variable, val)
70
+ # -------------
71
+ # Define reader
72
+ # -------------
73
+ if config[:reader]
74
+ define_method(name) do
75
+ val = instance_variable_get(variable)
76
+ unless val
77
+ val = attr.default
78
+ instance_variable_set(variable, val)
79
+ end
80
+ val
58
81
  end
59
- val
60
82
  end
61
-
62
83
  end
63
84
  end
64
85
  end
@@ -1,3 +1,3 @@
1
1
  module Yukata
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -2,29 +2,70 @@ require 'spec_helper'
2
2
 
3
3
  describe Yukata::Base do
4
4
  class MockedBase < Yukata::Base
5
+ attr_accessor :qux
5
6
  attribute :id, Integer
6
7
  attribute :foo, String
7
- attribute :created_at, DateTime, default: -> { DateTime.now }
8
+ attribute :created_at, Date, default: -> { Date.today }
8
9
  end
9
10
 
11
+ let(:today) { Date.today }
12
+ let(:params) { { id: '1', foo: 'bar', created_at: today, qux: 'doh' } }
13
+
10
14
  describe '#initialize' do
11
15
  context 'when a hash is not passed' do
12
16
  subject { MockedBase.new }
13
17
 
14
- its(:id) { should be_nil }
15
- its(:foo) { should be_nil }
16
- its(:created_at) { should_not be_nil }
18
+ it 'sets id to nil' do
19
+ expect(subject.id).to be_nil
20
+ end
21
+
22
+ it 'sets foo to nil' do
23
+ expect(subject.foo).to be_nil
24
+ end
25
+
26
+ it 'sets qux to nil' do
27
+ expect(subject.qux).to be_nil
28
+ end
17
29
  end
18
30
 
19
31
  context 'when a hash is passed' do
20
- let(:now) { DateTime.now }
21
- let(:params) { { id: '1', foo: 'bar', created_at: now } }
22
-
23
32
  subject { MockedBase.new(params) }
24
33
 
25
- its(:id) { should eq(1) }
26
- its(:foo) { should eq('bar') }
27
- its(:created_at) { should eq(now) }
34
+ it 'sets id to 1' do
35
+ expect(subject.id).to eq(1)
36
+ end
37
+
38
+ it 'sets foo to "bar"' do
39
+ expect(subject.foo).to eq('bar')
40
+ end
41
+
42
+ it 'sets qux to "doh"' do
43
+ expect(subject.qux).to eq('doh')
44
+ end
45
+
46
+ it 'sets created_at to today' do
47
+ expect(subject.created_at).to eq(today)
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '#attributes' do
53
+ subject { MockedBase.new(params).attributes }
54
+
55
+ it 'contains the :id' do
56
+ expect(subject[:id]).to eq(1)
57
+ end
58
+
59
+ it 'contains the :foo' do
60
+ expect(subject[:foo]).to eq('bar')
61
+ end
62
+
63
+ it 'contains the :qux' do
64
+ expect(subject[:qux]).to eq('doh')
65
+ end
66
+
67
+ it 'contains the :created_at' do
68
+ expect(subject[:created_at]).to eq(today)
28
69
  end
29
70
  end
30
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yukata
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Johnston
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-28 00:00:00.000000000 Z
11
+ date: 2014-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler