lean-attributes 0.0.1 → 0.1.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,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c6b251b6f7693a6a788b8cf3f006658fe11e256c
4
- data.tar.gz: d117bf7bb0f11f7857516109bc0416a8ab6d639d
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Yzc3ZDljN2E0MGU2N2Y3ZTg2YWI5NzY4Y2M1NWMyYjIzM2E2MzJmMQ==
5
+ data.tar.gz: !binary |-
6
+ ZWY1NWQ4MGI3NjczYTdiZGRjODU4MjM0YjU5MWE1MzY5NDZlMDY1Ng==
5
7
  SHA512:
6
- metadata.gz: cddfdcbf8ee64247a616dc021339322f7aeb19fa7156fad8934eedf8fe005b5fbcbbf31b8fc9d9b0e2582be402282553495de9d87b0173c64f7193362634e4d3
7
- data.tar.gz: e2e7859b6340308338c77bcced6816d2d9f226912e134997e4b4bf06c5933915649aa7116f66037054abb57f4f6e89c724ba12c12bf971797c55cbb3b1263c02
8
+ metadata.gz: !binary |-
9
+ Mjk3OGIyNjBlNDMzMWU2ZGI3MzBlNWI0MzNjOGM0NTA0NjUwODU5N2RiMGUz
10
+ NjJkOTM3MTNkNzg1YzMyZWYyZGJiZmMzYzI3MTg5MjBkY2FhNmFjYzRjOWI5
11
+ OWJjM2U5YTk4NDg1MmQxN2JiYzlkZTBlMDMwYjEyOTgxZWY5Njk=
12
+ data.tar.gz: !binary |-
13
+ NzY1MGFmODVlNGI2Nzc4NWU1NTUwNGI4MDY4NWQ4NzE1YTNkM2Y0YmRjYzkw
14
+ MGI2ODQ0OTAxNDJiMmFlYTdhNTdjYTUwODFjNDNkOTEwMWU1MWY4MWU0OTQ3
15
+ YWNiYjQ3ZjZmMTNlMjA2YmRkYjA2YWMyYWQ3NDgxZDIwY2Q0MjY=
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Lean::Attributes
2
2
 
3
3
  ## Status
