Git Product home page Git Product logo

classy_struct's Introduction

classy_struct

A better-performing alternative to OpenStruct

Usage

First, create a class instance of ClassyStruct:

require 'classy_struct'
Widget = ClassyStruct.new

Next, create instances of that class the same as you would with any other class:

w1 = Widget.new

These instances are open in much the same way that OpenStruct instances are open:

w1.foo = :bar
w1.foo  # => :bar

However, generated accessors are applied to the class instance (in this case, Widget) instead of the object instance. This means that accessors only need to be generated once for the class, and instances of that class get the accessors automatically.

You can also pass a Hash to the constructor, to be recursively converted to ClassyStruct objects:

w1 = Widget.new(:foo => :bar, :baz => {:xyzzy => :thud})
w1.foo        # => :bar
w1.baz.xyzzy  # => :thud

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Make sure you don’t break the existing specs.

  • Add specs for it. The specs should break with the buggy version if you’re fixing a bug, or verify the new functionality you’re adding. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches, which make it even easier to get the related changes and nothing else.

Copyright © 2009 Alf Mikula. See LICENSE for details.

classy_struct's People

Contributors

amikula avatar drprofesq avatar

Stargazers

Robby Dhillon avatar Angus H. avatar Yury Batenko avatar Terje Larsen avatar Tristan Harris avatar Chuck Remes avatar Mani Tadayon avatar Morgan Prior avatar Doug McInnes avatar  avatar Fernando Barajas avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

awesome djrobby

classy_struct's Issues

subclassing doesn't work

See this gist for the same error under both rubinius and mri 1.9.2:

https://gist.github.com/1079682

Here is my use-case. I send hashes over the network to communicate amongst distributed processes. The hash oftentimes look like so:

message = {
'code' => 3,
'operation' => 'create',
'payload' => {
'field1' => [1,2,3],
'field2' => 'string',
'field3' => 5
}
}

I would like to be able to pass that entire hash to a ClassyStruct and conditionally have only the payload get parsed. When accessing 'field1' and others, I don't want to do foo.payload.field1 (I throw away 'code' and 'operation'). I want to reach them directly like foo.field1.

So I write code like:

MessageParent = ClassyStruct.new

class Message < MessageParent
def initialize(document = nil)
if document
if document.has_key?('payload')
super(document['payload'])
else
super(document)
end
else
super()
end
end
end

This allows me to pass a one-level hash to Message and have it parsed the way I like it. The initializer also let's me pass a hash with an embedded payload and parse it the way I like it. Lastly, I can pass nothing/nil and set the fields manually.

Unfortunately, subclassing a ClassyStruct blows up. :(

Is there a work around or a fix?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.