Sha256: f4b29be5f65602e51338e89ecabb3ce244c9cee1f2b643a18e9e0ec63ad7c625

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

$LOAD_PATH.unshift '../lib'
require 'memory_record'

class Palette
  include MemoryRecord
  memory_record [
    { key: :coral,  r: 255, g: 127, b:   0 },
    { key: :tomato, r: 255, g:  99, b:  71 },
    { key: :gold,   r: 255, g: 215, b:   0 },
  ]

  def rgb
    [r, g, b]
  end

  def hex
    "#" + rgb.collect { |e| "%02X" % e }.join
  end

  def name
    super.capitalize
  end
end

Palette[:tomato].key        # => :tomato
Palette[:tomato].name       # => "Tomato"
Palette[:tomato].rgb        # => [255, 99, 71]
Palette[:tomato].hex        # => "#FF6347"
Palette.collect(&:hex)      # => ["#FF7F00", "#FF6347", "#FFD700"]

Palette.valid_key(:tomato)          # => :tomato
Palette.valid_key("tomato")         # => :tomato
Palette.valid_key(1)                # => :tomato
Palette.valid_key(:xxx)             # => nil
Palette.valid_key(:xxx, :tomato)    # => :tomato
Palette.valid_key(:xxx) { :tomato } # => :tomato

Palette.valid_key(:xxx, :tomato) # => :tomato
Palette.valid_key(:xxx, :black) rescue $! # => #<KeyError: Palette.fetch(:black) does not match anything

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
memory_record-0.0.19 examples/0100_basic.rb
memory_record-0.0.18 examples/0100_basic.rb