Skip to content

Commit

Permalink
Refactor function registration methods
Browse files Browse the repository at this point in the history
  • Loading branch information
project-eutopia committed Nov 22, 2018
1 parent aef42cb commit 64547c6
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions lib/keisan/functions/default_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,14 @@ def self.register_random_methods!(registry)
end

def self.register_date_time_methods!(registry)
registry.register!(:date, Proc.new {|*args|
if args.count == 1 && args.first.is_a?(::String)
AST::Date.new(::Date.parse(args.first))
else
AST::Date.new(::Date.new(*args))
end
}, force: true)
register_date_time!(registry)

registry.register!(:today, Proc.new { ::Date.today }, force: true)
registry.register!(:day, Proc.new {|d| d.mday }, force: true)
registry.register!(:weekday, Proc.new {|d| d.wday }, force: true)
registry.register!(:month, Proc.new {|d| d.month }, force: true)
registry.register!(:year, Proc.new {|d| d.year }, force: true)

registry.register!(:time, Proc.new {|*args|
if args.count == 1 && args.first.is_a?(::String)
AST::Time.new(::Time.parse(args.first))
else
AST::Time.new(::Time.new(*args))
end
}, force: true)

registry.register!(:now, Proc.new { ::Time.now }, force: true)
registry.register!(:hour, Proc.new {|t| t.hour }, force: true)
registry.register!(:minute, Proc.new {|t| t.min }, force: true)
Expand All @@ -158,6 +144,18 @@ def self.register_date_time_methods!(registry)
registry.register!(:to_time, Proc.new {|d| d.to_time }, force: true)
registry.register!(:to_date, Proc.new {|t| t.to_date }, force: true)
end

def self.register_date_time!(registry)
[::Date, ::Time].each do |klass|
registry.register!(klass.to_s.downcase.to_sym, Proc.new {|*args|
if args.count == 1 && args.first.is_a?(::String)
AST.const_get(klass.to_s).new(klass.parse(args.first))
else
AST.const_get(klass.to_s).new(klass.new(*args))
end
}, force: true)
end
end
end
end
end

0 comments on commit 64547c6

Please sign in to comment.