Sha256: eedca8aa32e6ce68cc6b2fb1035d98aed8185e8803ce44d166e719fd0022677f

Contents?: true

Size: 1.07 KB

Versions: 4

Compression:

Stored size: 1.07 KB

Contents

module SmartMonth
  # Magic is responsible for the rails-like dynamic "magic finders".
  module Magic
    
    # extends the Month class with the Month factory
    def self.included(base)
      base.extend MonthFactory
    end
    
    # This module allows your code to ignore the class instantiation, simply do Month.june!
    module MonthFactory
      # singleton month factory
      def method_missing(meth,*args)
        return new(meth.to_s.capitalize, (args.first || Time.now.year)) if Month::NAMES.include? meth.to_s.capitalize
      end
      
      # this allows you to essentially treat the month as a hash or array object to construct new months.
      def [](month)
         new(month)
      end
    end
    
    # nice catchall to allow rails-esque APIs if you choose to.
    def method_missing(meth)
      raw = meth.to_s.split("_")
      func = raw.slice!(0)
      if func == "every"
        args = raw.select { |a| a != "and" }
        self.send(func,args)
      else
        self.send(func,raw) if %w(first second third fourth last).include? func
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rmoriz-smartmonth-1.0.2 lib/smart_month/magic.rb
rmoriz-smartmonth-1.0 lib/smart_month/magic.rb
rmoriz-smartmonth-1.01 lib/smart_month/magic.rb
rmoriz-smartmonth-1.1.1 lib/smart_month/magic.rb