Giter VIP home page Giter VIP logo

cutesy's Introduction

Cutesy

Though the name will likely change seven times before I'm done with this, the intent probably won't. Cutesy is a, well, cutesy way to get nice, meaningful names for your setters. If you dig patterns, it's a flexible way to implement a Builder.

Why?

Whenever I sketch out ideas in code, I end up with nonsense like this: SleepPeriod.in(my_bed).with(teddy_bear).for(7.hours). I usually give that stuff up when I find myself writing methods that just tap and set an attribute, but then I decided that, no, I will have my sentences, and here we are.

Example

Suppose we have Person, and we're interested in a Person's name and birth date.

class Person

  attr_accessor :name, :birth_date

  # Person implementation

end

Now we can configure cutesy for Person:

Cutesy.klass Person do |person|
  person.sets :name, :with => :named
  person.sets :birth_date, :with => :born_on
end

And lo, we can do things like this:

ruby :001 > brian = Person.new.named('Brian').born_on('August 8th, 1988')
=> #<Person @name="Brian", @birth_date="August 8th, 1988">

Plays well (I hope!) with others

Out of the box, Cutesy will use an accessor if it can find one. Otherwise, it will set an instance variable directly.

But what do I know about anything? Maybe that behavior causes more problems for you than cute syntax is worth. No worries; you can bend Cutesy to your will. Let's say you have a Rails app and want to set every attribute using update_attribute (hypothetically, of course):

Cutesy.template do |object, attribute, value|
  object.update_attribute(attribute, value)
end

In the event that you have Cutesied up several classes, but you only need custom behavior for a subset of them, you can specify custom behavior on a per-class basis:

Cutesy.template Person do |object, attribute, value|
  # template will only be called for attributes on instances of Person
end

Finally, you can change the behavior for a single attribute of a single class:

Cutesy.template Person, :name do |object, attribute, value|
  # template will only be called when setting Person#name
end

Prior Art?

There's got to be something else like this, right? If so, please let me know. I'm really curious about other ways folks have indulged their prosaic tendencies.

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.