From 44d753ad7fa39e19b373c44ceafba885bb95513b Mon Sep 17 00:00:00 2001 From: Christopher Locke Date: Wed, 31 Jan 2018 19:31:32 -0800 Subject: [PATCH] Remove activesupport dependency --- keisan.gemspec | 2 -- lib/keisan.rb | 4 +--- lib/keisan/ast/builder.rb | 4 ++-- lib/keisan/ast/operator.rb | 2 +- lib/keisan/ast/parent.rb | 2 +- lib/keisan/ast/unary_operator.rb | 2 +- lib/keisan/context.rb | 8 ++++---- lib/keisan/parser.rb | 8 ++++---- lib/keisan/parsing/function.rb | 2 +- lib/keisan/parsing/hash.rb | 4 ++-- lib/keisan/token.rb | 2 +- lib/keisan/util.rb | 19 +++++++++++++++++++ 12 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 lib/keisan/util.rb diff --git a/keisan.gemspec b/keisan.gemspec index bccfd87..2afde4c 100644 --- a/keisan.gemspec +++ b/keisan.gemspec @@ -21,8 +21,6 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.0.0" - spec.add_dependency "activesupport", ">= 4.2.2" - spec.add_development_dependency "coveralls" spec.add_development_dependency "bundler", "~> 1.14" spec.add_development_dependency "rake", "~> 10.0" diff --git a/lib/keisan.rb b/lib/keisan.rb index f91cce3..eb12b1f 100644 --- a/lib/keisan.rb +++ b/lib/keisan.rb @@ -1,8 +1,6 @@ -require "active_support" -require "active_support/core_ext" - require "keisan/version" require "keisan/exceptions" +require "keisan/util" require "keisan/ast/node" require "keisan/ast/cell" diff --git a/lib/keisan/ast/builder.rb b/lib/keisan/ast/builder.rb index 1759931..ec8d4ab 100644 --- a/lib/keisan/ast/builder.rb +++ b/lib/keisan/ast/builder.rb @@ -12,10 +12,10 @@ def initialize(string: nil, parser: nil, components: nil) elsif !parser.nil? @components = parser.components else - @components = Array.wrap(components) + @components = Array(components) end - @lines = @components.split {|component| + @lines = Util.array_split(@components) {|component| component.is_a?(Parsing::LineSeparator) }.reject(&:empty?) diff --git a/lib/keisan/ast/operator.rb b/lib/keisan/ast/operator.rb index f7e0978..716f11e 100644 --- a/lib/keisan/ast/operator.rb +++ b/lib/keisan/ast/operator.rb @@ -41,7 +41,7 @@ def initialize(children = [], parsing_operators = []) raise Exceptions::ASTError.new("Mismatch of children and operators") end - children = Array.wrap(children) + children = Array(children) super(children) @parsing_operators = parsing_operators diff --git a/lib/keisan/ast/parent.rb b/lib/keisan/ast/parent.rb index 03f1e24..484f204 100644 --- a/lib/keisan/ast/parent.rb +++ b/lib/keisan/ast/parent.rb @@ -4,7 +4,7 @@ class Parent < Node attr_reader :children def initialize(children = []) - children = Array.wrap(children).map do |child| + children = Array(children).map do |child| child.is_a?(Cell) ? child : child.to_node end raise Exceptions::InternalError.new unless children.is_a?(Array) diff --git a/lib/keisan/ast/unary_operator.rb b/lib/keisan/ast/unary_operator.rb index c32dfa9..1f4f674 100644 --- a/lib/keisan/ast/unary_operator.rb +++ b/lib/keisan/ast/unary_operator.rb @@ -2,7 +2,7 @@ module Keisan module AST class UnaryOperator < Operator def initialize(children = []) - children = Array.wrap(children) + children = Array(children) super(children) if children.count != 1 raise Exceptions::ASTError.new("Unary operator takes has a single child") diff --git a/lib/keisan/context.rb b/lib/keisan/context.rb index c673e71..cf0151b 100644 --- a/lib/keisan/context.rb +++ b/lib/keisan/context.rb @@ -4,8 +4,8 @@ class Context def initialize(parent: nil, random: nil, allow_recursive: false, shadowed: []) @parent = parent - @function_registry = Functions::Registry.new(parent: @parent.try(:function_registry)) - @variable_registry = Variables::Registry.new(parent: @parent.try(:variable_registry), shadowed: shadowed) + @function_registry = Functions::Registry.new(parent: @parent&.function_registry) + @variable_registry = Variables::Registry.new(parent: @parent&.variable_registry, shadowed: shadowed) @random = random @allow_recursive = allow_recursive end @@ -47,7 +47,7 @@ def spawn_child(definitions: {}, shadowed: [], transient: nil) def transient_definitions return {} unless @transient - parent_definitions = @parent.present? ? @parent.transient_definitions : {} + parent_definitions = @parent.nil? ? {} : @parent.transient_definitions parent_definitions.merge( @variable_registry.locals ).merge( @@ -100,7 +100,7 @@ def register_function!(name, function, local: false) end def random - @random || @parent.try(:random) || Random.new + @random || @parent&.random || Random.new end protected diff --git a/lib/keisan/parser.rb b/lib/keisan/parser.rb index 137ca83..13af6e8 100644 --- a/lib/keisan/parser.rb +++ b/lib/keisan/parser.rb @@ -39,7 +39,7 @@ def multi_line? end def parse_multi_line! - line_parsers = @tokens.split {|token| token.is_a?(Tokens::LineSeparator)}.map {|tokens| self.class.new(tokens: tokens)} + line_parsers = Util.array_split(@tokens) {|token| token.is_a?(Tokens::LineSeparator)}.map {|tokens| self.class.new(tokens: tokens)} @components = [] line_parsers.each.with_index do |line_parser, i| @components += line_parser.components @@ -52,7 +52,7 @@ def parse_multi_line! def parse_keyword! keyword = tokens.first.string arguments = if tokens[1].is_a?(Tokens::Group) - tokens[1].sub_tokens.split {|token| token.is_a?(Tokens::Comma)}.map {|argument_tokens| + Util.array_split(tokens[1].sub_tokens) {|token| token.is_a?(Tokens::Comma)}.map {|argument_tokens| Parsing::Argument.new(argument_tokens) } else @@ -212,7 +212,7 @@ def add_group_element_components!(token) @components << Parsing::List.new(arguments_from_group(token)) when :curly if token.sub_tokens.any? {|token| token.is_a?(Tokens::Colon)} - @components << Parsing::Hash.new(token.sub_tokens.split {|token| token.is_a?(Tokens::Comma)}) + @components << Parsing::Hash.new(Util.array_split(token.sub_tokens) {|token| token.is_a?(Tokens::Comma)}) else @components << Parsing::CurlyGroup.new(token.sub_tokens) end @@ -280,7 +280,7 @@ def arguments_from_group(token) if token.sub_tokens.empty? [] else - token.sub_tokens.split {|sub_token| sub_token.is_a?(Tokens::Comma)}.map do |sub_tokens| + Util.array_split(token.sub_tokens) {|sub_token| sub_token.is_a?(Tokens::Comma)}.map do |sub_tokens| Parsing::Argument.new(sub_tokens) end end diff --git a/lib/keisan/parsing/function.rb b/lib/keisan/parsing/function.rb index ebbe587..ad2f5ba 100644 --- a/lib/keisan/parsing/function.rb +++ b/lib/keisan/parsing/function.rb @@ -5,7 +5,7 @@ class Function < Element def initialize(name, arguments) @name = name - @arguments = Array.wrap(arguments) + @arguments = Array(arguments) end end end diff --git a/lib/keisan/parsing/hash.rb b/lib/keisan/parsing/hash.rb index 43cf55b..703d720 100644 --- a/lib/keisan/parsing/hash.rb +++ b/lib/keisan/parsing/hash.rb @@ -4,7 +4,7 @@ class Hash < Element attr_reader :key_value_pairs def initialize(key_value_pairs) - @key_value_pairs = Array.wrap(key_value_pairs).map {|key_value_pair| + @key_value_pairs = Array(key_value_pairs).map {|key_value_pair| validate_and_extract_key_value_pair(key_value_pair) } end @@ -12,7 +12,7 @@ def initialize(key_value_pairs) private def validate_and_extract_key_value_pair(key_value_pair) - key, value = key_value_pair.split {|token| token.is_a?(Tokens::Colon)} + key, value = Util.array_split(key_value_pair) {|token| token.is_a?(Tokens::Colon)} raise Exceptions::ParseError.new("Invalid hash") unless key.size == 1 && value.size >= 1 key = key.first diff --git a/lib/keisan/token.rb b/lib/keisan/token.rb index 3300155..19e5fc6 100644 --- a/lib/keisan/token.rb +++ b/lib/keisan/token.rb @@ -12,7 +12,7 @@ def type end def self.type - @type ||= self.to_s.split("::").last.underscore.to_sym + @type ||= Util.underscore(self.to_s.split("::").last).to_sym end def regex diff --git a/lib/keisan/util.rb b/lib/keisan/util.rb new file mode 100644 index 0000000..19a110c --- /dev/null +++ b/lib/keisan/util.rb @@ -0,0 +1,19 @@ +module Keisan + class Util + def self.underscore(str) + str.to_s.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').downcase + end + + def self.array_split(array, &block) + array.inject([[]]) do |results, element| + if yield(element) + results << [] + else + results.last << element + end + + results + end + end + end +end