Module: Swagger::Bash

Included in:
Builder
Defined in:
lib/swagger/builder.rb

Overview

A Bash is a ‘builder’ Hash that can be used to be a Dash (‘defined’ or ‘discrete’ Hash). It provides a build method that turns it into a Dash. It enforces the same rules as a Dash except for ‘required’ properties, which are not enforced until converting to a Dash via build.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(dash) ⇒ Object

rubocop:disable Metrics/MethodLength



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/swagger/builder.rb', line 20

def self.included(dash) # rubocop:disable Metrics/MethodLength
  fail TypeError, 'Bash only works on Dash' unless dash <= Hashie::Dash
  dash.extend ClassMethods
  dash.instance_variable_get('@required_properties').clear

  # Very hacky... copy instance_variables for coercions
  base_dash = dash.superclass
  [:@properties, :@defaults].each do | property |
    dash.instance_variable_set(property, base_dash.instance_variable_get(property))
  end

  [:@key_coercions, :@value_coercions].each do | property |
    coercions = base_dash.instance_variable_get(property)
    coercions.each_pair do | key, into |
      infect_class coercions, key, into
    end if coercions
    dash.instance_variable_set(property, coercions)
  end

  def [](key, &_block)
    super(key) do |v|
      if block_given?
        v ||= send(:[]=, key, {})
        yield v
        v
      end
    end
  end
end

Instance Method Details

#[](key, &_block) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/swagger/builder.rb', line 39

def [](key, &_block)
  super(key) do |v|
    if block_given?
      v ||= send(:[]=, key, {})
      yield v
      v
    end
  end
end

#buildObject



50
51
52
# File 'lib/swagger/builder.rb', line 50

def build
  self.class.superclass.new(to_hash)
end