Skip to content

Allow multiple attributes to be defined in single cattr/iattr call#2

Merged
bnlucas merged 4 commits intomainfrom
define_multiple
Apr 23, 2025
Merged

Allow multiple attributes to be defined in single cattr/iattr call#2
bnlucas merged 4 commits intomainfrom
define_multiple

Conversation

@bnlucas
Copy link
Owner

@bnlucas bnlucas commented Apr 23, 2025

Adds the ability to define multiple attributes at once.

class Config
  include Cattri

  cattr :a, :b.                          # defines self.a/a=, self.b/b= defaulting to nil.
  cattr_reader :c, :d, default: false    # defines self.c, self.d readers, defaulting to false.

  cattr :e, :f do |val|                  # raises Cattri::AmbiguousBlockError, blocks not allow
    val                                  # for multiple attribute definitions.
  end
class Config
  include Cattri

  iattr :a, :b                           # defines new.a/a=, new.b/b= defaulting to nil.
  iattr_reader :c, :d, default: false    # defines new.a, new.b readers, defaulting to false.
  iattr_writer :e, :f                    # defines new.a, new.b writers, setters are not allowed.

  iattr :e, :f do |val|                  # raises Cattri::AmbiguousBlockError, blocks not allow
    val                                  # for multiple attribute definitions.
  end
end

Due to multiple attribute definitions not allowing an ambiguous setter block to provided, this also introduces class_attribute_setter/cattr_setter and instance_attribute_setter/iattr_setter

class Config
  include Cattri

  cattr :a, :b                          # defines def (a|b)=(v); @(a|b) = v; end

  cattr_setter :a do |val|              # applies a setter to the :a variable, changing it
    val.to_s.downcase.to_sym            # to def a=(v); @a = v.to_s.downcase.to_sym; end
  end

@codecov-commenter
Copy link

codecov-commenter commented Apr 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (df2777b) to head (499cf43).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main        #2   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9         9           
  Lines          278       311   +33     
  Branches        27        34    +7     
=========================================
+ Hits           278       311   +33     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bnlucas bnlucas merged commit 23f5b6e into main Apr 23, 2025
1 check passed
@bnlucas bnlucas deleted the define_multiple branch April 23, 2025 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants