Skip to content

Instantly share code, notes, and snippets.

@fgarcia
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fgarcia/9277156 to your computer and use it in GitHub Desktop.
Save fgarcia/9277156 to your computer and use it in GitHub Desktop.
require 'virtus'
# This Works!
class User
include Virtus.model
attribute :name, String
attribute :group, String
end
# Class User trying to make private attributes
class User_Private
include Virtus.model
attribute :name, String, :accessor => private
attribute :group, String, :accessor => private
end
# This fails!
u = User_Private.new(name:"demo", group:"staff")
###
# I am trying to have this ...
###
class UserForm < User
# ... something to make all attributes READ-ONLY
end
u = UserForm.new(name:"demo", group:"staff")
u = UserForm.new(user_post_data_hash)
u.name = 'other' # FAIL
u.name # OK
class UserEntity < User
# ... something to make all attributes PRIVATE
end
model = UserForm.new(name:"demo", group:"staff")
u = UserEntity(model)
u.name = 'other' # FAIL
u.name # FAIL
class UserValidationGUI < User
end
class UserValidationDomain < User
end
@fgarcia
Copy link
Author

fgarcia commented Feb 28, 2014

My problem is that I try to avoid the standard Virtus object examples. Usually I split them in two groups. One case is for value-like containers (all attributes read-only) like UserForm. The other case is for state-containing objects. A class containing state, must expose no attributes, not even read-only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment