attribeauty 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e210c51252c3ef22e45a6c8c2d544725a9ebfd521f34ecfcc66856eae73c1c15
4
+ data.tar.gz: 9787f43ec2bb540e09e2704c1fad4a0cb5601f6af493bbfd824d4b99090f47b3
5
+ SHA512:
6
+ metadata.gz: 0c7ebda74a3daa2cd25f5ab394a6fe464ddded6f7e2111304f63087d70a0de93e62c0630d6ae6d2a36501701109f55f475d6d5eb567646245f09c840c38529c9
7
+ data.tar.gz: 38ccdac185319b5999c23cfde69d971d5c294b5f0d7fccbc52394f31b1a6d8b9f432342728bd01567f339f74b0e7db2070031a25e352a12cd7490975c9d296e3
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-08-09
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in attribeauty.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ attribeauty (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ json (2.6.3)
11
+ language_server-protocol (3.17.0.3)
12
+ minitest (5.18.1)
13
+ parallel (1.23.0)
14
+ parser (3.2.2.3)
15
+ ast (~> 2.4.1)
16
+ racc
17
+ racc (1.7.1)
18
+ rainbow (3.1.1)
19
+ rake (13.0.6)
20
+ regexp_parser (2.8.1)
21
+ rexml (3.2.6)
22
+ rubocop (1.54.2)
23
+ json (~> 2.3)
24
+ language_server-protocol (>= 3.17.0)
25
+ parallel (~> 1.10)
26
+ parser (>= 3.2.2.3)
27
+ rainbow (>= 2.2.2, < 4.0)
28
+ regexp_parser (>= 1.8, < 3.0)
29
+ rexml (>= 3.2.5, < 4.0)
30
+ rubocop-ast (>= 1.28.0, < 2.0)
31
+ ruby-progressbar (~> 1.7)
32
+ unicode-display_width (>= 2.4.0, < 3.0)
33
+ rubocop-ast (1.29.0)
34
+ parser (>= 3.2.1.0)
35
+ ruby-progressbar (1.13.0)
36
+ unicode-display_width (2.4.2)
37
+
38
+ PLATFORMS
39
+ x86_64-darwin-20
40
+
41
+ DEPENDENCIES
42
+ attribeauty!
43
+ minitest (~> 5.0)
44
+ rake (~> 13.0)
45
+ rubocop (~> 1.21)
46
+
47
+ BUNDLED WITH
48
+ 2.4.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Toby
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # Attribeauty
2
+
3
+ I just wanted a quick, simple way to initialize mutable objects. This is it.
4
+ There are so many of these, but none were what I wanted.
5
+
6
+ ## Installation
7
+
8
+ Install the gem and add to the application's Gemfile by executing:
9
+
10
+ $ bundle add attribeauty
11
+
12
+ If bundler is not being used to manage dependencies, install the gem by executing:
13
+
14
+ $ gem install attribeauty
15
+
16
+ ## Usage
17
+
18
+ Inherit from `Attribeauty::Base` and then add attribute with the type you want.
19
+ Initialize with these and they will be cast to that attribute.
20
+ Use `assign_attributes` to update the object.
21
+
22
+
23
+ ```
24
+ class MyClass < Attribeauty::Base
25
+ attribute :first, :string
26
+ attribute :second, :integer
27
+ attribute :third, :float
28
+ attribute :forth, :boolean
29
+ attribute :fifth, :time
30
+ attribute :sixth, :koala
31
+ end
32
+
33
+ instance = MyClass.new(first: 456)
34
+ instance.first # => "456"
35
+ instance.assign_attributes(second: "456")
36
+ instance.second # => 456
37
+ instance.first = 9000
38
+ instance.first # => "9000"
39
+ ```
40
+
41
+ To add your own types, simply have a class that handles `MyClassName.new.cast(value)`:
42
+
43
+ ```
44
+ Attribeauty.configure do |config|
45
+ config.types[:koala] = MyTypes::Koala
46
+ end
47
+
48
+ module MyTypes
49
+ class Koala
50
+ def cast(value)
51
+ value.inspect.to_s << "_koalas"
52
+ end
53
+ end
54
+ end
55
+
56
+ class MyClass < Attribeauty::Base
57
+ attribute :wild_animal, :koala
58
+ end
59
+
60
+ instance = MyClass.new(wild_animal: "the_wildest_animals_are")
61
+ instance.wild_animal # => "the_wildest_animals_are_koalas"
62
+
63
+ ```
64
+
65
+ Note, failing to call `inspect` on the value when casting is likely to raise an error.
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
71
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tobyond/attribeauty.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Attribeauty
4
+ # base class to inherit from
5
+ # class MyClass < Attribeauty::Base
6
+ class Base
7
+ def self.attribute(name, type)
8
+ @attributes ||= {}
9
+ return if @attributes[name]
10
+
11
+ @attributes[name] = type
12
+
13
+ class_eval(<<-CODE, __FILE__, __LINE__ + 1)
14
+ def #{name}=(value); @#{name} = Cast.cast(value, #{type.inspect}); end
15
+
16
+ def #{name};@#{name};end
17
+ CODE
18
+
19
+ return unless type == :boolean
20
+
21
+ class_eval(<<-CODE, __FILE__, __LINE__ + 1)
22
+ def #{name}?; !!#{name}; end
23
+ CODE
24
+ end
25
+
26
+ def initialize(**attributes)
27
+ attributes.each do |key, value|
28
+ method = "#{key}=".to_sym
29
+ public_send(method, value)
30
+ end
31
+ end
32
+
33
+ def assign_attributes(**attributes)
34
+ attributes.each do |key, value|
35
+ method = "#{key}=".to_sym
36
+ public_send(method, value)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "types/string"
4
+ require_relative "types/integer"
5
+ require_relative "types/float"
6
+ require_relative "types/boolean"
7
+ require_relative "types/time"
8
+
9
+ module Attribeauty
10
+ # base cast for types
11
+ class Cast
12
+ BASE_TYPES = {
13
+ float: Types::Float,
14
+ integer: Types::Integer,
15
+ boolean: Types::Boolean,
16
+ time: Types::Time,
17
+ string: Types::String
18
+ }.freeze
19
+
20
+ def self.cast(value, type)
21
+ return nil if value.nil?
22
+
23
+ all_types = Attribeauty.configuration.types
24
+
25
+ raise ArgumentError, "#{type} not supported" if all_types[type].nil?
26
+
27
+ all_types[type].new.cast(value)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Attribeauty
4
+ # Attribeauty.configure do |config|
5
+ # config.types[:koala] = MyClass::Koala
6
+ # end
7
+ class Configuration
8
+ attr_accessor :types
9
+
10
+ def initialize
11
+ @types = Cast::BASE_TYPES.dup
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Attribeauty
4
+ module Types
5
+ # custom boolean type
6
+ class Boolean
7
+ FALSE_VALUES = [
8
+ false, 0,
9
+ "0", :"0",
10
+ "f", :f,
11
+ "F", :F,
12
+ "false", :false,
13
+ "FALSE", :FALSE,
14
+ "off", :off,
15
+ "OFF", :OFF
16
+ ].to_set.freeze
17
+
18
+ def cast(value)
19
+ !FALSE_VALUES.include?(value)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Attribeauty
4
+ module Types
5
+ # custom float type
6
+ class Float
7
+ def cast(value)
8
+ Float(value)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Attribeauty
4
+ module Types
5
+ # custom integer type
6
+ class Integer
7
+ def cast(value)
8
+ Integer(value)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Attribeauty
4
+ module Types
5
+ # custom string type
6
+ class String
7
+ def cast(value)
8
+ String(value)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Attribeauty
4
+ module Types
5
+ # custom Time type
6
+ class Time
7
+ def cast(value)
8
+ case value
9
+ when Time
10
+ value
11
+ when Date, DateTime
12
+ value.to_time
13
+ when Integer
14
+ ::Time.at(value / 1000.0)
15
+ when Numeric
16
+ ::Time.at(value)
17
+ else
18
+ ::Time.parse(value)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Attribeauty
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "attribeauty/version"
4
+ require_relative "attribeauty/base"
5
+ require_relative "attribeauty/cast"
6
+ require_relative "attribeauty/configuration"
7
+ require "date"
8
+ require "time"
9
+
10
+ # Module
11
+ module Attribeauty
12
+ class Error < StandardError; end
13
+
14
+ class << self
15
+ def configuration
16
+ @configuration ||= Configuration.new
17
+ end
18
+
19
+ def configure
20
+ yield(configuration)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ module Attribeauty
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attribeauty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Toby
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-08-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: There are so many of these, I just needed this one.
14
+ email:
15
+ - toby@darkroom.tech
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rubocop.yml"
21
+ - CHANGELOG.md
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/attribeauty.rb
28
+ - lib/attribeauty/base.rb
29
+ - lib/attribeauty/cast.rb
30
+ - lib/attribeauty/configuration.rb
31
+ - lib/attribeauty/types/boolean.rb
32
+ - lib/attribeauty/types/float.rb
33
+ - lib/attribeauty/types/integer.rb
34
+ - lib/attribeauty/types/string.rb
35
+ - lib/attribeauty/types/time.rb
36
+ - lib/attribeauty/version.rb
37
+ - sig/attribeauty.rbs
38
+ homepage: https://github.com/tobyond/attribeauty
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ homepage_uri: https://github.com/tobyond/attribeauty
43
+ source_code_uri: https://github.com/tobyond/attribeauty
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.6.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.4.10
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Attributes simply done
63
+ test_files: []