makandra / modularity Goto Github PK
View Code? Open in Web Editor NEWTraits and partial classes for Ruby
Home Page: http://makandra.com/
License: MIT License
Traits and partial classes for Ruby
Home Page: http://makandra.com/
License: MIT License
... so since Rails 3 ? you can do:
# app/models/article.rb
class Article
include Shared::StripFields
strip_fields :name, :brand
end
module Shared::StripFields
extend ActiveSupport::Concern
module ClassMethods
def strip_fields(fields)
fields = fields.is_a?(Array) ? fields : [fields]
fields.each do |field|
define_method("#{field}=") do |value|
self[field] = value.strip
end
end
end
end
end
Class methods can be called through an included block and instance methods can be declared inside the mixin directly without an InstanceMethods module like this:
module Shared::MyMixin
extend ActiveSupport::Concern
included do
scope :by_type, lambda {|type| where(:type => type)}
has_many ...
end
module ClassMethods
...
end
def my_instance_method
...
end
end
ActiveSupport::Concern now also resolves mixin dependencies properly as described in its API doc.
P.S.: I also found no problems with the visibility of class and instance methods.
In a trait, I have a method that is using one of the trait arguments. In case the argument is not given, it falls back to a default value.
The unexpected: as soon as the default value is generated, it is stored at trait level and gets returned on each invocation. In other words, the default value is only evaluated once. In many cases, this will be no problem. However, the issue arises with a dynamic default value. See (and try) this Ruby script, where the default value is derived from the class name:
require 'modularity'
module DoesDemo
as_trait do |name: nil|
define_method :demo do
name ||= self.class.name
puts name
end
end
end
class Base
include DoesDemo
end
class One < Base
end
class Two < Base
end
One.new.demo # => One
Two.new.demo # => One # wrong
As you can see, the return value of any subclass's demo
method is the name of the first-defined class. (It would be the same with name ||= Time.now
: the defaulted timestamp is instantiated once and returned on each invocation of demo
.)
The distilled issue seems to be that assigning variables named like trait arguments will change state at trait level, i.e. modify the trait argument value.
demo
would always return the name of the current class (or the current time). Assigning a variable named like a trait argument should not change the value of the trait argument.
Avoiding to assign variables named like any of the trait arguments.
I am not sure whether this behavior is a real bug, or if it might be a feature of define_method
. Anyway, a solution that enables to ||=
-assign variables named like trait arguments would be appreciated.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.