Class: Typelizer::Shape

Inherits:
Object
  • Object
show all
Defined in:
lib/typelizer/shape.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties:) ⇒ Shape

Returns a new instance of Shape.



7
8
9
10
11
12
# File 'lib/typelizer/shape.rb', line 7

def initialize(properties:)
  @properties = properties.freeze
  @fingerprint = ["Shape", properties.map(&:fingerprint)].freeze
  @hash = @fingerprint.hash
  freeze
end

Instance Attribute Details

#fingerprintObject (readonly)

Returns the value of attribute fingerprint.



28
29
30
# File 'lib/typelizer/shape.rb', line 28

def fingerprint
  @fingerprint
end

#hashObject (readonly)

Returns the value of attribute hash.



28
29
30
# File 'lib/typelizer/shape.rb', line 28

def hash
  @hash
end

#propertiesObject (readonly)

Returns the value of attribute properties.



5
6
7
# File 'lib/typelizer/shape.rb', line 5

def properties
  @properties
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



30
31
32
# File 'lib/typelizer/shape.rb', line 30

def ==(other)
  other.is_a?(Shape) && fingerprint == other.fingerprint
end

#inspectObject



35
36
37
# File 'lib/typelizer/shape.rb', line 35

def inspect
  "<Typelizer::Shape properties=#{properties.inspect}>"
end

#map_propertiesObject



14
15
16
# File 'lib/typelizer/shape.rb', line 14

def map_properties
  self.class.new(properties: properties.map { |p| yield p })
end

#render(sort_order: :none, prefer_double_quotes: false) ⇒ Object Also known as: to_s



18
19
20
21
22
23
24
# File 'lib/typelizer/shape.rb', line 18

def render(sort_order: :none, prefer_double_quotes: false)
  inner = properties.map { |p|
    (p.render(sort_order: sort_order, prefer_double_quotes: prefer_double_quotes) + ";")
      .gsub(/^/, "  ")
  }.join("\n")
  "{\n#{inner}\n}"
end