sphene 0.1.0 → 0.2.0

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
  SHA256:
3
- metadata.gz: bf99d47c7026f5c4a64341060d3787ab4ee20533593168251ddb0164b5b1f3d6
4
- data.tar.gz: db7835e3e50dfbde58e3a777a5c4e381e889ca01000184f08f3b5318f2f6fa26
3
+ metadata.gz: 91b76824fa0ecc2418e1b299e66df90c20c73ee7f1fba1563f98f720ce5ad69c
4
+ data.tar.gz: 12e1c20dc77847a0fc65373d21ecff8e0d7569657edd03f092e91049ea4daca6
5
5
  SHA512:
6
- metadata.gz: d820e9547b57520d64672644c663f7a5e4a99962a73dee1a61831b50617b70017be4b2bb2bddd5a89309c56be43c4ff9d1409acf39c7a6d90d0dda4ee91cee2f
7
- data.tar.gz: f2ecbb779eba882e16dbc682e3548ab9595c89c24a3e77077ae5dcb5869608be8ca011a924598d7e8369be08992b8eb16e11879bc116a86fb08abc5573a3d1b9
6
+ metadata.gz: d95f5e80b28e111ea3cfce0c9ffb92ba2990ecd24bbaad06f17c51ea02a3b459a28bd90aca039627375ed55d2b4203f883622dd2098a5542dd3dec6a8c6e0dbe
7
+ data.tar.gz: 7a9da9ed8c9936517d2948a315c86473d0205c7c59ef6e1a61f31801b2858b077cb63b59c3bd31f57d933897d026f8c42470428d60bc437a660cf0c3e27281a2
data/.rubocop.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.6
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.7
3
4
 
4
5
  Style/StringLiterals:
5
6
  Enabled: true
@@ -9,11 +10,11 @@ Style/StringLiteralsInInterpolation:
9
10
  Enabled: true
10
11
  EnforcedStyle: double_quotes
11
12
 
12
- Style/Documentation:
13
- Enabled: false
14
-
15
13
  Layout/LineLength:
16
14
  Max: 120
17
15
 
16
+ Style/Documentation:
17
+ Enabled: false
18
+
18
19
  Layout/EmptyLineAfterGuardClause:
19
20
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,4 +1,8 @@
1
- ## [Unreleased]
1
+ ## Sphene
2
+
3
+ ## [0.2.0] - 2022-04-01
4
+
5
+ - Add ability to use Procs as default values.
2
6
 
3
7
  ## [0.1.0] - 2021-12-10
4
8
 
@@ -2,34 +2,39 @@
2
2
 
3
3
  module Sphene
4
4
  class Attribute
5
- attr_reader :name, :type, :default
5
+ attr_reader :name, :model, :type, :default
6
6
 
7
- def initialize(name, type: Types::Default, default: nil)
7
+ def initialize(name, model, type: Types::Default, default: nil)
8
8
  @name = name
9
+ @model = model
9
10
  @type = type
10
11
  @default = default
11
12
  end
12
13
 
13
14
  def value
14
- return default unless ivar_defined?(:@value_before_cast)
15
+ return default_value unless ivar_defined?(:@value_before_cast)
15
16
  return @value if ivar_defined?(:@value)
16
- @value = type.cast(value_to_cast)
17
+ @value = type.cast(@value_before_cast)
17
18
  end
18
19
 
19
20
  def value=(value)
20
- remove_instance_variable(:@value) if ivar_defined?(:@value)
21
+ remove_ivar(:@value) if ivar_defined?(:@value)
21
22
  @value_before_cast = value
22
23
  end
23
24
 
24
25
  private
25
26
 
26
- def value_to_cast
27
- ivar_defined?(:@value_before_cast) ?
28
- @value_before_cast : default
27
+ def default_value
28
+ return default unless default.respond_to?(:call)
29
+ model.instance_exec(&default)
29
30
  end
30
31
 
31
32
  def ivar_defined?(name)
32
33
  instance_variable_defined?(name)
33
34
  end
35
+
36
+ def remove_ivar(name)
37
+ remove_instance_variable(name)
38
+ end
34
39
  end
35
40
  end
@@ -4,17 +4,17 @@ require "delegate"
4
4
 
5
5
  module Sphene
6
6
  class AttributeSet < SimpleDelegator
7
- attr_reader :object
7
+ attr_reader :model
8
8
 
9
- def initialize(object)
10
- @object = object
9
+ def initialize(model)
10
+ @model = model
11
11
  super({})
12
12
  end
13
13
 
