Skip to content

Add alias and predicate support#4

Merged
bnlucas merged 3 commits intomainfrom
proxies
Apr 23, 2025
Merged

Add alias and predicate support#4
bnlucas merged 3 commits intomainfrom
proxies

Conversation

@bnlucas
Copy link
Owner

@bnlucas bnlucas commented Apr 23, 2025

This adds attribute aliasing and predicate support to both class- and instance-level attributes.

Alias Support

Defines an alias method that is not registered on the class context, just a simple proxy to the original method. An error is raised if the method you're trying to alias has not been defined.

class Config
  cattr :enabled, default: true     # iattr :enabled, default: true
  cattr_alias :allowed, :enabled    # iattr_alias :allowed, :enabled

  cattr_alias :alias_attr, :unknown # raises Cattri::AttributeNotDefinedError

  # Config.allowed -> def allowed; send(:enabled); end
end

Predicate support

The new predicate: true|false flag on cattr ... and iattr ... defines a proxy method of #{name}? that is not registered on the class context. This proxy calls !!send(:name).

A guard was also added in so attributes cannot be defined as predicates directly, they must use the new flag on a regular attribute.

class Config
  cattr :enabled?                                   # raises Cattri::AttributeError
  cattr :enabled, default: true, predicate: true    # iattr :enabled, default: true, predicate: true

  # Config.enabled? -> def enabled?; !!send(:enabled); end
end

@codecov
Copy link

codecov bot commented Apr 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (23f5b6e) to head (cae7ea7).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main        #4   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9         9           
  Lines          311       343   +32     
  Branches        34        44   +10     
=========================================
+ Hits           311       343   +32     

☔ 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 479470c into main Apr 23, 2025
3 checks passed
@bnlucas bnlucas deleted the proxies branch April 23, 2025 07:27
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.

1 participant