4
+ [![Gem Version](https://badge.fury.io/rb/lean-attributes.svg)](http://badge.fury.io/rb/lean-attributes)
4
5
  [![Build Status](https://travis-ci.org/lleolin/lean-attributes.svg)](https://travis-ci.org/lleolin/lean-attributes)
5
6
  [![Test Coverage](https://codeclimate.com/github/lleolin/lean-attributes/badges/coverage.svg)](https://codeclimate.com/github/lleolin/lean-attributes/coverage)
6
7
  [![Code Climate](https://codeclimate.com/github/lleolin/lean-attributes/badges/gpa.svg)](https://codeclimate.com/github/lleolin/lean-attributes)
7
8
  [![Dependency Status](https://gemnasium.com/lleolin/lean-attributes.svg)](https://gemnasium.com/lleolin/lean-attributes)
8
- [![Inline docs](http://inch-ci.org/github/lotus/model.svg?branch=master)](http://inch-ci.org/github/lleolin/lean-attributes)
9
+ [![Inline docs](http://inch-ci.org/github/lleolin/lean-attributes.svg?branch=master)](http://inch-ci.org/github/lleolin/lean-attributes)
@@ -0,0 +1,32 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'lean-attributes/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'lean-attributes'
8
+ s.version = Lean::Attributes::VERSION
9
+
10
+ s.platform = Gem::Platform::RUBY
11
+ s.required_ruby_version = '>= 1.9.3'
12
+ s.rubygems_version = '>= 1.3.6'
13
+
14
+ s.homepage = 'https://github.com/lleolin/lean-attributes'
15
+ s.authors = ['R. Elliott Mason']
16
+ s.email = ['r.elliott.mason@fastmail.fm']
17
+ s.summary = 'Lean attributes for Ruby classes'
18
+ s.description = <<-desc
19
+ lean-attributes allows you to define typed attributes for Ruby classes
20
+ desc
21
+
22
+ s.licenses = ['MIT']
23
+
24
+ s.files = `git ls-files -z -- lib/* bin/* LICENSE.md README.md \
25
+ CHANGELOG.md FEATURES.md lean-attributes.gemspec`.split("\x0")
26
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
27
+ s.extra_rdoc_files = ['README.md']
28
+ s.require_paths = ['lib']
29
+
30
+ s.add_development_dependency 'rake', '~> 10.3', '>= 10.3.2'
31
+ s.add_development_dependency 'rspec', '~> 3.0', '>= 3.0.0'
32
+ end
@@ -1,13 +1,22 @@
1
1
  require 'lean-attributes/attributes/class_methods'
2
- require 'lean-attributes/attributes/coercion'
2
+ require 'lean-attributes/attributes/coercion_helpers'
3
3
  require 'lean-attributes/attributes/initializer'
4
4
 
5
5
  module Lean
6
6
  module Attributes
7
7
  def self.included(base)
8
8
  base.class_eval do
9
- extend ClassMethods
10
- include Coercion
9
+ include Basic
10
+ include CoercionHelpers
11
+ include Initializer
12
+ end
13
+ end
14
+
15
+ module Basic
16
+ def self.included(base)
17
+ base.class_eval do
18
+ extend ClassMethods
19
+ end
11
20
  end
12
21
  end
13
22
  end
@@ -4,10 +4,9 @@ module Lean
4
4
  attr_reader :name
5
5
 
6
6
  def initialize(options = {})
7
- @default_value = options[:default_value]
8
- @name = options[:name]
9
- @parent_class = options[:parent_class]
10
- @type = options[:type].to_s
7
+ @default = options[:default]
8
+ @name = options[:name].to_sym
9
+ @type = options[:type].to_s.to_sym
11
10
  end
12
11
 
13
12
  def coercion_method
@@ -18,25 +17,20 @@ module Lean
18
17
  EOS
19
18
  end
20
19
 
21
- def coercible?
22
- return @coercible unless @coercible.nil?
23
-
24
- @coercible = @parent_class.method_defined?(coercion_method_name)
25
- end
26
-
27
20
  def coercion_method_name(from = nil)
28
- ['coerce', from, 'to', @type.downcase].compact.join('_')
21
+ ['coerce', from, 'to', @type.to_s.downcase].compact.join('_')
29
22
  end
30
23
 
31
- def default_value
32
- return "send(:#{@default_value})" if @default_value.is_a?(Symbol) &&
33
- !@type.is_a?(Symbol)
24
+ def default
25
+ if @default.is_a?(Symbol) && @type != :Symbol
26
+ return "send(:#{@default})"
27
+ end
34
28
 
35
- @default_value.inspect
29
+ @default.inspect
36
30
  end
37
31
 
38
32
  def getter_method
39
- return getter_method_with_default unless @default_value.nil?
33
+ return getter_method_with_default unless @default.nil?
40
34
 
41
35
  "attr_reader :#{@name}"
42
36
  end
@@ -44,35 +38,19 @@ module Lean
44
38
  def getter_method_with_default
45
39
  <<-EOS
46
40
  def #{name}
47
- @#{name} ||= #{default_value}
41
+ @#{name} ||= #{default}
48
42
  end
49
43
  EOS
50
44
  end
51
45
 
52
- def name
53
- @name.to_sym
54
- end
55
-
56
46
  def setter_method
57
47
  <<-EOS
58
48
  def #{name}=(value)
59
- #{setter_method_coercion}
49
+ value = #{coercion_method_name}(value) unless value.is_a?(#{@type})
60
50
  @#{name} = value
61
51
  end
62
52
  EOS
63
53
  end
64
-
65
- def setter_method_coercion
66
- return unless coercible?
67
-
68
- <<-EOS
69
- value = #{coercion_method_name}(value) unless value.is_a?(#{@type})
70
- EOS
71
- end
72
-
73
- # def type_coercion_method_name
74
- # @type_coercion_method_name ||= :"coerce_to_#{@type.downcase}"
75
- # end
76
54
  end
77
55
  end
78
56
  end
@@ -4,11 +4,13 @@ module Lean
4
4
  module Attributes
5
5
  module ClassMethods
6
6
  def attribute(name, type, options = {})
7
- attribute = Attribute.new(default_value: options[:default],
8
- name: name, parent_class: self, type: type)
7
+ attribute = Attribute.new(
8
+ default: options[:default],
9
+ name: name,
10
+ type: type
11
+ )
9
12
 
10
- class_eval(attribute.coercion_method, __FILE__, __LINE__ + 1) \
11
- if attribute.coercible?
13
+ class_eval(attribute.coercion_method, __FILE__, __LINE__ + 1)
12
14
  class_eval(attribute.getter_method, __FILE__, __LINE__ + 1)
13
15
  class_eval(attribute.setter_method, __FILE__, __LINE__ + 1)
14
16
 
@@ -1,6 +1,6 @@
1
1
  module Lean
2
2
  module Attributes
3
- module Coercion
3
+ module CoercionHelpers
4
4
  def coerce_to_array(value)
5
5
  Array(value) unless value.nil?
6
6
  end
@@ -3,7 +3,7 @@ module Lean
3
3
  module Initializer
4
4
  def initialize(attributes = {})
5
5
  attributes.each do |name, value|
6
- send("#{name}=", value)
6
+ send(:"#{name}=", value)
7
7
  end
8
8
  end
9
9
  end
@@ -1,5 +1,5 @@
1
1
  module Lean
2
2
  module Attributes
3
- VERSION = '0.0.1'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,57 +1,58 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lean-attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - R. Elliott Mason
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-08 00:00:00.000000000 Z
11
+ date: 2015-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '10.3'
20
- - - ">="
20
+ - - ! '>='
21
21
  - !ruby/object:Gem::Version
22
22
  version: 10.3.2
23
23
  type: :development
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - "~>"
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: '10.3'
30
- - - ">="
30
+ - - ! '>='
31
31
  - !ruby/object:Gem::Version
32
32
  version: 10.3.2
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: rspec
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - "~>"
37
+ - - ~>
38
38
  - !ruby/object:Gem::Version
39
39
  version: '3.0'
40
- - - ">="
40
+ - - ! '>='
41
41
  - !ruby/object:Gem::Version
42
42
  version: 3.0.0
43
43
  type: :development
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - "~>"
47
+ - - ~>
48
48
  - !ruby/object:Gem::Version
49
49
  version: '3.0'
50
- - - ">="
50
+ - - ! '>='
51
51
  - !ruby/object:Gem::Version
52
52
  version: 3.0.0
53
- description: |
54
- lean-attributes allows you to define coerced attributes for Ruby classes
53
+ description: ! 'lean-attributes allows you to define typed attributes for Ruby classes
54
+
55
+ '
55
56
  email:
56
57
  - r.elliott.mason@fastmail.fm
57
58
  executables: []
@@ -59,18 +60,15 @@ extensions: []
59
60
  extra_rdoc_files:
60
61
  - README.md
61
62
  files:
62
- - LICENSE
63
63
  - README.md
64
+ - lean-attributes.gemspec
64
65
  - lib/lean-attributes.rb
65
66
  - lib/lean-attributes/attributes.rb
66
67
  - lib/lean-attributes/attributes/attribute.rb
67
68
  - lib/lean-attributes/attributes/class_methods.rb
68
- - lib/lean-attributes/attributes/coercion.rb
69
+ - lib/lean-attributes/attributes/coercion_helpers.rb
69
70
  - lib/lean-attributes/attributes/initializer.rb
70
71
  - lib/lean-attributes/version.rb
71
- - spec/fixtures.rb
72
- - spec/lean_attributes_spec.rb
73
- - spec/spec_helper.rb
74
72
  homepage: https://github.com/lleolin/lean-attributes
75
73
  licenses:
76
74
  - MIT
@@ -81,12 +79,12 @@ require_paths:
81
79
  - lib
82
80
  required_ruby_version: !ruby/object:Gem::Requirement
83
81
  requirements:
84
- - - ">="
82
+ - - ! '>='
85
83
  - !ruby/object:Gem::Version
86
84
  version: 1.9.3
87
85
  required_rubygems_version: !ruby/object:Gem::Requirement
88
86
  requirements:
89
- - - ">="
87
+ - - ! '>='
90
88
  - !ruby/object:Gem::Version
91
89
  version: '0'
92
90
  requirements: []
@@ -95,7 +93,4 @@ rubygems_version: 2.4.5
95
93
  signing_key:
96
94
  specification_version: 4
97
95
  summary: Lean attributes for Ruby classes
98
- test_files:
99
- - spec/fixtures.rb
100
- - spec/lean_attributes_spec.rb
101
- - spec/spec_helper.rb
96
+ test_files: []
data/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) [year] [fullname]
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
data/spec/fixtures.rb DELETED
@@ -1,37 +0,0 @@
1
- require 'bigdecimal'
2
-
3
- class Medium
4
- include Lean::Attributes
5
-
6
- attribute :authors, Array
7
- attribute :finished, DateTime
8
- attribute :price, BigDecimal
9
- attribute :published, Date
10
- attribute :rate, Float
11
- attribute :sold, Time
12
- attribute :title, String
13
- end
14
-
15
- class Book < Medium
16
- attribute :format, Symbol, default: :hardcover
17
- attribute :pages, Integer
18
- end
19
-
20
- class Author
21
- include Lean::Attributes
22
- include Lean::Attributes::Initializer
23
-
24
- attribute :age, Integer
25
- attribute :name, String
26
- end
27
-
28
- class ReadingProgress
29
- include Lean::Attributes
30
-
31
- attribute :date, Time, default: :time_now
32
- attribute :page, Integer, default: 1
33
-
34
- def time_now
35
- Time.new('2015-09-08').utc
36
- end
37
- end
@@ -1,48 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'Lean::Attributes' do
4
- context 'inclusive class' do
5
- let(:book) do
6
- Book.new.tap do |book|
7
- book.title = :"War and Peace"
8
- book.pages = '1'
9
- book.authors = 'Leo Tolstoy'
10
- book.published = '1869-01-01'
11
- book.sold = '2015-09-08'
12
- book.finished = '2015-09-10'
13
- book.price = 10.00
14
- book.format = 'paperback'
15
- end
16
- end
17
-
18
- it 'has no initializer' do
19
- expect { Book.new(title: 'War and Peace') } \
20
- .to raise_error(ArgumentError)
21
- end
22
-
23
- it 'has coercible and writable attributes' do
24
- expect(book.title).to match 'War and Peace'
25
- expect(book.pages).to eq 1
26
- expect(book.authors).to match_array ['Leo Tolstoy']
27
- expect(book.published).to eq Date.parse('1869-01-01')
28
- expect(book.sold).to eq Time.new('2015-09-08')
29
- expect(book.price).to be_kind_of BigDecimal
30
- expect(book.price).to eq 10.00
31
- expect(book.format).to eq :paperback
32
- end
33
- end
34
-
35
- context 'inclusive class with Initializer' do
36
- let(:author) { Author.new(name: 'Elliott Mason', age: 30) }
37
-
38
- it { expect(author.name).to match 'Elliott Mason' }
39
- it { expect(author.age).to eq 30 }
40
- end
41
-
42
- context 'inclusive class with attribute defaults' do
43
- let(:reading_progress) { ReadingProgress.new }
44
-
45
- it { expect(reading_progress.page).to eq 1 }
46
- it { expect(reading_progress.date).to be_kind_of Time }
47
- end
48
- end
data/spec/spec_helper.rb DELETED
@@ -1,21 +0,0 @@
1
- require 'simplecov'
2
- require 'codeclimate-test-reporter'
3
-
4
- formatters = [SimpleCov::Formatter::HTMLFormatter]
5
- formatters << CodeClimate::TestReporter::Formatter if ENV['TRAVIS']
6
- SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[*formatters]
7
- SimpleCov.start do
8
- add_filter '/.bundle'
9
- add_filter '/spec'
10
- end
11
-
12
- require 'rspec'
13
-
14
- RSpec.configure do |config|
15
- config.before(:each) do |example|
16
- SimpleCov.command_name(example.location)
17
- end
18
- end
19
-
20
- require 'lean-attributes'
21
- require 'fixtures.rb'