GithubHelp home page GithubHelp logo

annawinkler / domain_model Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rafer/domain_model

0.0 2.0 0.0 115 KB

A minimal set of utilities for declaring object attributes with optional type and validation support

License: MIT License

Ruby 100.00%

domain_model's Introduction

DomainModel

DomainModel provides a minimal set of utilties for declaring object attributes with optional type and validation support.

Usage

Add DomainModel to your class like this:

class Fish
  include DomainModel
end

Then add attributes to your class using the keyword field like this:

class Fish
  include DomainModel

  field :color, :required => true, :type => String
  field :size, :type => Float, :validate => true
  field :habitat, :type => Habitat, :validate => true
  ...

end

class Habitat
  include DomainModel

  field :temperature, :type => Float, :validate => true
  field :bottom_type, :type => String

  ...
end

The name of your field (or attribute) is required. When you specify a field, you get getters and setters automatically:

red_fish = Fish.new
red_fish.color = "red"
red_fish.habitat = Habitat.new({
  :temperature => 75.0,
  :bottom_type => "sand"
)

A field has these optional attributes:

Attribute Description
:type The type of the field, which could be any valid Ruby Object type, including custom classes.
:required Is this a required field? Used for validation.
:collection The field is an array of values. Note that you can't have a required collection.
:validate Validate the field. If this is a collection, will validate the collection's items too.

You can extend the validation with custom code in your class:

validate :size do |errors|
  if size < 0.0 || size > 500.0 # In our world fish can only be so big
    errors.add("has to be between 0.0 and 500.0")
  end
end

Then when you create an instance of your class, you can ask if the class is valid and get any errors:

my_fish = Fish.new({ :color => "green", :size => "-10" })
if my_fish.valid?
  go_fishing
else
  my_fish.errors
end

errors is a Enumerable object of error objects. If you want to print out all of the error messages from validation, you'll want to get the flat_errors which is an array of the error messages. If you have nested DomainModel objects and you want to see all the errors, use flat_errors.

Development

To run the tests (assuming you have already run gem install bundler):

bundle install && bundle exec rake

domain_model's People

Contributors

rafer avatar

Watchers

 avatar  avatar

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.