Module: Marley::Plugins::OrmRestConvenience::InstanceMethods

Defined in:
lib/marley/plugins/orm_rest_convenience.rb

Instance Method Summary collapse

Instance Method Details

#actions(parent_instance = nil) ⇒ Object



63
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 63

def actions(parent_instance=nil); _instance_actions; end

#authorize(verb) ⇒ Object

the next 2 will have to be overridden for most applications



54
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 54

def authorize(verb); true ; end

#col_mods_match(mod_type) ⇒ Object



57
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 57

def col_mods_match(mod_type); lambda {|c| c.to_s.match(Regexp.union(send(:"_#{mod_type}")))}; end

#hidden_colsObject



61
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 61

def hidden_cols; rest_cols.select &col_mods_match(:hidden_cols);end

#reggae_instance(parent_instance = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 90

def reggae_instance(parent_instance=nil)
  a=Marley::ReggaeInstance.new( 
    {:name => self.class.resource_name,:url => url ,:new_rec => self.new?,:schema => reggae_schema,:actions => self.actions(parent_instance)}
  )
  a.contents=rest_associations.to_a.map do |assoc|
    assoc.map{|instance|  instance.reggae_instance(self)} 
  end unless new?
  a
end


105
106
107
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 105

def reggae_link(action='')
  ReggaeLink.new({:url => url,:title => "#{action.humanize}"})
end

#reggae_schema(list_schema = false) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 67

def reggae_schema(list_schema=false)
  Marley::ReggaeSchema.new(
    rest_cols.map do |col_name|
      db_spec=db_schema.to_hash[col_name]
      col_type=db_spec ? db_spec[:db_type].downcase : "text"
      col_type=:password if col_name.to_s.match(/password/)
      if list_schema
        restrictions=RESTRICT_RO
        restrictions|=RESTRICT_HIDE if hidden_cols.include?(col_name)
        [col_type, col_name, restrictions]
      else
        restrictions=0
        restrictions|=RESTRICT_HIDE if hidden_cols.include?(col_name)
        restrictions|=RESTRICT_RO unless write_cols.include?(col_name)
        restrictions|=RESTRICT_REQ if required_cols.include?(col_name) || (db_spec && !db_spec[:allow_null])
        [col_type, col_name, restrictions,send(col_name)]
      end
    end 
  )
end

#required_colsObject



62
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 62

def required_cols; rest_cols.select &col_mods_match(:required_cols);end

#requires_user?Boolean

Returns:

  • (Boolean)


55
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 55

def requires_user?; false; end

#rest_associationsObject



65
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 65

def rest_associations;[];end

#rest_colsObject



59
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 59

def rest_cols; _derived_before_cols.to_a + (columns.reject &col_mods_match(:reject_cols)) + _derived_after_cols.to_a;end

#to_json(*args) ⇒ Object



99
100
101
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 99

def to_json(*args)
  reggae_instance.to_json
end

#to_sObject



87
88
89
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 87

def to_s
  respond_to?('name') ? name : "#{self.class.name} #{id.to_s}"
end

#url(action = nil) ⇒ Object



102
103
104
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 102

def url(action=nil)
  "/#{self.class.resource_name}/#{self[:id]}/#{action}".sub(/\/$/,'')
end

#write_colsObject



60
# File 'lib/marley/plugins/orm_rest_convenience.rb', line 60

def write_cols; rest_cols.reject &col_mods_match(:ro_cols);end