Giter Site home page Giter Site logo

modularity's People

Contributors

5v3n avatar brunosedler avatar denzelem avatar kraatob avatar kratob avatar niklas-hasselmeyer avatar stemps avatar triskweline avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

modularity's Issues

Does ActiveSupport::Concern solve the problem?

... so since Rails 3 ? you can do:

app/models/article.rb

# app/models/article.rb
class Article
  include Shared::StripFields

  strip_fields :name, :brand
end

app/models/shared/strip_fields.rb

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.

Unexpected behavior with trait arguments

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.

Expected behavior

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.

Workaround

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.