Coder Social home page Coder Social logo

profile's Introduction

profile

Simple tool to define objects named data sets

Usage


class MyClass
  include Profile::Document
  define_profile :some_profile, %w(attr0 attr1 meth0)
  attr_accessor :attr0, attr1, attr2

  def meth0
    "#{attr0}: #{attr1}"
  end
end

c = MyClass.new
c.attr0 = "foo"
c.attr1 = "bar"
c.attr2 = "tar"

p c.profile.some_profile
# produces { :attr0 => "foo", :attr1 => "bar", :meth0 => "foo: bar" }

# profile also extends ruby enumerable instances:
collection = []
class A
  include Profile::Document
  define_profile :b, %w(foo bar)
  attr_accessor :foo

  def bar
    foo * foo
  end
end

10.times do |i|
  obj = A.new
  obj.foo = i
  collection << i
end

p collection.profile(:b)
# produces [{:foo=>0, :bar=>0}, {:foo=>1, :bar=>1},
  {:foo=>2, :bar=>4}, {:foo=>3, :bar=>9}, {:foo=>4, :bar=>16}, 
  {:foo=>5, :bar=>25}, {:foo=>6, :bar=>36}, {:foo=>7, :bar=>49}, 
  {:foo=>8, :bar=>64}, {:foo=>9, :bar=>81}]

new in version 0.2

You can define profiles via hash, this way input hash keys would be a result keys, and input key values would be an instance methods to call when profile builds.

class B
  include Profile::Document
  define profile :b, :k1 => v1, :k2 => v2
  attr_accessor :v1

  def v2
    v1 * v1
  end
end

b = B.new
b.v1 = 5
p b.profile.b
# produces {:k1=>5, :k2=>25}

new in version 0.3

# profiles are extendable
# You can define base profile
# and extend it any way you need

class C
  include Profile::Document
  attr_accessor :attr1, :attr2, :attr3, :attr4
  define_profile do |p|
    p.profile1 = %w(attr1 attr2)
    p.profile2 = p.profile1.continue(%w(attr3))
    p.profile3 = p.profile2.continue(%w(attr4))
  end
end

Real life usage

This gem may be powerful using with rails and github.com/josevalim/inherited_resources gem when your model become fat. Suppose you need to render select options using json API. No reason to transfer all the model objects data in this case. Example below show how to reject useless fields in API response:

# apps/controllers/items.rb

class ItemsController < InheritedResources::Base
  respond_to :json, :xml, :html
  def index
    index! do |wants|
      wants.json { respond_with collection.profile(:json) }
      wants.xml { respond_with collection.profile(:xml) }
    end
  end
end

# apps/models/item.rb
# No mater object model you are using here
# Here is an example with mongoid odm:
class Item
  include Mongoid::Document
  include Profile::Document
  field :title
  field :description # ok, description is helpful for api
  field :wery_wery_long_text # but this field is not

  define_profile do |p|
    p.xml = { :id => :_id, :brief => :description }
    p.json = p.xml.continue(%w(extra)) # so, json profile would contain th same fields as xml extended by extra field
  end

  def extra
    # something helpful here
    ...
  end
end

Copyright © 2011 4pcbr. See LICENSE.txt for further details.

profile's People

Contributors

osdrv avatar

Stargazers

 avatar

Watchers

 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.