Module: Binky::Struct

Includes:
Helper
Defined in:
lib/binky/builder.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Methods included from Helper

#accessor_builder, #attribute_from_inner_key, #build_by_keys, #nested_hash_value

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/binky/builder.rb', line 75

def method_missing(name, *args)
  attribute = name.to_s.start_with?(/\d/) ? "_#{name.to_s}" : name.to_s
  if attribute =~ /=$/
    if args[0].respond_to?(:key?) || args[0].is_a?(Hash)
      @to_h[attribute.chop] = self.class.new(args[0])
    else
      @to_h[attribute.chop] = args[0]
    end
  else
    @to_h[attribute]
  end
end

Instance Method Details

#initialize(json = nil) ⇒ Object



68
69
70
71
72
73
# File 'lib/binky/builder.rb', line 68

def initialize(json = nil)
  accessor_builder('to_h',{})
  json&.each do |k, v|
    self.send("#{k}=", v)
  end
end