Socket
Book a DemoInstallSign in
Socket

records

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

records

1.0.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

Records - Frozen / Immutable Structs with Copy on Updates

records gem / library - frozen / immutable structs with copy on updates

  • home :: github.com/s6ruby/records
  • bugs :: github.com/s6ruby/records/issues
  • gem :: rubygems.org/gems/records
  • rdoc :: rubydoc.info/gems/records

Usage

Use Record.new like Struct.new to build / create a new frozen / immutable record class. Example:


Record.new( :Account,
              balance:    Integer,
              allowances: Hash )

# -or-

Record.new :Account,
              balance:    Integer,
              allowances: Hash

# -or-

Record.new :Account, { balance:    Integer,
                       allowances: Hash }

# -or-

class Account < Record::Base
  field :balance,    Integer
  field :allowances, Hash
end

And use the new record class like:

account1a  = Account.new( 1, {} )
account1a.frozen?            #=> true
account1a.values.frozen?     #=> true
account1a.balance            #=> 1
account1a.allowances         #=> {}
account1a.values             #=> [1, {}]

Account.keys                 #=> [:balance, :allowances]
Account.fields               #=> [<Field @key=:balance, @index=0, @type=Integer>,
                             #    <Field @key=:allowances, @index=1, @type=Hash>]
Account.index( :balance )    #=> 0
Account.index( :allowances ) #=> 1

Note: The update method (or the << alias) ALWAYS returns a new record.

account1a.update( balance: 20 )     #=> [20, {}]
account1a.update( balance: 30 )     #=> [30, {}]
account1a.update( { balance: 30 } ) #=> [30, {}]

account1a << { balance: 20 }        #=> [20, {}]
account1a << { balance: 30 }        #=> [30, {}]

account1b = account1a.update( balance: 40, allowances: { 'Alice': 20 } )
account1b.balance                   #=> 40
account1b.allowances                #=> { 'Alice': 20 }
account1b.values                    #=> [40, { 'Alice': 20 } ]
# ...

And so on and so forth.

Bonus - Record Update Language Syntax Pragmas - {...} and ={...}

Using the Record Update Pragma. Lets you

account1a {... balance: 20 }     #=> [20, {}]
account1a {... balance: 30 }     #=> [30, {}]

account1a = {... balance: 40, allowances: { 'Alice': 20 }}

turn into:

account1a.update( balance: 20 )     #=> [20, {}]
account1a.update( balance: 30 )     #=> [30, {}]

account1a = account1a.update( balance: 40, allowances: { 'Alice': 20 } )

See Language Syntax Pragmas - Let's Evolve Ruby by Experimenting in a Pragma(tic) Way for more.

License

The records scripts are dedicated to the public domain. Use it as you please with no restrictions whatsoever.

Questions? Comments?

Send them along to the wwwmake forum. Thanks!

FAQs

Package last updated on 13 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.