mlipper / runt Goto Github PK
View Code? Open in Web Editor NEWRuby Temporal Expressions
License: MIT License
Ruby Temporal Expressions
License: MIT License
I discovered Runt this week while trying to implement an iCalender type recurring event in a Rails application. I see were the icalendar tests where included, but many test would not work because of lack support for negative mday range.
I'm not very proficient in Ruby, but I attacked the problem by overwriting the REMonth include? method to support negative ranges (which you accept, but don't work)
module Runt
class REMonth
include TExprUtils
def include?(date)
if @range.begin < 0 || @range.end < 0
include_negative(date)
else
@range.include? date.mday
end
end
def include_negative(date)
eom_mday = max_day_of_month(date)
range = (@range.begin < 0 ? eom_mday + @range.begin + 1 : @range.begin)..(@range.end < 0 ? eom_mday + @range.end + 1 : @range.end)
range.include? date.mday
end
end
end
I couldn't get the tests to run (didn't clone Runt), but this patch seems to work.
# from icalendar test 18
start_date = DateTime.parse("US-Eastern:19970930T090000") #Sep 30, 1997
end_date = start_date + 365 #Sep 30, 1998
test_date = DateTime.parse("US-Eastern:19971031T090000") #Oct 31
#rrule = RecurrenceRule.new("FREQ=MONTHLY;COUNT=10;BYMONTHDAY=1,-1")
te = REMonth.new(1) | REMonth.new(-1) #first and last days of the month
results = te.dates(DateRange.new(start_date, end_date), 10)
I did notice that if I used Time instead of DateTime and got an error:
results = te.dates(DateRange.new(start_date, end_date), 10)
TypeError: can't iterate from Time
I just have to watch that when I actually try to do something.
monthly_3rd_to_14th
creates a REMonth object:
2.0.0-p247 :127 > m34 = monthly_3rd_to_14th
=> #<Runt::REMonth:0x00000104e4b628 @range="3".."14">
and passess the monthly sugar test, since you convert expected and actual to a string. Calling m34.include?(date)
will not work. (looking for integer in string range)
I found this trying to add my negative_month offset and testing it. I changed
st_day, end_day = $1, $2
#to
st_day, end_day = $1.to_i, $2.to_i
and all test pass and monthly_3rd_to_14th
works.
After adding runt gem to the rails project, some of the functions provided by Rails' DateTime do not work properly.
For eg,
DateTime.now + 1.day #gives some random date in 2035
DateTime.now + 2.hours #gives some random date in 2033
Any idea what might fix these issues?
On the "paydici" branch, on Ruby 2.0.0 we see this error repeatedly when running rake test.
Similarly: undefined method '+' for nil class, undefined method '-' for nil class.
I do not see anything to do with infinite recursion in sugar.rb the way I do on Ruby 1.9.3.
I ran this:
Bills-MacBook-Air:runt-bill Bill$ rdebug -I lib -I test ./test/icalendartest.rb -n test_example_1
With a break point at icalendartest.rb:23 and stepped a few times and saw this in sugar.rb:
156 def method_missing(name, *args, &block)
=> 157 result = self.build(name, *args, &block)
158 return result unless result.nil?
159 super
160 end
161
(rdb:1) p self
(rdb:1) p name
:to_str
Here is the stack:
--> #0 Runt.method_missing(name#Symbol)
at line /Users/Bill/Projects/Rails/runt-bill/lib/runt/sugar.rb:157
#1 Runt.method_missing(name#Symbol)
at line /Users/Bill/Projects/Rails/runt-bill/lib/runt/sugar.rb:159
#2 Runt::TExpr.dates(date_range#Runt::DateRange)
at line /Users/Bill/Projects/Rails/runt-bill/lib/runt/temporalexpression.rb:79
I wonder if DateTime#method_missing (probably defined up in Kernel or Object) is causing to_str to be invoked on self which in turn is invoking method_missing (down in the Runt module) again!
I've tried using runt with the following ruby versions:
In both cases i got a recursion error somewhat hard to debug. I've tried also the Readme example in irb getting the very same error ( irb says also that it could be its own error: maybe irb bug). The Readme example and tutorials work well in ruby 1.8.7.
Reading the issues here i decided to move to ruby 2.0.0p195. I can try the tutorials in irb without any error. However if i bundle runt with my Rails 3.2.13 application the ruby process goes up to 99% of cpu usage and POW become unresponsive.
I'm on OS X Lion 10.7.5. I handle ruby versions with rbenv and ruby-build. runt gem version is updated to 0.9.0.
I wonder if you've integrated runt into a rails app. I'm trying to figure out how I would combine all of my schedule inputs into a serializable hash to store in an hstore field and then deserialize it to make use of the schedule to check for availability.
Since there are multiple layers to a schedule, as in your example with a north and south expression with multiple dates and exclusions, the complexity is leaving me a bit stumped.
Do you think that it would be a better idea to store all of the elements separately in my database and then interact with each individual element once I retrieve a particular schedule's dates and relevant events?
$ rake test
...
rake aborted!
Don't know how to build task 'check_dependencies'
It is not clear to me from this documentation (without a very convoluted approach) how to create an expression that is "exactly this date, no repeat."
How might I go about saying:
only March 3, 2018
How might I go about saying:
except March 3, 2018
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.