Skip to content

Commit

Permalink
Merge pull request #3 from kdgm/feature/kwargs
Browse files Browse the repository at this point in the history
Add support for Ruby 3.X `**kwargs`
  • Loading branch information
LeipeLeon authored Sep 11, 2024
2 parents b6cfd5e + 8fba102 commit 8025c1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ jobs:
- "2.5"
- "2.6"
- "2.7"
- "3.0"
- "3.1"
- "3.2"
- "3.3"
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
Expand Down
24 changes: 12 additions & 12 deletions lib/to_json/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,42 @@ def self.included(base)
end

module ClassMethods
def encode!(*args, &block)
new.encode!(*args, &block)
def encode!(*args, **kwargs, &block)
new.encode!(*args, **kwargs, &block)
end

def json!(*args, &block)
new.json!(*args, &block)
def json!(*args, **kwargs, &block)
new.json!(*args, **kwargs, &block)
end
end

#
# instance methods
#

def json!(*args, &block)
encode!(*args, &block)
def json!(*args, **kwargs, &block)
encode!(*args, **kwargs, &block)
to_json
end

def encode!(*args, &block)
def encode!(*args, **kwargs, &block)
@_scope = args[0]
@_obj_depth = 0
if @_oj
@_oj.reset
else
@_oj = Oj::StringWriter.new(:mode => :compat)
end
serialize(*args, &block)
serialize(*args, **kwargs, &block)
@_oj.pop_all
self
end

def serialize(*args, &block)
def serialize(*args, **kwargs, &block)
# the following line gets the callers 'self' so we can copy instance vars and delegate to it
@_scope = ::Kernel.eval 'self', block.binding
_copy_ivars @_scope
instance_exec(*args, &block)
instance_exec(*args, **kwargs, &block)
end

def to_json
Expand Down Expand Up @@ -277,9 +277,9 @@ def put!(key=nil, value=nil, &block)
@_key = nil # ensure key is cleared
end

def method_missing(method, *args, &block)
def method_missing(method, *args, **kwargs, &block)
# delegate to the scope
@_scope.send method, *args, &block
@_scope.send method, *args, **kwargs, &block
end

def const_missing(name)
Expand Down

0 comments on commit 8025c1f

Please sign in to comment.