14
- def self.from(object)
15
- new(object).tap do |data|
16
- object.attributes.each do |name, options|
17
- data[name] = Attribute.new(name, **options)
14
+ def self.from(model)
15
+ new(model).tap do |data|
16
+ model.class.attributes.each do |name, options|
17
+ data[name] = Attribute.new(name, model, **options)
18
18
  end
19
19
  end
20
20
  end
@@ -17,14 +17,14 @@ module Sphene
17
17
  end
18
18
 
19
19
  def read_attribute(name)
20
- ensure_attribute(name) do |name|
21
- @attributes[name].value
20
+ ensure_attribute(name) do |attr|
21
+ @attributes[attr].value
22
22
  end
23
23
  end
24
24
 
25
25
  def write_attribute(name, value)
26
- ensure_attribute(name) do |name|
27
- @attributes[name].value = value
26
+ ensure_attribute(name) do |attr|
27
+ @attributes[attr].value = value
28
28
  end
29
29
  end
30
30
 
@@ -41,7 +41,7 @@ module Sphene
41
41
  private
42
42
 
43
43
  def initialize_attributes
44
- @attributes = AttributeSet.from(self.class)
44
+ @attributes = AttributeSet.from(self)
45
45
  end
46
46
 
47
47
  def ensure_attribute(name)
@@ -50,9 +50,8 @@ module Sphene
50
50
  end
51
51
 
52
52
  def assert_attribute(name)
53
- unless @attributes.key?(name.to_sym)
54
- raise InvalidAttributeNameError.new(name)
55
- end
53
+ @attributes.key?(name.to_sym) ||
54
+ (raise InvalidAttributeNameError, name)
56
55
  end
57
56
 
58
57
  module ClassMethods
@@ -65,6 +64,7 @@ module Sphene
65
64
  end
66
65
 
67
66
  def inherited(base)
67
+ super
68
68
  base.instance_variable_set(:@attributes, attributes.dup)
69
69
  end
70
70
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sphene
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/sphene.gemspec CHANGED
@@ -8,17 +8,18 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ["Alejandro Gutiérrez"]
9
9
  spec.email = ["alejandrodevs@gmail.com"]
10
10
 
11
- spec.summary = "A lightweight gem for class attributes"
12
- spec.description = "A lightweight gem for class attributes"
11
+ spec.summary = "Simple and lightweight gem for class attributes."
12
+ spec.description = "Simple and lightweight gem for class attributes."
13
13
  spec.homepage = "https://github.com/alejandrodevs/sphene"
14
14
  spec.license = "MIT"
15
- spec.required_ruby_version = ">= 2.6.0"
15
+ spec.required_ruby_version = ">= 2.7.0"
16
16
 
17
- #spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
18
 
19
19
  spec.metadata["homepage_uri"] = spec.homepage
20
20
  spec.metadata["source_code_uri"] = spec.homepage
21
21
  spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/master/CHANGELOG.md"
22
+ spec.metadata["rubygems_mfa_required"] = "true"
22
23
 
23
24
  # Specify which files should be added to the gem when it is released.
24
25
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sphene
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alejandro Gutiérrez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-19 00:00:00.000000000 Z
11
+ date: 2022-04-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A lightweight gem for class attributes
13
+ description: Simple and lightweight gem for class attributes.
14
14
  email:
15
15
  - alejandrodevs@gmail.com
16
16
  executables: []
@@ -54,6 +54,7 @@ metadata:
54
54
  homepage_uri: https://github.com/alejandrodevs/sphene
55
55
  source_code_uri: https://github.com/alejandrodevs/sphene
56
56
  changelog_uri: https://github.com/alejandrodevs/sphene/blob/master/CHANGELOG.md
57
+ rubygems_mfa_required: 'true'
57
58
  post_install_message:
58
59
  rdoc_options: []
59
60
  require_paths:
@@ -62,15 +63,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
63
  requirements:
63
64
  - - ">="
64
65
  - !ruby/object:Gem::Version
65
- version: 2.6.0
66
+ version: 2.7.0
66
67
  required_rubygems_version: !ruby/object:Gem::Requirement
67
68
  requirements:
68
69
  - - ">="
69
70
  - !ruby/object:Gem::Version
70
71
  version: '0'
71
72
  requirements: []
72
- rubygems_version: 3.2.32
73
+ rubygems_version: 3.3.7
73
74
  signing_key:
74
75
  specification_version: 4
75
- summary: A lightweight gem for class attributes
76
+ summary: Simple and lightweight gem for class attributes.
76
77
  test_files: []